Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 09-11-2009
Member
 
Join Date: Jun 2009
Posts: 83
Could NullPointerException cause some problems

I noticed that using the exception "NullPointerException" could cause problems. But I do not understand why? For example in this line of code:
Code:
if (value == null) throw new NullPointerException("The added value is zero.");
What can it happens? The virtual machine can run?

If I replace NullPointerException by IllegalArgumentException is that this will be better? I was told to replace IllegalArgumentException what difference does it gonna happen?
Reply With Quote
  #2  
Old 09-11-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: Could NullPointerException cause some problems

Hello,

no, there is virtually no difference between these two exceptions. NullPointerException is traditionally thrown when one tries to use a field or method of a null reference. I therefore tend to advise using IllegalArgumentException, but hey, there is very little difference.
Reply With Quote
  #3  
Old 09-11-2009
Member
 
Join Date: Jun 2009
Posts: 83
Re: Could NullPointerException cause some problems

I use a plugin in eclipse "PMD" who corrects my code, and when I weighed exception NullPointerException. I put

Avoid throwing a NullPointerException - it's confusing because most people will assume that the virtual machine threw it. Consider using an IllegalArgumentException instead, this will be clearly seen as a program-initiated exception.

I still see no reason why it is preferable to remove as exception IllegalArgumentException instead of NullPointerException?
Reply With Quote
  #4  
Old 09-11-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
Re: Could NullPointerException cause some problems

The answer seems clear to me:

- A NullPointerException is a technical error, which usually rise when the case (here: add a null value) is not supported. It is a purely technical mistake

- You want to handle this case, so we advise you to use an IllegalArgumentException for your error message that is already more "functional" (versus technical)

Specifically, we agree that it is the same, it's just a "best practice", you make what you want.
Reply With Quote
  #5  
Old 09-11-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: Could NullPointerException cause some problems

Take the example of your item value if you do that then that value is null:
Code:
if ( value. equals ( "" ) ) return ;
your program execution will start automatically NullPointerException to warn that you can not call the equals method on your component value.

Putting
Code:
if (value == null) throw new NullPointerException ("The value added is zero.") ;
has returned the same exception as above but is managed by yourself. PMD advice here is to differentiate an exception that you throw yourself directly because the argument is incorrect with an exception launched by the JVM directly. The purpose is the same (well, depending on the catch of your application) but will in fact seeing someone logs easier to understand the type of problem and especially to understand that you manage the exception and that it is not the JVM that it launches

In writing this:
Code:
if ( value == null ) throw new IllegalArgumentException ( "The value added is zero." ) ;
If the exception is thrown, the logs will be readable as a problem on a method argument (not an unhandled exception in your application)
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Could NullPointerException cause some problems"
Thread Thread Starter Forum Replies Last Post
What is java.lang.NullPointerException Logan 2 Software Development 5 27-02-2010 05:30 AM
Error NullPointerException in my program Linux-Us Software Development 3 02-12-2009 02:11 PM
NullPointerException exception occures in "AWT-EventQueue-0" Harmony60 Software Development 4 09-11-2009 06:52 PM
NullPointerException when using array of string in Java Sean J Software Development 2 25-04-2009 08:14 PM
2 different vista problems. Memory dump crash and startup repair cannot fix problems Hiera Vista Help 4 28-06-2008 01:06 PM


All times are GMT +5.5. The time now is 01:29 AM.