• 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

null confusion

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all im really confused about the 'null' concept .. does any1 kno a good article which tells me all theres to know about null??? please let me know anything you know about null and specially cases which lead to a nullpointerexception
cheers
nik
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Null in Java isn't really that complicated, unlike nulls in a relational database. Basically null is the abscence of an object. If you have an object reference that does not point to an object, it is null. As for NullPointerExceptions, they occur when you attempt to call a method using a null object reference. For example:

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A variable of reference type -- i.e., a variable that refers to any kind of object -- is like a dog leash. It can have a dog on the end of it, or it can have no dog. It can be attached to a dog sometimes, and no dog at other times -- or different dogs over time. Whether it has a dog or not, it's the same leash. "null" means "no dog." It's like an empty leash.

You get a NullPointerException any time you write (or some other code writes) "x.anything" if x is null. The only exception is if "anything" is a static method or variable, because in that case the value of "x" is actually not needed.

This is like what happens if you try to walk, feed, or pet the non-existent dog at the end of an empty leash.

You can read a longer discussion that uses a different metaphor here. That story talks about other kinds of Java variables too.
 
nik rb
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx that solved a lot of stuff... i guess just one further question will a reference become null only if it is assignedthe value null explicitly as in

strInst=nulll;

or are there other ways that makes refrences null???

to reframe the question is the dog free only if i unleash it or there are other elements that may help break the shackles ??
"who let the dogs out.........."
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are other ways where you can end up with a null object that could cause you to get a NullPointerException. The most common one I've seen is where you do a query on a database where no record is retrieved. Think of this as calling a dog kennel and ask for the wrong dog or vise-versa. The dog may exist but you asked for it the wrong way. Now, if you ASSUME you have retrieved the right record, or any record for that matter and try to do something like .getName(), this will throw your nullpointerexception. The best bet is to check to make sure you have gotten at least one record before trying to manipulate the query. Think of this as making sure you have a dog on the leash before you go walking it in the park.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Raistlin points out, the null in the assignment does not have to be explicit. There are times when a method will return a null value. If you assign this return value to a reference variable, then the reference will be null. This is why it is important to check the documentation for any method that returns a reference. You need to be aware whether or not the method will return null.

However, notice that this still involves an explicit assignment, even if the null value is not so obvious. There is one place where an explicit assignment is not necessary: member variables. All member variables are initialized to a default value if you do not explicitly initialize them yourself. For reference variables, the default is null.

To avoid any problems this may cause, it is a good practice to explicitly initialize all class variables yourself. Even if you explicitly initialize it to null, then you are aware of what its value is.

Layne
reply
    Bookmark Topic Watch Topic
  • New Topic