Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中调用ArrayList.get()时获取NullPointerException?_Java_Sockets_Nullpointerexception - Fatal编程技术网

在Java中调用ArrayList.get()时获取NullPointerException?

在Java中调用ArrayList.get()时获取NullPointerException?,java,sockets,nullpointerexception,Java,Sockets,Nullpointerexception,所以我有一个聊天室,它过去可以工作,但我稍微修改了代码来尝试一些东西,但它们不起作用,所以我尝试恢复到我的原始代码,但我不确定我做错了什么,因为现在它抛出了NullPointerException。我的代码有一个PrintWriter的ArrayList和一个showAll()方法,它按照它所说的做;向聊天室中的所有人发送消息。所以基本上我想知道的是为什么我会得到例外 //This is the main code, used to add printwriters to the arr

所以我有一个聊天室,它过去可以工作,但我稍微修改了代码来尝试一些东西,但它们不起作用,所以我尝试恢复到我的原始代码,但我不确定我做错了什么,因为现在它抛出了NullPointerException。我的代码有一个PrintWriter的ArrayList和一个showAll()方法,它按照它所说的做;向聊天室中的所有人发送消息。所以基本上我想知道的是为什么我会得到例外

    //This is the main code, used to add printwriters to the arraylist and to connect to clients
    //The Communicate thread is used to get input from users and use the showAll() method with that input
    public void listen() {
        listWriters = new ArrayList<PrintWriter>();
        try {
            scanner = new Scanner(System.in);
            portnum = getPortNumber(scanner);
            System.out.println("Listening on " + portnum);
            serverSocket = new ServerSocket(portnum);
            while(true) {
                clientcommunicate = serverSocket.accept();
                System.out.println("Connection accepted: " + clientcommunicate.toString());

                PrintWriter client = new PrintWriter(clientcommunicate.getOutputStream(), true);
                listWriters.add(client);
                Thread t = new Thread(new Communicate(clientcommunicate));
                t.start();
            }
        } catch (IOException ioe) {
            System.err.println(ioe);
            System.err.println("Error.");
            System.exit(1);
        }
    }

    //This uses a printwriter obtained in the Communicate thread; the thread initializes a socket in it with the socket obtained in the constructor
    public void showAll(String msg, PrintWriter printwriter) {
        for(int i = 0; i < listWriters.size(); i++) { //this is where the exception is thrown
            if(!listWriters.get(i).equals(printwriter)) { //if I change the paramater listWriters.size() to a regular integer like 3, and only create 2 clients or even less, the exception is thrown here instead
                listWriters.get(i).println(msg);
            }
        }
    }
//这是主代码,用于将PrintWriter添加到arraylist并连接到客户端
//通信线程用于从用户获取输入,并使用showAll()方法处理该输入
公共图书馆{
listWriters=新的ArrayList();
试一试{
扫描仪=新扫描仪(System.in);
portnum=getPortNumber(扫描仪);
System.out.println(“监听”+portnum);
serverSocket=新的serverSocket(portnum);
while(true){
clientcommunicate=serverSocket.accept();
System.out.println(“已接受连接:+clientcommunicate.toString());
PrintWriter客户端=新的PrintWriter(clientCommunication.getOutputStream(),true);
添加(客户端);
线程t=新线程(新通信(客户端通信));
t、 start();
}
}捕获(ioe异常ioe){
系统错误打印项次(ioe);
System.err.println(“Error”);
系统出口(1);
}
}
//这将使用在通信线程中获得的printwriter;线程使用构造函数中获得的套接字初始化其中的套接字
public void showAll(字符串msg,PrintWriter PrintWriter){
对于(int i=0;i
编辑:


好吧,我不再收到错误了,但现在我似乎无法发送消息。如果我从客户端发送消息,则没有错误,但消息不会显示在任何客户端上

由于试图取消对
null
变量的引用(即对其调用方法或从中读取字段),因此引发了一个
NullPointerException

在这种情况下,
listWriters
显然为空(因为它是发生异常的行上唯一取消引用的变量-查找
字符)。由于这是在您的
listen()
方法中分配的,因此如果在调用
listen()
之前调用
showAll()
,我猜您会遇到此错误

一个非常简单的字段是将
listWriters
分配给其声明中的空列表,这样它就永远不能为空:

private List<PrintWriter> listWriters = new ArrayList<PrintWriter>();
private List listWriters=new ArrayList();

根据应用程序的并发性要求,您可能需要做一些更详细的工作,不过一般原则是,您必须在尝试从中读取之前初始化
列表编写器

您将得到一个
NullPointerException
,因为您试图取消引用(即,打开调用方法或从中读取字段),变量为
null

在这种情况下,
listWriters
显然是空的(因为它是发生异常的行上唯一被取消引用的变量-查找
字符)。由于这是在
listen()
方法中分配的,因此如果调用
showAll(),我猜您会遇到此错误
在调用
listen()
之前

一个非常简单的字段是将
listWriters
分配给其声明中的空列表,这样它就永远不能为空:

private List<PrintWriter> listWriters = new ArrayList<PrintWriter>();
private List listWriters=new ArrayList();

根据应用程序的并发性要求,您可能需要做一些更详细的工作,不过一般原则是,您必须在尝试从中读取之前初始化
listWriters

您必须在
侦听
方法之前调用
showAll
方法。
showAll
方法之前,您的
listWriter
没有在
listen
方法中初始化。

您必须在
listen
方法之前调用
showAll
方法。
showAll
方法之前,您的
listWriter
没有在
listen
方法中初始化。

这可能是因为您已在
listen()方法中初始化了
listWriter

因此,如果您的
showAll()
方法在
listen()
方法之前被调用,它将为您的
listWriters
获取一个
null
值(假设您已将
listWriters
声明为实例变量,并且没有在那里初始化)

您可以在声明它的位置尝试初始化它

private List<PrintWriter> listWriters = new ArrayList<PrintWriter>();
private List listWriters=new ArrayList();

这可能是因为您已经在
listen()方法中初始化了
listWriter

因此,如果您的
showAll()
方法在
listen()
方法之前被调用,它将为您的
listWriters
获取一个
null
值(假设您已将
listWriters
声明为实例变量,并且没有在那里初始化)

您可以在声明它的位置尝试初始化它

private List<PrintWriter> listWriters = new ArrayList<PrintWriter>();
private List listWriters=new ArrayList();
listen()