零点异常 [英] Null point exception

查看:85
本文介绍了零点异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();
            }
}







我在上面的代码中得到一个零点异常第5行.-> a.setBalance(a.getBalance()+ a.getDepositamt());



请告诉我这个解决方案





OP更新(3小时后)


我可以这样做:






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);
            }

推荐答案

您应该首先了解基础知识。以下是程序抛出 NullPointerException [ ^ ]。

  • 在null对象上调用instance方法时。如果在null对象上调用静态方法或类方法,则不会得到空指针异常,因为静态方法不需要实例来调用任何方法。
  • 访问或更改null上的任何变量或字段时
  • 当预期抛出异常时抛出null。
  • 当数组为null时调用数组的长度。
  • 访问或更改null的插槽像数组一样。
  • 当您尝试在null对象上进行同步或在Java中的synchronized块内使用null时
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
  • 确定发生的行。
  • 询问引用了哪些对象(或数组)这条线。其中一个是null,通常很明显。
  • 弄清楚为什么它为空。


你可以做的就是调试:



使用Eclipse进行调试 [< a href =http://www.vogella.com/articles/EclipseDebugging/article.htmltarget =_ b lanktitle =新窗口> ^ ]



使用Netbeans进行调试 [ ^ ]



像其中一个值的接缝是NULL:



- a(无论a是什么。 ..请使用更好的名字)

- 返回值a.getBalance()

- 返回值a.getDepositamt()



所以你应该通过调试检查这些值。
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.


我可以这样做:



can I do this :

if(a!=null){
            a.setBalance(a.getBalance() + a.getDepositamt());
            adi.update(account);
            }else {
                adi.update(account);
            }


这篇关于零点异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆