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]);
}
}