在matise中加载表单时不执行某些代码

时间:2014-11-11 18:03:27

标签: java swing netbeans nullpointerexception matisse

我有一个我在NetBeans的Mattise表单编辑器中设计的JPanel。我现在想将此表单包含在JFrame中。但是,这是失败的,因为在JPanel的构造函数中,它引用了程序中其他位置的静态变量,它在表单编辑器中为null,因为它仅在程序中的其他程序中初始化,在JFrame之前已加载,因此它会出现NullPointerException

public UserManagementPanel() {
    initComponents();
    loadUserRequest();
}

private void loadUserRequest() {
    GetUserListRequest req = new GetUserListRequest();
    req.setListener(this);
    transcription.Main.cq.addRequest(req);
}

调用transcription.Main.cq.addRequest失败,因为它未初始化。

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)

这段代码对于表单编辑器并不重要 - 我只需要在实际程序中运行该代码。有没有办法告诉表单编辑器在初始化时忽略该代码?

1 个答案:

答案 0 :(得分:0)

最后,我做了一个简单的解决方法 - 我抓住并忽略了NullPointerException:

private void loadUserRequest() {
    GetUserListRequest req = new GetUserListRequest();
    req.setListener(this);
    try {
        transcription.Main.cq.addRequest(req);
    } catch (NullPointerException e) {}
}

这不是一个完美的解决方案,因为如果由于某些原因在实际程序执行期间cq实际上是null,那么我会希望它抛出异常,所以我很好奇其他人是否有更优雅的解决方案。