• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException in Java  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.lang.NullPointerException is a unchecked exception. It extends the RuntimeException. By default when you create the reference variable, it will point to null i.e, it has no values (A skeleton)

What is Null?
It can be setted to any reference object. It simply means a reference to nothing. i.e, once a reference variable sets to null, it means the variable indicate that it does not refer to any object or array.

Why it is null pointer (not null reference)?
Let us explain it with below statement easily.

So the first statement executes like this. First the str string is created in heap and then obj (a reference variable) is pointing to that string.
Now when you assign null to it, the reference to string goes away. Now it is obviously pointing to null. (i.e, pointing nothing). In this case, if you call
obj.toCharArray() which means you are trying to access the obj (dereferencing the obj) which pointing nothing. so it throws null pointer exception. Hence, when we access the reference, we are dereferencing it.

Why it is unchecked exception?
A reference variable can change its state and values at Run time. It can not be checked during compile time. So it is unchecked. Hence it does not need to get catch explicitly.

Causes of NullPointerException:
1. Calling a instance method of a reference object which points to null Example:

2. Instance field of a reference object is accessed or modified

In this, Human human = null;
human.name = "John" is accessing the instance of null reference
3. If the array is null, we should avoid taking length of it
String[] ar = null;
ar.length should be avoided.
4. After creating array of objects, accessing the individual objects;


5. when the array is pointing to null, accessing or modifying its values
ar[0] = "John";
6. Declaring the reference variable.
Human human;
Here, human is pointing to nothing. Not pointing to any object
7. Throwing null as if it were a throwable value (this cas will happen rarely)

Finding the root cause null pointer exception:
Finding the exception is far easier than it looks. The exception trace will show the line of exception. We can see which reference variable's instances or array's values are accessed (i.e, reference used before . ex: human.name In this, human could be the cause of the exception). That particular variable could makes the exception.

How to avoid the NullPointerException:
By following simple steps, we can easily avoid getting this exception. Whenever we use the reference variable's values, we should do the null check as follows:
1. Do null check before accessing the instances of reference variable


2. While accessing array, do null check for array.

3. Be cautious while accessing the array of objects
Human[] humen = new Human[10];
Note that, a pool of objects have been created in the heap. But individual object has to be created i.e.,

4. Avoid nesting the method calls
human.getName().toCharArray();
It first gets the name and converts that to character array. If human is null, first will make the exception. Another possibility is if name is null, the next access will create NullPointerException. So avoid using like this.

 
Ranch Hand
Posts: 159
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have any questions about NullPointerExceptions?
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Do you have a blog or something; we have a forum dedicated to blogging.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Do you have a blog or something; we have a forum dedicated to blogging.

@santhoshkumar: And if you do, here's a contentious post to kick it off:

Java program's shouldn't throw NullPointerExceptions (NPEs) explicitly. EVER.

It's only my opinion, but to me an NPE is an effect, not a cause; and it should only ever be thrown by the JVM to indicate a bug in a program - and if I was re-writing the language, I would never have made it visible to developers, except as a protected class written by the designers.

That might have provided more reason for people to explain how they can be avoided, rather than encountered.

Quite apart from that is the fact that Java isn't supposed to have "pointers", is it? How about a NullObjectException for the proletariat?

My stir, for the start of the week.

Winston
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Quite apart from that is the fact that Java isn't supposed to have "pointers", is it? How about a NullObjectException for the proletariat?


Nonono... that would be NullReferenceException.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't that what they call NPEs in C#?

Are you suggesting, Winston, that one should always write throw new IllegalArgumentException("No nulls permitted"); instead of using NPEs? That particular point has come up several times before, and we have never got a consensus. I still prefer to use NPEs if a null is passed inappropriately, but isn't that a minor style point?

And there is the third school of thought.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago. I wrote: . . . the third school of thought.
“Who care what sort of Exception you throw as long as you throw it hard enough?”
 
Ranch Foreman
Posts: 3068
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That particular point has come up several times before, and we have never got a consensus.


Getting a consensus on certain topics in IT is a bit like herding cats.
An exercise in futility.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Are you suggesting, Winston, that one should always write throw new IllegalArgumentException("No nulls permitted");

Pretty much; although, as I say, it's simply my opinion.

Why?
1. If a program of mine throws an NPE, I can be darn sure I didn't throw it.
2. It says to the reader (or client): "this is something I specifically checked for, and you got it WRONG".
3. it gets you into the habit of checking for NPEs before (or when) they happen.

Of course, there are exceptions (there are always exceptions ). And for them I have two other rules (which should probably actually be 1 and 2):
4. Fail fast.
5. Fail LOUD.

So, if you have an object that shouldn't be null, the quickest way to make sure is to call something on it, eg:
  Iterator i = passedList.iterator();
And if that's the first line in a method, then it'll be pretty obvious what the cause was if you get an NPE.

So perhaps I should restate my rule as:
Either:
(a) Force the JVM to throw an NPE immediately.
or, (if you can't do that)
(b) Check for null explicitly and throw some other Exception to indicate the problem.

But whatever you do, DON'T just leave it to become an effect.

My 2¢.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!