up vote 1 down vote favorite
share [g+] share [fb]

This problem came up in a practice test: create a new string array, initialize it to null, then initializing the first element and printing it. Why does this result in a null pointer exception? Why doesn't it print "one"? Is it something to do with string immutability?

public static void main(String args[]) {
        try {
            String arr[] = new String[10];
            arr = null;
            arr[0] = "one";
            System.out.print(arr[0]);
        } catch(NullPointerException nex) { 
            System.out.print("null pointer exception"); 
        } catch(Exception ex) {
            System.out.print("exception");
        }
    }

Thanks!

link|improve this question

25% accept rate
You set arr to null. What else could possibly happen when you try to access an element of arr? – NullUserException Nov 11 '11 at 4:51
1  
Should I downvote this question? >:) – Hendra Jaya Nov 11 '11 at 5:06
arr is a String[] variable. If you set arr to null then you are setting the String[] to null -- the initial new String[10] is gone. Then setting arr[0] tries to use a null array reference. You could set arr[0] to be null and then set it to be "one" but not the arr array. – Gray Nov 11 '11 at 5:14
Think of it as null[0] = "one". arr is no longer referencing ("pointing at", sort of) the space that could hold up to ten Strings. – Dave Newton Nov 11 '11 at 5:19
feedback

3 Answers

Because you made arr referring to null, so it threw a NullPointerException.


EDIT:

Let me explain it via figures:

After this line:

String arr[] = new String[10];

10 places will be reserved in the heap for the array arr:

enter image description here

and after this line:

arr = null;

You are removing the reference to the array and make it refer to null:

enter image description here

So when you call this line:

arr[0] = "one";

A NullPointerException will be thrown.

link|improve this answer
I know I set arr reference to null, but then I changed it. Are you saying once the arr reference is null it can never be changed? – Pregnant mom Nov 11 '11 at 4:54
@MichaelMotherway see the update. – Eng.Fouad Nov 11 '11 at 5:00
1  
Picture is worth a thousand words +1 – Tarun Nov 11 '11 at 5:12
2  
Nice picture! +1 – dann.dev Nov 11 '11 at 5:29
1  
That IS a nice picture -- should be copyrighted or something. Thanks! – Pregnant mom Nov 11 '11 at 15:11
show 2 more comments
feedback

arr is null, and then... if it did print one, wouldn't you be surprised? put a string into a null?

link|improve this answer
arr[0] is not null. I'm printing arr[0]. Why do you think arr[0] is null? – Pregnant mom Nov 11 '11 at 4:55
arr is null, your exception is thrown from arr[0] = "one"; NOT System.out.print(arr[0]); you can not put something into a null. – James.Xu Nov 11 '11 at 4:56
feedback

With arr = null; you are deleting the reference to the object. So you can't access it anymore with arr[anynumber] .

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.