• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.lang.NullPointerException

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
To make myself clear about java.lang.NullPointer exception I want to know it is thrown:
1. When we assign null to some object reference. And then we invoke some member method on that object. Like
String str = null;
str.concat("123");
The toString() method is an exception in this case. Pls. correct me if I am wrong. And pls. if someone can explain in more detail that under what else conditions it is thrown.
Regards
Muhammad Ali
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ali,
We can say in general that the NullPointerException will be thrown whenever a null object is acted upon. By that I mean that a method or variable of some class is being accessed with a null object.
You are essentially correct, but we don't need to assign an object to null to have a null object, which can later cause the NullPointerException to be thrown.
Regards,
Manfred.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Md
a NullPointerException gets thrown everytime an INSTANCE method is invoked on an null object reference, i.e.

will throw a NullPointerException.
If, however, the method is static like in the following code no Exception is thrown

BUT, in the case of of an instance of String being null, the following code should print "null"

Correct me if I am wrong...
HIH
Val
 
For my next trick, I'll need the help of a tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic