Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
try{
            AccountDAOImpl adi = new AccountDAOImpl();
            Account a = new Account();
            a = adi.getAccount(account, depositamt);
            a.setBalance(a.getBalance() + a.getDepositamt());
            adi.update(account);
            }catch(Exception ex){
                ex.printStackTrace();
            }
}



im getting a null point exception in above code 5th line.-> a.setBalance(a.getBalance() + a.getDepositamt());

Please tell me a solution for this


Updates by OP(3h later)

can I do this :

Java
if(a!=null){
            a.setBalance(a.getBalance() + a.getDepositamt());
            adi.update(account);
            }else {
                adi.update(account);
            }
Posted
Updated 13-Jun-13 23:05pm
v2
Comments
[no name] 3-Feb-14 2:11am    
why we should handle the NullpointerException in if block only why not in trycatch block.give me the answer

You should understand the basics first. Following are the possible causes when program throws NullPointerException[^].
  • When you call instance method on a null object. you won't get null pointer exception if you call static method or class method on null object because static method doesn't require an instance to call any method.
  • While accessing or changing any variable or field on null object.
  • Throwing null when an Exception is expected to throw.
  • When calling length of array when array is null.
  • Accessing or changing slots of null just like an array.
  • When you try to synchronize on null object or using null inside synchronized block in Java


Following are the checkpoints to resolve this issue:
  • Identify the line where it occurred.
  • Ask "what objects (or arrays) are being referenced on this line". One of them is null and it's usually obvious which.
  • Figure out why it's null.


You can also check:
How to : Avoid Null Pointer Exception in Java[^]
Why do I get a NullPointerException?[^]



--Amit
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Jul-13 23:22pm    
Well explained, a 5.
—SA
_Amy 12-Jul-13 21:31pm    
Thank you Sergery. :)
What you can do is debugging:

Debugging with Eclipse[^]

Debugging with Netbeans[^]

Seams like one of the values is NULL:

- a (whatever "a" is... please use better names)
- return value of a.getBalance()
- return value of a.getDepositamt()

So you should check the values by debugging.
 
Share this answer
 
can I do this :

C#
if(a!=null){
            a.setBalance(a.getBalance() + a.getDepositamt());
            adi.update(account);
            }else {
                adi.update(account);
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900