第七章综合练习空指针异常问题
package gamed;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.SynchronousQueue;
public class poker {
private String col;
private String num;
List<poker> a = new ArrayList<poker>();
List<player> playertocreate = new ArrayList<player>();
public poker(String col,String num){
this.col=col;
this.num=num;
}
public poker() {
// TODO Auto-generated constructor stub
}
public String getCol() {
return col;
}
public void setCol(String col) {
this.col = col;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public void creat(){
String[] col1 ={"黑桃","红桃","梅花","方片"};
String[] num1 ={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for(int i=0;i<col1.length;i++){
for(int j=0;j<num1.length;j++){
poker b = new poker(col1[i],num1[j]);
a.add(b);
}
}
System.out.println("扑克创建完成");
System.out.println("您创建的扑克如下");
for (poker po : a) {
System.out.println(po.col+po.num);
}
}
public void flash(){
Collections.shuffle(a);
System.out.println("洗牌完成");
}
public void Creatplayer(){
//List<player> playertocreate = new ArrayList<player>();
int i=0;
Scanner console = new Scanner(System.in);
while(i<2){
//try{
System.out.println("请输入第"+(i+1)+"个玩家的姓名");
player player = new player();
console = new Scanner(System.in);
player.setName(console.next());
console = new Scanner(System.in);
try{
System.out.println("请输入第"+(i+1)+"个玩家的ID");
player.setID(console.nextInt());
playertocreate.add(player);
i++;
}catch (Exception e) {
System.out.println("请输入正确的玩家ID");
}
}
for (player pl : playertocreate) {
System.out.println("欢迎"+pl.getName()+"您的ID为"+pl.getID());
}
}
public void sendpoker(){
for (player player : playertocreate) {
System.out.println(player.getName()+"拿牌");
Random random = new Random();
for(int i=0;i<2;i++){
player.playerpoker.add(i, (poker)a.get(random.nextInt(a.size())));
System.out.println(player.getName()+"拿到的"+(i+1)
+"牌为"+(poker)player.playerpoker.get(i));
}
}
}
public void match(){
}
public static void main(String[] args) {
poker p = new poker();
p.creat();
p.flash();
p.Creatplayer();
p.sendpoker();
}
}
加黑部分运行的时候为什么会抛一个空指针异常 没太想明白