gpt4 book ai didi

java - 线程中出现异常 "AWT-EventQueue-0"java.lang.NullPointerException JInternalFrame 关闭方法

转载 作者:行者123 更新时间:2023-11-29 15:23:49 24 4
gpt4 key购买 nike

有人可以帮我解决这个错误吗?

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at oopw4.FormManager.searchForm(FormManager.java:51)
at oopw4.FormManager.unregisterForm(FormManager.java:40)

FormManager.class

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package oopw4;

import java.util.ArrayList;
import javax.swing.JInternalFrame;

public class FormManager {

private JInternalFrame openedForm = null;
private ArrayList<JInternalFrame> frameList = new ArrayList<>();

public boolean isFormOpened(String namaForm) {
boolean isFound = false;
JInternalFrame jif = searchForm(namaForm);
if (jif != null) {
isFound = true;
openedForm = jif;
}
return isFound;
}

public JInternalFrame getOpenedForm() {
return openedForm;
}

public void registerForm(JInternalFrame frame) {
frameList.add(frame);
}

public void unregisterForm(JInternalFrame frame) {
JInternalFrame jif = (JInternalFrame)searchForm(frame.getName());
if (jif != null) {
frameList.remove(frame);
}
}

private JInternalFrame searchForm(String namaForm) {
JInternalFrame frame = null;
int idx = 0;

while (idx < frameList.size()) {
if (frameList.get(idx).getName().equals(namaForm)) {
frame = frameList.get(idx);
break;
} else {
idx++;
}
}
return frame;
}
}

UserCrud.class

private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
// TODO add your handling code here:
AppLib.formManager.unregisterForm(this);
}

MainFormDemo.class

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package oopw4;

import javax.swing.*;
import simbengkel.AppLib;

public class MainFormDemo extends javax.swing.JFrame {

/**
* Creates new form Frame
*/
public MainFormDemo() {
initComponents();
setExtendedState(JFrame.MAXIMIZED_BOTH);
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

DesktopPane = new javax.swing.JDesktopPane();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
menuOpenedForm = new javax.swing.JMenuItem();
openFormTableUser = new javax.swing.JMenuItem();
menuClosedForm = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
menuHelp = new javax.swing.JMenu();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setLocation(new java.awt.Point(0, 0));
getContentPane().setLayout(new java.awt.GridLayout(1, 0));

javax.swing.GroupLayout DesktopPaneLayout = new javax.swing.GroupLayout(DesktopPane);
DesktopPane.setLayout(DesktopPaneLayout);
DesktopPaneLayout.setHorizontalGroup(
DesktopPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 507, Short.MAX_VALUE)
);
DesktopPaneLayout.setVerticalGroup(
DesktopPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 319, Short.MAX_VALUE)
);

getContentPane().add(DesktopPane);

jMenu1.setText("File");

menuOpenedForm.setText("Tic Tac Toe");
menuOpenedForm.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuOpenedFormActionPerformed(evt);
}
});
jMenu1.add(menuOpenedForm);

openFormTableUser.setText("Table User");
openFormTableUser.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openFormTableUserActionPerformed(evt);
}
});
jMenu1.add(openFormTableUser);

menuClosedForm.setText("Logout");
menuClosedForm.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuClosedFormActionPerformed(evt);
}
});
jMenu1.add(menuClosedForm);

jMenuBar1.add(jMenu1);

jMenu2.setText("Edit");

menuHelp.setText("Help");
jMenu2.add(menuHelp);

jMenuBar1.add(jMenu2);

setJMenuBar(jMenuBar1);

pack();
setLocationRelativeTo(null);
}// </editor-fold>

private void menuOpenedFormActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.dispose();
TicTacToe main = new TicTacToe();
main.show();
}

private void menuClosedFormActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.dispose();
LoginUser login = new LoginUser();
login.show();
}

private void openFormTableUserActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (AppLib.formManager.isFormOpened("UserCrud")) {
JOptionPane.showMessageDialog(this, "Form Already Opened");
JInternalFrame jif = AppLib.formManager.getOpenedForm();
jif.setVisible(true);
} else {
UserCrud tableUser = new UserCrud();
DesktopPane.add(tableUser);
tableUser.setVisible(true);
AppLib.formManager.registerForm(tableUser);
}
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainFormDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainFormDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainFormDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainFormDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainFormDemo().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JDesktopPane DesktopPane;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem menuClosedForm;
private javax.swing.JMenu menuHelp;
private javax.swing.JMenuItem menuOpenedForm;
private javax.swing.JMenuItem openFormTableUser;
// End of variables declaration
}

错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at oopw4.FormManager.searchForm(FormManager.java:51)
at oopw4.FormManager.unregisterForm(FormManager.java:40)
at oopw4.UserCrud.closeButtonActionPerformed(UserCrud.java:263)
at oopw4.UserCrud.access$500(UserCrud.java:24)
at oopw4.UserCrud$6.actionPerformed(UserCrud.java:187)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
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)

有人可以帮助我吗?

最佳答案

请记住 Swing UI 在其自己的线程上运行。而且 Swing 中的组件并非设计为线程安全的。那么,欢迎来到 Swing。您可能会发现您的搜索功能在此处发生故障 frameList.get(idx).getName().equals(namaForm) ...因为框架尚未创建。阅读“InvokeLater”并使用它来响应 UX 事件。这可以确保在您尝试对组件执行任何操作之前,执行窗口管理的 Swing 线程全部被捕获。

关于java - 线程中出现异常 "AWT-EventQueue-0"java.lang.NullPointerException JInternalFrame 关闭方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59181661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com