0

Why am I getting a nullpointer error at the second if statement below?

I'm checking in that if statement if the array string scores is empty or not, but it always crashes saying nullpointer exception. I also added the LogCat if that helps.

String[] scores;
String times = "";

public void Read() {
    FileInputStream inputStream = null;

    try {

        inputStream = openFileInput("timesfile");

        byte[] reader = new byte[inputStream.available()];

        while (inputStream.read(reader) != -1) {
        }
        times = new String(reader);

    } catch (IOException e) {
        e.printStackTrace();

    } finally {
        if (inputStream != null) {
            try {
                    inputStream.close();
            } catch (IOException e) {
                    e.printStackTrace();
            }

        }

    }

    if (!times.isEmpty() && times != null && times != ""
                    && times.length() != 0) 
    {
        scores = new String(times).split(",");
        Arrays.sort(scores);
    }
    if (scores.length != 0 && scores != null && scores.length >= 4) { //I get a nullpointer exception here!

        better = false;
        if (Integer.parseInt(endTime.split(":")[0]) > Integer
                        .parseInt(scores[0].split(":")[0])) {
            better = true;
        } else if (Integer.parseInt(endTime.split(":")[1]) > Integer
                        .parseInt(scores[0].split(":")[1])) {
            better = true;
        } else if (Integer.parseInt(endTime.split(":")[2]) > Integer
                        .parseInt(scores[0].split(":")[2])) {
            better = true;
        } else {
            better = false;
        }

    }

}

LogCat:

11-16 22:32:21.195: E/AndroidRuntime(28584): FATAL EXCEPTION: Thread-2278
11-16 22:32:21.195: E/AndroidRuntime(28584): java.lang.NullPointerException
11-16 22:32:21.195: E/AndroidRuntime(28584):    at com.example.app.TestActivity$TestView.scoreRead(TestActivity.java:426)
11-16 22:32:21.195: E/AndroidRuntime(28584):    at com.example.app.TestActivity$TestView.over(TestActivity.java:303)
11-16 22:32:21.195: E/AndroidRuntime(28584):    at com.example.app.TestActivity$TestView.run(TestActivity.java:267)
11-16 22:32:21.195: E/AndroidRuntime(28584):    at java.lang.Thread.run(Thread.java:856)
4
  • 1
    try switching the order to if (times != null && !times.isEmpty() && times != "" Commented Nov 16, 2012 at 21:41
  • This is line 426: if (scores.length != 0 && scores != null && scores.length >= 4) { Commented Nov 16, 2012 at 21:42
  • 1
    always check for a null object before any properties on the object Commented Nov 16, 2012 at 21:43
  • You can just use if(!TextUtils.isEmpty(times)) instead Commented Nov 16, 2012 at 21:46

7 Answers 7

3

can you try with keeping the times != null in the left most. The order of operation is from left to right.

if (times != null && !times.isEmpty() && times != "" && times.length() != 0)

EDIT: and yes @Simon is right.. times != "" should be !times.equal("")

But anyway you are already checking !times.isEmpty() so you don't need that piece at all.

This should do:

if (times != null && !times.isEmpty() )

Sign up to request clarification or add additional context in comments.

Comments

1

do if(times!= null) first

then nested in that do !times.isEmpty()

isEmpty() checks to see if the length of the string is zero or not. Not if it is null or not. length of 0 is "", not null

the same principle applies if it is another string that is being checked

1 Comment

^ This. You can just use if(!TextUtils.isEmpty(times)) instead
1

Replace

if (scores.length != 0 && scores != null && scores.length >= 4) {

with

if (scores != null && scores.length >= 4) {

Replace

if (!times.isEmpty() && times != null && times != "" && times.length() != 0) 

with

if (times != null && !times.isEmpty()) 

i.e. put null checks at front of condition and remove unnecessary guards.

Comments

0

This is not a safe check.

times != ""

The equality operator for String tests whether the 2 Strings are the same object. Instead, use

!times.equal("")

!= might work some times is "" because Java "interns" strings and by chance, they might be the same object but you should not rely on that.

Comments

0

Your question and your comment don't match up, but regardless of whether you are checking scores or times you must check if they are null first. (Otherwise you will see the NPE.)

Use:

if (times != null && !times.isEmpty() && times != "" && times.length() != 0)

And:

if (scores != null && scores.length != 0 && scores.length >= 4) {

Why am i getting a nullpointer error at the second if statement below?

A NullPointerException occurs when you try to call a method on variable that is null. So if scores is null, this code scores.length is trying to call null.doSomething(). You cannot ask nothing to do something.

Comments

0

actually your scores array is initialized only if

!times.isEmpty() && times != null && times != "" && times.length() != 0

is true. It happened so that it is not true :). And there is no sense in invoking method on times and than compare it for null. However no NPE is thown here, so let's omit that.

!times.isEmpty is the same as times.legth != 0;

times != "" is always true as soon as it is initialized with 'new' operaton and is not interned.

So all the condition is equal to (time is not null as you can see)

!times.isEmpty

Anyway you should check on null before the other checks that involves method invocation on checked object.

Comments

0

The variable times not is null. She was not initialized with null value but empty value.

The NullPointerException can be found in the row 426 in TestActivity class. I think that row is:

if (scores.length != 0 && scores != null && scores.length >= 4) {

So, if you look better into your code, the scores variables is null because the condition above is wrong.

Change your code for this:

if (!times.isEmpty()) 
{
     scores = times.split(",");
     Arrays.sort(scores);
}
if (scores != null && scores.length > 0) {

   ...

}
else {

   System.err.println("There isn't score");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.