空指针异常 帮忙看看怎么处理

来源:7-1 简易扑克牌游戏

马银玲180

2015-09-07 10:05

package www.imooc;


import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.Scanner;


public class PlayingGames {


/**

* @param args

*/

public List<Card> cardlist=new ArrayList<Card>();

public List<Player> playerlist=new ArrayList<Player>();// 用来放创建的玩家

public List<Card> cardlist1=new ArrayList<Card>(); //用于放玩家拿到的纸牌

/**

* 定义创建纸牌的方法

*/

public void CreatCards(){

String[] size={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

String[] huase={"方块","梅花","红桃","黑桃"};

for(int i=0;i<huase.length;i++){

for(int j=0;j<size.length;j++){

cardlist.add(new Card(huase[i],size[j]));

}

}

System.out.println("共添加了"+cardlist.size()+"张扑克牌");

for (Card cr : cardlist) {

System.out.print(cr.huase+cr.size+",");

}

}

public void sortcard(){

Collections.shuffle(cardlist);

System.out.println("洗牌结束");

}

public void createcard(){

CreatCards();

sortcard();

}

/**

* 创建玩家

* @param args

*/

public void CreatPlayer(){

for(int i=1;i<3;i++){

Scanner console=new Scanner(System.in);

Player player=new Player();

System.out.println("请输入第"+i+"位的ID和姓名");

for(int j=0;j<2;j++){

try{

String id=console.next();

player.ID=id;

break;

}catch(Exception e){

System.out.println("请输入一个整数ID");

continue;

}finally{

}

}

System.out.println("请输入第"+i+"位玩家的姓名");

String name=console.next();

player.name=name;

playerlist.add(player);

}

for (Player pl : playerlist) {

System.out.println(pl.ID+":"+pl.name);

}

}

   public void GiveCards(){

  int num=0;

  for(int i=0;i<4/playerlist.size();i++){

  for (Player player : playerlist) {

player.cards.add(cardlist.get(num));

num++;

}

  }

 

 for (Player player : playerlist) {

 for (Card card : player.cards) {

System.out.println(player.name+":"+card.huase+card.size);

}

}

  }

   


public static void main(String[] args) {

//

PlayingGames pg=new PlayingGames();

pg.createcard();

pg.CreatPlayer();

pg.GiveCards();

}


}

package www.imooc;


import java.util.ArrayList;

import java.util.List;


public class Player {

public String ID;

public String name;

public List<Card> cards;

public Player(String id,String name){

this.ID=id;

this.name=name;

cards=new ArrayList<Card>();

}

public Player(){

}

}

package www.imooc;


public class Card {

public  String huase;

public String size;



public Card(String huase,String size) {

// TODO Auto-generated constructor stub

this.huase=huase;

this.size=size;

}

public Card(){

}

}


Exception in thread "main" java.lang.NullPointerException

at www.imooc.PlayingGames.GiveCards(PlayingGames.java:83)

at www.imooc.PlayingGames.main(PlayingGames.java:104)




写回答 关注

1回答

  • 马银玲180
    2015-09-07 15:45:36

    已经解决

    DeppWa...

    怎么解决的?

    2016-04-08 21:14:22

    共 1 条回复 >

Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409792 学习 · 4340 问题

查看课程

相似问题