Java – NullPointerException when trying to set a jcheckbox

javanullpointerexception

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.EventDispatchThread.run(EventDispatchThread.java:122)

Best Answer

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)