NullPointerException durante il tentativo di impostare un jcheckbox
-
22-07-2019 - |
Domanda
Tentando di impostarlo in modo che, se viene soddisfatta una determinata condizione, verrà selezionata una delle due caselle di controllo. Tuttavia continuo a ricevere un errore nullpointerexception.
il codice è ..
//Set the flat rate or hourly billing check boxes.
if(flatRateint > 0) {
InvoiceUI.jCheckBox1.setSelected(true);
}
else {
InvoiceUI.jCheckBox2.setSelected(true);
}
L'errore è
Eccezione nel thread " AWT-EventQueue-0 " java.lang.NullPointerException at my.freelancebillingapp.InvoiceSelectionUI.jButton1MouseClicked (InvoiceSelectionUI.java:224) su 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)
Soluzione
Supponendo che la riga 224 di InvoiceSelectionUI.java sia inclusa nell'esempio, uno dei seguenti deve essere nullo:
-
InvoiceUI
-
InvoiceUI.jCheckBox1
-
InvoiceUI.jCheckBox2
-
flatRateint
(se si tratta di unnumero intero
, ma non se si tratta di unint
)
Altri suggerimenti
Penso che jcheckBox [12] siano campi statici di InvoiceUI che non hai inizializzato.
Il mio primo istinto è che flatRateint
potrebbe essere nullo, ma dal nome suppongo sia un int
primitivo, che lo escluderebbe. Il tuo messaggio di errore riguarda i clic del mouse; quindi sospetto che il codice nel tuo metodo onclick ()
. Non sono un guru, ma trovo che gli errori si verificano spesso due o tre righe prima della posizione indicata nella stampa dell'errore reale. Forse qualcosa come
if(someObj.someMethod() == foo)
flatRateint = 5;
else
flatRateint = 8;
e someObj
non sono mai stati inizializzati?