特定于大小写的空指针异常问题-java

特定于大小写的空指针异常问题-java,java,list,nullpointerexception,linked-list,Java,List,Nullpointerexception,Linked List,我有一个关于空指针异常的奇怪问题。我将有问题的代码发布在下面: public static void main(String[] args) { BetHistory testObject = new BetHistory(6); testObject.addResponse(2, 1, 0); // ERROR HERE ... } public class PlayerResponses { public List<Integer> response;

我有一个关于空指针异常的奇怪问题。我将有问题的代码发布在下面:

public static void main(String[] args) {

BetHistory testObject = new BetHistory(6);

testObject.addResponse(2, 1, 0); //  ERROR HERE

...

}

public class PlayerResponses {

    public List<Integer> response;

    public PlayerResponses() {
        super();
        response = new LinkedList<Integer>();
    }

...

}

public class BetHistory {

    PlayerResponses [][] responses; 
    int nPlayer;

    public BetHistory(int totalPlayers) {
        super();
        responses = new PlayerResponses[4][totalPlayers];
        nPlayer = totalPlayers;

        }
    public void addResponse(int response, int playerNo, int roundNo)
    {
        responses[roundNo][playerNo].response.add(response); // DUE TO HERE
    }

...

}
publicstaticvoidmain(字符串[]args){
BethHistory testObject=新的BethHistory(6);
testObject.addResponse(2,1,0);//此处出错
...
}
公共类玩家响应{
公众名单回应;
公共玩家响应(){
超级();
response=newlinkedlist();
}
...
}
公共类历史{
PlayerResponses[][]响应;
int N层;
公共历史(整版){
超级();
响应=新玩家响应[4][totalPlayers];
nPlayer=全部玩家;
}
public void addResponse(int response、int playerNo、int roundNo)
{
回复[roundNo][playerNo].response.add(response);//此处到期
}
...
}
提前感谢您的时间。

在这行:

responses = new PlayerResponses[4][totalPlayers];
您可以创建一个二维数组,用于保存对
PlayerResponses
对象的引用。您甚至不创建一个
PlayerResponses
对象

您需要在循环中填写这些内容(如果需要,可在以后按需填写):


那是因为
公共列表响应playerResponse中,存储在数组中的
响应未初始化。
它获得默认的
null
,这就是为什么当您调用
add
方法时,您会得到NPE。

import java.util.*;
import java.util.*;

    public class A {
    public static void main(String[] args) {

            BetHistory testObject = new BetHistory(6);

            testObject.addResponse(2, 1, 0); //  ERROR HERE

    }
    }

    class PlayerResponses {

        public LinkedList<Integer> response;

        public PlayerResponses() {
            super();
            response = new LinkedList<Integer>();
        }


    }

    class BetHistory {

        PlayerResponses [][] responses; 
        int nPlayer;

        public BetHistory(int totalPlayers) {
            super();
            responses = new PlayerResponses[4][totalPlayers];
            for(int i=0;i<totalPlayers;i++) {
                            responses[0][i] = new PlayerResponses();
                            responses[1][i] = new PlayerResponses();
                            responses[2][i] = new PlayerResponses();
                            responses[3][i] = new PlayerResponses();
                    }
            nPlayer = totalPlayers;

            }
        public void addResponse(int response, int playerNo, int roundNo)
        {
            responses[roundNo][playerNo].response.add(response); // DUE TO HERE
        }


    }
公共A类{ 公共静态void main(字符串[]args){ BethHistory testObject=新的BethHistory(6); testObject.addResponse(2,1,0);//此处出错 } } 类PlayerResponses{ 公共链接列表响应; 公共玩家响应(){ 超级(); response=newlinkedlist(); } } 阶级历史{ PlayerResponses[][]响应; int N层; 公共历史(整版){ 超级(); 响应=新玩家响应[4][totalPlayers];
对于(inti=0;我可以发布堆栈跟踪吗?调用
addResponse()
2,1,0
)时,该特定数据是否会出现问题?我不认为填鸭式(特别是在没有解释的情况下)是解决方法。
import java.util.*;

    public class A {
    public static void main(String[] args) {

            BetHistory testObject = new BetHistory(6);

            testObject.addResponse(2, 1, 0); //  ERROR HERE

    }
    }

    class PlayerResponses {

        public LinkedList<Integer> response;

        public PlayerResponses() {
            super();
            response = new LinkedList<Integer>();
        }


    }

    class BetHistory {

        PlayerResponses [][] responses; 
        int nPlayer;

        public BetHistory(int totalPlayers) {
            super();
            responses = new PlayerResponses[4][totalPlayers];
            for(int i=0;i<totalPlayers;i++) {
                            responses[0][i] = new PlayerResponses();
                            responses[1][i] = new PlayerResponses();
                            responses[2][i] = new PlayerResponses();
                            responses[3][i] = new PlayerResponses();
                    }
            nPlayer = totalPlayers;

            }
        public void addResponse(int response, int playerNo, int roundNo)
        {
            responses[roundNo][playerNo].response.add(response); // DUE TO HERE
        }


    }