3

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!

4
  • You set arr to null. What else could possibly happen when you try to access an element of arr? Commented Nov 11, 2011 at 4:51
  • 2
    Should I downvote this question? >:) Commented Nov 11, 2011 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. Commented Nov 11, 2011 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. Commented Nov 11, 2011 at 5:19

3 Answers 3

16

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.

5
  • 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? Commented Nov 11, 2011 at 4:54
  • no, it has nothing to do with changable or not, you just can not put something into a null. Commented Nov 11, 2011 at 5:00
  • 1
    Picture is worth a thousand words +1 Commented Nov 11, 2011 at 5:12
  • 1
    That IS a nice picture -- should be copyrighted or something. Thanks! Commented Nov 11, 2011 at 15:11
  • @Pregnantmom You are welcome. Don't forget to accept this answer if you find it solves your problrm :) Commented Nov 11, 2011 at 18:13
1

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

0

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

2
  • arr[0] is not null. I'm printing arr[0]. Why do you think arr[0] is null? Commented Nov 11, 2011 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. Commented Nov 11, 2011 at 4:56

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.