Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java
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 :
 
if(a!=null){
            a.setBalance(a.getBalance() + a.getDepositamt());
            adi.update(account);
            }else {
                adi.update(account);
            }
Posted 13-Jun-13 19:24pm
Edited 14-Jun-13 0:05am
TorstenH.33.9K
v2
Comments
Member 10566327 at 3-Feb-14 2:11am
   
why we should handle the NullpointerException in if block only why not in trycatch block.give me the answer
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 7-Jul-13 23:22pm
   
Well explained, a 5.
—SA
_Amy at 12-Jul-13 21:31pm
   
Thank you Sergery. :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

can I do this :
 
if(a!=null){
            a.setBalance(a.getBalance() + a.getDepositamt());
            adi.update(account);
            }else {
                adi.update(account);
            }
  Permalink  

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

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 14 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100