I have a JPanel that I designed in NetBeans' Mattise form editor. I now want to include this form in a JFrame. However, this is failing because in the contructor of the JPanel, it refers to a static variable from elsewhere in the program, which in the form editor is null
, because it is only initialized in the program elsewhere, before the JFrame is loaded, so it comes up with a NullPointerException
.
public UserManagementPanel() {
initComponents();
loadUserRequest();
}
private void loadUserRequest() {
GetUserListRequest req = new GetUserListRequest();
req.setListener(this);
transcription.Main.cq.addRequest(req);
}
It fails on the call to transcription.Main.cq.addRequest
, because it is not initialized.
java.lang.NullPointerException
at transcription.users.UserManagementPanel.loadUserRequest(UserManagementPanel.java:28)
at transcription.users.UserManagementPanel.<init>(UserManagementPanel.java:22)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at java.lang.Class.newInstance(Class.java:433)
at org.netbeans.modules.form.CreationFactory.createDefaultInstance(CreationFactory.java:178)
at org.netbeans.modules.form.RADComponent.createBeanInstance(RADComponent.java:252)
at org.netbeans.modules.form.RADComponent.initInstance(RADComponent.java:191)
at org.netbeans.modules.form.GandalfPersistenceManager.restoreComponent(GandalfPersistenceManager.java:780)
at org.netbeans.modules.form.GandalfPersistenceManager.loadComponent(GandalfPersistenceManager.java:968)
at org.netbeans.modules.form.GandalfPersistenceManager.restoreComponent(GandalfPersistenceManager.java:824)
at org.netbeans.modules.form.GandalfPersistenceManager.loadComponent(GandalfPersistenceManager.java:968)
at org.netbeans.modules.form.GandalfPersistenceManager.loadForm(GandalfPersistenceManager.java:503)
at org.netbeans.modules.form.GandalfPersistenceManager.loadForm(GandalfPersistenceManager.java:283)
at org.netbeans.modules.form.FormEditor$2.run(FormEditor.java:352)
at org.netbeans.modules.form.FormLAF$2.run(FormLAF.java:293)
at org.openide.util.Mutex.doEventAccess(Mutex.java:1376)
at org.openide.util.Mutex.readAccess(Mutex.java:327)
at org.netbeans.modules.form.FormLAF.executeWithLookAndFeel(FormLAF.java:276)
at org.netbeans.modules.form.FormEditor.loadFormData(FormEditor.java:349)
at org.netbeans.modules.nbform.FormEditorSupport.loadOpeningForm(FormEditorSupport.java:467)
at org.netbeans.modules.nbform.FormDesignerTC.loadForm(FormDesignerTC.java:279)
at org.netbeans.modules.nbform.FormDesignerTC.access$300(FormDesignerTC.java:87)
at org.netbeans.modules.nbform.FormDesignerTC$PreLoadTask$1.run(FormDesignerTC.java:268)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:159)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
This piece of code is not important for the form editor - I only need that code to be run in the actual program. Is there a way I can tell the form editor to ignore that code when initializing it?
In the end, I did a simple workaround - I caught and ignored the NullPointerException:
This isn't a perfect solution, because if for some reason during actual program execution cq would actually be
null
, then I would want it to throw an exception, so I'm curious if anyone else has a more elegant solution.