0

I have problem with reading from my socket i read only if value isn't null but it doesnt't works.

@Override
public void run() {
    System.out.println("Reading from socket");
    while(true){
        try {
            if(!(br.readLine().equals(null)))read += br.readLine();
        } catch (IOException e) {
            System.out.println("error " + e);
        }
    }
}

here is error:

Exception in thread "Thread-4" java.lang.NullPointerException at connection.CreateConnection.run(CreateConnection.java:61) at java.lang.Thread.run(Unknown Source)

g_1_k
  • 484
  • 1
  • 6
  • 13

3 Answers3

8

If br.readLine() returns null, then calling .equals(null) on it will throw an exception - it won't return true. You just want to compare reference identity with null.

Calling .equals(null) is never useful, unless you're testing that your equals implementation works properly :)

Additionally, you'll be skipping every other line by calling readLine() twice on each loop iteration.

You want something like:

String line;
if ((line = br.readLine()) != null) {
    read += line;
}

... except that will be painfully slow due to repeated string concatenation. You should almost certainly be using a StringBuilder instead.

Also, doing all of this in a loop which catches IOException seems like a recipe for disaster - if the a call fails, it's very likely that it'll keep failing forever, whereupon your program is basically hung in a tight loop. You should almost certainly stop when you get an exception, rather than keeping going. For example:

try {
    String line;
    while ((line = reader.readLine()) != null) {
        read += line; // Or builder.append(line);
    }
} catch (IOException e) {
    // Whatever you want to do
}

Finally, consider the value of whitespace, both horizontal and vertical, as well as the benefits of using braces even in single-statement if statements etc. The line if(!(br.readLine().equals(null)))read += br.readLine(); is compact at the expense of readability.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • if you use `if ((line = br.readLine()) != null)` PMD will complain about **assignments in operands** – BanzaiTokyo Mar 29 '17 at 16:08
  • @BanzaiTokyo: Well, that's a tool - it's meant to be working for you. If you don't like that style, don't use it... but if it were me, I'd try to tweak the tool to ignore common patterns like this. – Jon Skeet Mar 29 '17 at 16:58
0

Say you are on the last line of your file, you call br.readLine() to check if it's not null. Your line exists so your test passes. Then you call br.readLine() once again. Unfortunately there is nothing left to read! And you get your exception.

Rewrite your code like that:

@Override
public void run() {
    System.out.println("Reading from socket");
    String line;
    while(true){
        try {
            line = br.readLine()
            if((line != null))read += line;
        } catch (IOException e) {
            System.out.println("error " + e);
        }
    }
}
StephaneM
  • 4,779
  • 1
  • 16
  • 33
  • Your answer seems to imply that the OP's code will work if he manages to successfully read the last line - it won't, due to the broken way of comparing the string with `null`. – Jon Skeet Jun 19 '14 at 14:21
-1

You are calling .equals() on a null object, which causes the null pointer issue I assume. If you want to read with a buffered reader, try doing something like:

        String line = reader.readLine();
        while(line != null){
            System.out.println(line);
            line = reader.readLine();
        }

This will eliminate the issue of null pointers, and will also stop you from skipping any lines during reading.

Mike Elofson
  • 2,017
  • 1
  • 10
  • 16