learnerX
learnerX

Reputation: 1082

Java Split() function giving Null pointer Exception error

public class url {


 public static void main(String[] args) throws Exception {

    URL imdb = new URL("http://www.omdbapi.com/?t=The%20incredible%20Hulk");

    URLConnection yc = imdb.openConnection();

    BufferedReader in = new BufferedReader(new InputStreamReader(
                                yc.getInputStream()));
    String inputLine;

    while ((inputLine = in.readLine()) != null) 
        System.out.println(inputLine);

    String[] strarr = inputLine.split(",");


    System.out.println("splits.size: " + strarr.length);

    in.close();
}
}

The Error is : "Exception in thread "main" java.lang.NullPointerException at imdb.url.main(url.java:25)"

Please explain to me to how I can remove it. I have googled a lot on using split function and null pointer exception, but just can't understand what I'm doing wrong.

Upvotes: 0

Views: 4535

Answers (5)

Vishal K
Vishal K

Reputation: 13066

This is because when inputline read last time in While loop nothing is read in inputLine . So after while loop when you trying to split the inputlineit is giving NullPointerExcpetion.

while ((inputLine = in.readLine()) != null) 
        System.out.println(inputLine);
System.out.println(inputLine);//InputLine is null here.
String[] strarr = inputLine.split(",");//throwing NPE

The workaround is to use StringBuilder as follows:

StringBuilder sbuilder = new StringBuilder();
while ((inputLine = in.readLine()) != null) 
            sbuilder.append(inputLine);
String[] strarr = (sbuilder.toString()).split(",");

Upvotes: 1

thejh
thejh

Reputation: 45578

When String[] strarr = inputLine.split(","); is reached, you have read the whole response – but inputLine is null at that point, so you can't split it.

How to fix this depends on what you are trying to do. If you want to split the whole document, try collecting all the data in one big string:

String inputLine;
String allData = "";

while ((inputLine = in.readLine()) != null) {
    System.out.println(inputLine);
    allData += inputLine+"\n";
}

String[] strarr = allData.split(",");

(This code is a bit inefficient, but it should work.)

However, your URL points to a JSON document, so you might want to use a JSON parser instead of doing this. It will be more reliable and probably also easier than trying to parse JSON manually. For information on JSON and JSON libraries for various languages, see http://json.org .

Upvotes: 2

Oren
Oren

Reputation: 4252

you are missing parentheses in your while loop. causing inputLine to be null when you try to split() it. try

 while ((inputLine = in.readLine()) != null) {
    System.out.println(inputLine);

    String[] strarr = inputLine.split(",");

    System.out.println("splits.size: " + strarr.length);
}

Upvotes: 3

Omaha
Omaha

Reputation: 2292

When your while loop exits, inputLine is null, therefore calling inputLine.split() will always produce a NullPointerException. I believe what you want to do is put curly brackets around the System.out.println() calls and everything in between, so that they are all included in the loop iteration:

 while ((inputLine = in.readLine()) != null)
 {
    System.out.println(inputLine);

    String[] strarr = inputLine.split(",");

    System.out.println("splits.size: " + strarr.length);
}

Does this have the intended effect?

Upvotes: 5

Bohemian
Bohemian

Reputation: 425083

If a null is ever returned from in.readLine() in:

 while ((inputLine = in.readLine()) != null) 
    System.out.println(inputLine);

then the loop will exit and inputLine will remain null.

Logically, inputLine will always be null when the loop ends, so the exception in inevitable!

Upvotes: 5

Related Questions