• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

NullPointerException or not

 
Greenhorn
Posts: 6
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks both of you for those explanations.
 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    reply
      Bookmark Topic Watch Topic
    • New Topic