Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException or not

 
Janet Smith
Greenhorn
Posts: 6
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm currently working my way through the tests on the CD that comes with the OCA/OCP Java SE7 Programmer I & II Study guide. I got a question that contained the following code and was asked to predict the outcome:



I got this question wrong because I thought I would get a NullPointerException as the first for loop populates elements 1 and 2 of the array, but the second for loop prints all elements, including the one at position 0, which the code never populated.

The correct answer is that the output is "null b c"

I looked through the book and chapter 5 says "If you actually try to use that null reference by, say applying the dot operator to invoke a method on it, you'll get the infamous NullPointerException"

So I can do a System.out.print on sb[0] which will print "null", but if I tried to call sb[0].toLowerCase() I would get a NullPointerException?

I was wondering if someone could explain a little about this? I had it in my head that any reference to sb[0] would throw a NullPointerException so I'm interested to know what will and what won't throw it.
 
Henry Wong
author
Marshal
Pie
Posts: 22110
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janet Smith wrote:
I was wondering if someone could explain a little about this? I had it in my head that any reference to sb[0] would throw a NullPointerException so I'm interested to know what will and what won't throw it.


If you dereference a null reference, meaning use it to access an instance variable or call an instance method, you will get a NullPointerException exception. It is perfectly fine to pass a null reference around. It is also fine to use a null reference to access static variable or static methods. In the case of the println() example, you simply passed the reference to the method (which is fine) -- and luckily, that method has code that checks to see if the reference is null, and if so, replaces it will a string that is set to "null".

Henry
 
Winston Gutkowski
Bartender
Pie
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:and luckily, that method has code that checks to see if the reference is null, and if so, replaces it will a string that is set to "null".

@Janet: Specifically, println() uses String.valueOf(Object) to determine what string to print out, and if you look at the docs for that method, you'll read precisely the behaviour you're seeing.

HIH

Winston
 
Janet Smith
Greenhorn
Posts: 6
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks both of you for those explanations.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:@Janet: Specifically, println() uses String.valueOf(Object) to determine what string to print out, and if you look at the docs for that method, you'll read precisely the behaviour you're seeing.

I have to nitpick a little bit about that statement This would have been 100% accurate if the line of code at line12 would have beenIn this case the overloaded version of the print() method taking an Object parameter would be executed. And the implementation of this method uses indeed String.valueOf(Object) to create a string representation of the Object parameter. And that method will (as Henry mentioned) check for null; it returns "null" in that case, object.toString() otherwise (that illustrates why the null check is so important). So if you would use the statement System.out.print(s); (on line12) in the above code snippet, the output will be "nullbc".

But the above code snippet uses a different statementIn this case concatenating s with " " happens first and then the result (a String object) will be passed to the overloaded version of the print() method taking an Object parameter. So now a new question arises: why does the concatenation s + " " not throw a NullPointerException? Luckily the answer is very simple (and already discussed). The same method String.valueOf(Object) is used here as well. So s + " " is executed as String.valueOf(s) + " ". And that's why no NPE will be thrown. So if you would use the statement System.out.print(s); (on line12) in the above code snippet, the output will be "null b c ".

And here is a final note. You know two different ways to concatenate strings: either use the concatenation operator + or use the concat(String) method of the String class. Although both ways will concatenate, there are a few important differences:
  • if a is null, then a.concat(b) throws a NullPointerException but a + b will not; so if you would use the statement System.out.print(s.concat(" ")); (on line12) in the above code snippet, a NullPointerException will be thrown
  • if b is null, then b.concat(a) throws a NullPointerException but a + b will not; so if you would use the statement System.out.print(" ".concat(s)); (on line12) in the above code snippet, a NullPointerException will be thrown; using statement System.out.print(" " + s); (on line12) in the above code snippet will result in the output " null b c"
  • the concat() method only accepts String values while with the + operator you can append all other types (primitive data types and reference types) as well; using statement System.out.print(s.concat(1)); (on line12) in the above code snippet results in a compiler error; if you would use the statement System.out.print(s + 1); (on line12) in the above code snippet, the output will be "null1b1c1"


  • Hope it helps!
    Kind regards,
    Roel
     
    Winston Gutkowski
    Bartender
    Pie
    Posts: 10571
    64
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Roel De Nijs wrote:So now a new question arises: why does the concatenation s + " " not throw a NullPointerException?...

    Good point, and nice explanation. Have a cow.

    Winston
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic