1
     Product p = dao.checkProduct(pnumber);
     ExpensiveProduct ep = dao.checkexpensiveProduct(pnumber);
     if ((p.getNumber() == null)&&(ep.getNumber()==null) ){ // java.lang.NullPointerException 
     //do something

     }else{
    //do something

      }

Why this statement giving java.lang.NullPointerException Do I have any other way to check this?

2
  • 2
    Make sure p and ep are non-null before calling getNumber() on them. Commented Oct 21, 2012 at 17:03
  • 1
    Full error? + More of the code... Commented Oct 21, 2012 at 17:03

5 Answers 5

3

The only non-trivial possibility where this code can throw NPE is pnumber being Integer where checkProduct() or checkexpensiveProduct() expects int (or similar).

Other trivial reasons are dao or p being null (easy to check).

Sign up to request clarification or add additional context in comments.

1 Comment

i need to check that nulls and get true on that statement can i && those 2?
2

Try it like this:

if ((p != null) && (p.getNumber() == null) && (ep != null) && (ep.getNumber()==null) ){

2 Comments

this always false i need to do else part also any idea to do that?
If it's always false, I think you have your answer. Fix p and ep.
1

NullPointerExceptions (NPEs) occur when you call a method or access a property of a null object.

To check for nulls, you could print the values of your variables before you try to use them, or step through your program with a debugger and check what the variables' values are when you reach the line where the exception happens.

EDIT:
Regarding your comment

i need to check p.getNumber() and ep.getNumber() both returning null and get ture on that statement

your existing code

if ((p.getNumber() == null)&&(ep.getNumber()==null) )

is already doing that. Since you're getting an NPE on that line, p itself is null, or ep is null, or both. You should examine your checkProduct() and checkexpensiveProduct() methods, where p and ep are set, to see if they're working correctly.

4 Comments

i need to check that nulls and get true on that statement can i && those 2?
I don't understand your comment. Can you rephrase?
i need to check p.getNumber() and ep.getNumber() both returning null and get ture on that statement
I edited. In your program, does it ever make sense for p or ep to be null? Or would they only ever be null if something went wrong?
1

check your p variable and ep variable .One of them is null.check why

 dao.checkProduct(pnumber)  

or

dao.checkexpensiveProduct(pnumber); is returning null

Comments

0

What line is giving the NullPointerException? Be sure that p or ep are not null.

4 Comments

i need to check those are returning null and get ture and do else part.
If one of the objects is null, you can't use his methods like you did in your code. Check the methods dao.checkProduct() and dao.checkexpensiveProduct() to be sure that they will not return a null object. Instead, they can return a instatiated object without any attribute.
so you are saying if both return null its gonabe NullPointerException?
If one method return null, the code will throw the NullPointerException because you are trying to acess a method within a null object. When you do p.getNumber(), if p is null, you'll get a NullPointerException. Same thing for the pe object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.