简体   繁体   English

NullPointerException 和 BufferedReader

[英]NullPointerException and BufferedReader

my program keeps throwing nullPointerException at me and I have no idea why.我的程序不断向我抛出 nullPointerException ,我不知道为什么。 I thought maybe it's because of the bufferedReader but I'm not sure.我想可能是因为 bufferedReader 但我不确定。

String line = reader.readLine();
while (!line.isEmpty()) {
    line = repairLine(line);
    tree.add(line);
    line = reader.readLine();
}

Yes, it is wrapped in try - catch block.是的,它被包裹在 try-catch 块中。 It says that the problem is on the while-line.它说问题出在 while 线上。 It didn't work with "if (line != null)".它不适用于“if (line != null)”。 I really don't know what could cause that.我真的不知道是什么原因造成的。 Thanks you for any help.谢谢你的任何帮助。

The problem seems to be with the following lines of your code :问题似乎出在以下几行代码中:

String line = reader.readLine();
while (!line.isEmpty()) {   

}

If there is nothing to read from the file, line will be null.如果没有要从文件中读取的内容,则 line 将为空。 Thus the exception in the while loop.因此,while 循环中的异常。 You are trying to call a method on a null reference.您正在尝试对空引用调用方法。

That being said, the traditional way of reading lines from a file is to assign the line read to a variable in the while condition itself (code not tested) :话虽如此,从文件中读取行的传统方法是将读取的行分配给 while 条件本身中的变量(代码未测试):

String line = "";
while ((line=reader.readLine()!=null) && !line.isEmpty()) {
    line = repairLine(line);
    tree.add(line);
}

I have had this same error and it meant that the socket is closing on the other connected client while it is trying to read on the server.我遇到了同样的错误,这意味着套接字在其他连接的客户端上尝试在服务器上读取时正在关闭。

To add some context, I am running a server and various clients are connecting to it via java sockets.为了添加一些上下文,我正在运行一个服务器,并且各种客户端通过 java 套接字连接到它。 I was having this error on the server side when the client closes the socket.当客户端关闭套接字时,我在服务器端遇到此错误。

So to solve the issue I decided to close both server side and client side sockets after every connection.所以为了解决这个问题,我决定在每次连接后关闭服务器端和客户端套接字。 The way I have done this is before client is closed it sends a "quit" message to the server before closing its sockets.我这样做的方法是在客户端关闭之前,它在关闭套接字之前向服务器发送“退出”消息。 When the server reads the "quit" message it will also close its sockets.当服务器读取“退出”消息时,它也会关闭它的套接字。

For example:例如:

  • Client side客户端
if(Quit){
   if(mySocket != null && !mySocket.isClosed()) {
        output.println("quit");
        output.flush();
        
        try {
            mySocket.close();
        } catch (IOException e) {
            System.out.println("Close net variables exception - " + e.getMessage());
        }
    }
}
  • Server side服务器端
String line = input.readLine(); //Thread stops here untill it reads something

if(line.equals("something")){
   do_something();
else if(line.equals("quit")){
   try {
        mySocket.close();
    } catch (IOException e) {
        System.out.println("Close net variables exception - " + e.getMessage());
    }
else{
   do_default();
}

Worked for me, hope it helps!为我工作,希望它有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 BufferedReader中的NullPointerException - NullPointerException in BufferedReader Java:BufferedReader和NullPointerException - Java: BufferedReader and NullPointerException 如何在 Android 上的 Bufferedreader 中修复 NullPointerException? - How to fix NullPointerException in Bufferedreader on Android? 使用ObjectInputStream而不是BufferedReader时获取NullPointerException - Get NullPointerException when using ObjectInputStream instead of BufferedReader NullPointerException,虽然它不为空,但读取BufferedReader的内容时 - NullPointerException while reading content of BufferedReader although it's not empty 从BufferedReader读取时发生NullPointerException,仅当以jar运行时 - NullPointerException when reading from BufferedReader, only when ran as a jar 使用 BufferedReader 从抛出 NullPointerException 的二维数组中获取值 - Using BufferedReader to grab values from a 2D array throwing NullPointerException 在while循环中使用bufferedreader时获取NullPointerException - Getting NullPointerException while using bufferedreader inside while loop BufferedReader / AsynchTask- java.lang.NullPointerException:锁== null - BufferedReader / AsynchTask- java.lang.NullPointerException: lock == null 使用BufferedReader(readLine())检查更多值时如何停止NullPointerException - How to stop a NullPointerException when using BufferedReader (readLine()) to check for any more values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM