This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Not catching a null somewhere Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Not catching a null somewhere" Watch "Not catching a null somewhere" New topic
Author

Not catching a null somewhere

Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Hello All, I have the following piece of code:

that sets up a call later in the program to stockInventoryLevel.
When I run this code:

I get a null pointer exception, however when I run

I do not get the error. I know I am failing to catch a null somewhere in the first block of code and would appreciate any help in pointing out my error.

Thank you!
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2840
    
  11

I'd guess that stockLevelInventory is null when you call that code. Calling a method (like toString()) on a null reference causes an NPE. Passing a null as a parameter (to product.setStockLevel()) doesn't cause an NPE, unless something in that method tries to dereference it. If you can run the code under a debugger, you can see exactly what the value of stockLevelInventory is when you reach that line of code.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38040
    
  22
In my opinion you should never let nulls live. Make sure your constructor and setXXX methods prohibit them as far as possible. Make sure all fields are initialised to a value which establishes the class invariants by the time the constructor completes.Even this cannot eliminate all nulls; for example if I were a Name object my middleName field would be null.
An alternative version of the constructor follows:-
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Hi, Thank you both for replying. I have stepped through the debugger and stockLevelInventory is null down to the call after which if a value is not returned then should set it up for the product variable assignment, but still nulls are getting through as demonstrated with the NPE. I am not sure how this is happening.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Dan Grindstaff wrote:

from where you are calling this?
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Right below the nested if structure.

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2840
    
  11

The problem is you only assign a non-null value to stockLevelInventory deep into your nesting structure. There's a number of conditions that would mean you never got to lines 13-17, such as detailItem is null, subItems is empty, and something about a code not matching. That would mean the null you set to it at line 1, would still be there when you got to line 36. You could try setting it to 0 at line 1 (in addition to lines 14 - 17).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38040
    
  22
What type is it? Is it an Integer or similar? If it is, why are you using a Wrapper, rather than the primitive int?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4446
    
    5

A couple of things:
1. Arrow Anti-Pattern - the indentation and deep nesting results in an arrowhead form. Deeply nested code is more complex and is more difficult to debug. The complexity of your code is made even greater because you have nested if-statements and for-statements
2. If a method parameter should not be null, I prefer to throw an IllegalArgumentException rather than a NullPointerException. I treat NullPointerExceptions as programming errors; I just let them happen but try to find them as soon as possible through unit testing.


Junilu - [How to Ask Questions] [How to Answer Questions]
 
wood burning stoves
 
subject: Not catching a null somewhere
 
Similar Threads
error while connecting to webservices
Exiting for loop using JOptionPane
Can't close JOptionDialog box by clicking OK
Doubt about Switch statement (Compile Time Constant)
beginner SharedPreferences, any ready made code open source