为了账号安全,请及时绑定邮箱和手机立即绑定

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

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 回答

已经解决

0 回复 有任何疑惑可以回复我~
#1

DeppWang

怎么解决的?
2016-04-08 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Java入门第三季
  • 参与学习       409789    人
  • 解答问题       4340    个

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

进入课程

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

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信