在mattise中加载表单时未执行某些代码



我有一个JPanel,它是在NetBeans的Mattise表单编辑器中设计的。我现在想将此表单包含在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)

这段代码对表单编辑器来说并不重要——我只需要在实际程序中运行这段代码。有没有一种方法可以告诉表单编辑器在初始化时忽略该代码?

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

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

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

相关内容

  • 没有找到相关文章

最新更新