开发者

NullPointerException when trying to set a jcheckbox

Trying to set it so if a certain condition is met then one of two check-boxes will be checked. However I keep getting a nullpointerexception error.

the code is..

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

The error is

Exception in thread "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) at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253) at java.awt.Component.processMouseEvent(Component.java:6266) at 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) at java.awt.开发者_如何学JAVAEventDispatchThread.run(EventDispatchThread.java:122)


Assuming line 224 of InvoiceSelectionUI.java is included in your sample, one of the following must be null:

  • InvoiceUI
  • InvoiceUI.jCheckBox1
  • InvoiceUI.jCheckBox2
  • flatRateint (if it's an Integer, but not if it's an int)


I think jcheckBox[12] are static fields of InvoiceUI you didn't initialize.


My first instinct is that flatRateint might be null, but from the name I'm guessing it's a primitive int, which would rule that out. Your error message is about mouse clicks; so I suspect code in your onclick() method. I'm no guru, but I find that errors often occur two or three lines before the location mentioned in the actual error printout. Maybe something like

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

and someObj was never initialized?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜