尝试设置jcheckbox时出现NullPointerException

时间:2009-10-31 00:26:12

标签: java nullpointerexception

尝试将其设置为如果满足某个条件,则将检查两个复选框中的一个。但是我一直收到nullpointerexception错误。

代码是..

        //Set the flat rate or hourly billing check boxes.
    if(flatRateint > 0) {
        InvoiceUI.jCheckBox1.setSelected(true);
    }
    else {
        InvoiceUI.jCheckBox2.setSelected(true);
    }

错误是

  

线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException           at my.freelancebillingapp.InvoiceSelectionUI.jButton1MouseClicked(InvoiceSelectionUI.java:224)           at my.freelancebillingapp.InvoiceSelectionUI.access $ 100(InvoiceSelectionUI.java:17)           at my.freelancebillingapp.InvoiceSelectionUI $ 2.mouseClicked(InvoiceSelectionUI.java:86)           在java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)           at java.awt.Component.processMouseEvent(Component.java:6266)           在javax.swing.JComponent.processMouseEvent(JComponent.java:3267)           at java.awt.Component.processEvent(Component.java:6028)           at java.awt.Container.processEvent(Container.java:2041)           at java.awt.Component.dispatchEventImpl(Component.java:4630)           at java.awt.Container.dispatchEventImpl(Container.java:2099)           at java.awt.Component.dispatchEvent(Component.java:4460)           at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)           at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4247)           at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)           at java.awt.Container.dispatchEventImpl(Container.java:2085)           at java.awt.Window.dispatchEventImpl(Window.java:2475)           at java.awt.Component.dispatchEvent(Component.java:4460)           at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)           at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)           at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)           at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)           at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)           at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)           在java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

3 个答案:

答案 0 :(得分:1)

假设InvoiceSelectionUI.java的第224行包含在您的示例中,则以下之一必须为null:

  • InvoiceUI
  • InvoiceUI.jCheckBox1
  • InvoiceUI.jCheckBox2
  • flatRateint(如果是Integer,但不是int

答案 1 :(得分:0)

我认为jcheckBox [12]是你没有初始化的InvoiceUI的静态字段。

答案 2 :(得分:0)

我的第一直觉是flatRateint可能为空,但从名称我猜它是一个原始的int,这将排除它。您的错误消息是关于鼠标点击;所以我怀疑你onclick()方法中的代码。我不是大师,但我发现错误经常发生在实际错误打印输出中提到的位置之前的两三行。也许像是

if(someObj.someMethod() == foo)
    flatRateint = 5;
else
    flatRateint = 8;

并且someObj从未初始化?