第七章綜合練習空指針異常問題
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("撲克創(chuàng)建完成");
?? ?System.out.println("您創(chuàng)建的撲克如下");
?? ?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();
}?? ?
}
加黑部分運行的時候為什么會拋一個空指針異常 沒太想明白
2016-12-13
加黑部分是想存一個鍵值對兒到ArrayList里? 是不是和Map元素弄混了