-2

A java.lang.NullPointerException occurs when a Java application or Java applet has been badly coded. Typically, the Java program (and consequently, the programmer) attempted to access the reference or handle to a Java object that did not exist

I have gone round in circles reading . I have made two minimal files - Two.java will compile.

 public class Two {
   public static int width;
   public static int height;

   public static void main(String[] args) {
     int width = 320;
     int height = 100;  
     System.out.println(width + "," + height);
   }
 }

One.java in eclipse will print the string intwo to console. My question is if it will print, Why is it null? I am trying to convert the string to int so I can do math with it. In real life there is a lot more than two numbers arriving in.

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class One {
  public static int width;
  public static int height;

  public static void main(String[] args) throws IOException, InterruptedException {
    Process p = Runtime.getRuntime().exec("java -jar Two.jar");
    BufferedReader is;
    String intwo;
    is = new BufferedReader(new InputStreamReader(p.getInputStream()));

    while ((intwo = is.readLine()) != null)
      System.out.println(intwo); // Prints to console

    String[] items = (intwo).split(","); //java.langNullPointerException
    int[] results = new int[items.length];

    for (int i = 0; i < items.length; i++) {
      results[i] = Integer.parseInt(items[i]);
    }
    System.out.println(results[1]);
  }
}
Qwerky
  • 18,217
  • 6
  • 44
  • 80

5 Answers5

2
while ((intwo = is.readLine()) != null)
  System.out.println(intwo); // Prints to console

//Here intwo is guaranteed to be null!!!!
String[] items = (intwo).split(",");

By the way, it is a bad practice to write if/while statements without curly braces. You should really avoid it.

Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
1

Here's your while clause:

while ((intwo = is.readLine()) != null)
  System.out.println(intwo); // Prints to console

It exists when intwo is null, right? So it's obvious that on the next line intwo will be null and will cause a NullPointerException:

String[] items = (intwo).split(","); //java.langNullPointerException
Egor
  • 39,695
  • 10
  • 113
  • 130
1

See your code:

while ((intwo = is.readLine()) != null)

When do you exit the while loop? When intwo is null!

So when you exit the loop, you're doing null.split(","); which of course causes NPE.

Put parenthesis around the while loop and you should be fine.

Maroun
  • 94,125
  • 30
  • 188
  • 241
1
while ((intwo = is.readLine()) != null)
    System.out.println(intwo); // Prints to console
String[] items = (intwo).split(","); //java.langNullPointerException

Of course you get NullPointerException. You basically iterate until intwo is null (so you ensure intwo is null) and then you call a method on that null reference.

Sethiel
  • 182
  • 6
  • 14
0

This:

 while ((intwo = is.readLine()) != null)
 System.out.println(intwo); // Prints to console
 String[] items = (intwo).split(","); 

is the same as this:

while ((intwo = is.readLine()) != null){
    System.out.println(intwo); // Prints to console
}

String[] items = (intwo).split(","); 

It gets pretty obvious why you get a NullPointerException

Eugene
  • 117,005
  • 15
  • 201
  • 306