问题描述
- 实在搞不懂,这里的错误。求助
-
package viewer; import java.awt.BorderLayout; import java.awt.Dialog; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javafx.scene.layout.Border; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JScrollBar; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import util.EmpUtil; import model.EmpException; import model.User; public class UserPanel extends JPanel { private static final long serialVersionUID = 1L; private JPanel jp1, jp2; private JLabel jl1; private JTable jt; private JScrollPane jsp; private JButton jb1, jb2, jb3; private JTable jtable; private UserTableModel usermodel; private AddDialog ad; private JFrame jf; public UserPanel(JFrame jf) { this.setLayout(new BorderLayout()); this.jf = jf; jp1 = new JPanel(); jp2 = new JPanel(); jl1 = new JLabel("用户管理界面"); jb1 = new JButton("添加用户"); jb1.addActionListener(new UserManagerClick()); jb2 = new JButton("删除用户"); jb2.addActionListener(new UserManagerClick()); jb3 = new JButton("修改用户"); jb3.addActionListener(new UserManagerClick()); jp1.add(jl1); jp2.add(jb1); jp2.add(jb2); jp2.add(jb3); usermodel = new UserTableModel(); jtable = new JTable(usermodel); jsp = new JScrollPane(jtable); this.add(jp1, BorderLayout.NORTH); this.add(jp2, BorderLayout.SOUTH); this.add(jsp); } private class UserManagerClick implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == jb1) { // 添加 ad = new AddDialog(); } else if (e.getSource() == jb2) { // 删除 } else if (e.getSource() == jb3) { // 修改 } } } private class AddDialog extends JDialog { private JLabel jl1, jl2, jl3; private JButton jb1, jb2; private JPanel jp1, jp2, jp3, jp4; private JTextField jtf1, jtf2; private JPasswordField jpf; private JDialog jdg=this; public AddDialog() { this.setSize(300, 200); this.setModal(true); // 设置为模态对话框,不能操作主窗体。如果这里用JFrame就无法实现该功能了。 this.setLocation(jf.getX() + 50, jf.getY() + 50); this.setTitle("添加用户信息"); this.setLayout(new GridLayout(4, 1)); jp1 = new JPanel(); jp2 = new JPanel(); jp3 = new JPanel(); jp4 = new JPanel(); jl1 = new JLabel("用 户 名:"); jl2 = new JLabel("用户密码:"); jl3 = new JLabel("用户昵称:"); jb1 = new JButton("添加用户");jb1.addActionListener(new AddDialogClick()); jb2 = new JButton("重置数据");jb2.addActionListener(new AddDialogClick()); jtf1 = new JTextField(20); jtf2 = new JTextField(20); jpf = new JPasswordField(20); jp1.add(jl1); jp1.add(jtf1); jp2.add(jl2); jp2.add(jpf); jp3.add(jl3); jp3.add(jtf2); jp4.add(jb1); jp4.add(jb2); this.add(jp1); this.add(jp2); this.add(jp3); this.add(jp4); this.pack(); this.setResizable(false); this.setVisible(true); } private void reset() { jtf1.setText(""); jtf2.setText(""); jpf.setText(""); } private class AddDialogClick implements ActionListener { @Override public void actionPerformed(ActionEvent e) { try { if (e.getSource() == jb1) { // 添加用户 String username=jtf1.getText(); if (username == null ||"".equals(username.trim())) { //属于哪个窗口就是当弹出提示对话框时,非属于的其他窗体全部隐藏不显示. /* JOptionPane.showMessageDialog(jdg, "请输入正确的用户名!", "发现错误!", JOptionPane.ERROR_MESSAGE);*/ EmpUtil.showError(jdg, "请输入正确的用户名!"); return;//必须得有return。虽然弹出提示对话框,但是程序还是继续往下执行的。 } String password=new String(jpf.getPassword()); String nickname=jtf2.getText(); User u=new User(); u.setUsername(username); u.setPassword(password); u.setNickname(nickname); usermodel.getUd().add(u); AddDialog.this.setVisible(false); //ad.dispose(); } else if (e.getSource() == jb2) { // 重置数据 reset(); } } catch (EmpException e1) { EmpUtil.showError(jdg, e1.getMessage()); } } } }}
AddDialog.this.setVisible(false);为什么这句改成ad.setVisible(false);
会报空指针异常。
解决方案
if (e.getSource() == jb1) {
// 添加
ad = new AddDialog();//弹出窗体,然后这里是不往下执行,只有窗体不显示了才往下执行。所以在弹出的时候,界面内操作时,ad还没有赋值完成,是null,你可以这句话前后加print看看执行过程
}
解决方案二:
你确定时ad空指针错误么?我运行了你的代码,把你没有提供的类注释掉后,可以完成你想要的操作。这是我运行的代码,没有异常,贴代码挺好的,但是贴异常信息最重要。
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class UserPanel extends JPanel {
private static final long serialVersionUID = 1L;
private JPanel jp1, jp2;
private JLabel jl1;
private JTable jt;
private JScrollPane jsp;
private JButton jb1, jb2, jb3;
private JTable jtable;
private AddDialog ad;
private JFrame jf;
public UserPanel(JFrame jf) {
this.setLayout(new BorderLayout());
this.jf = jf;
jp1 = new JPanel();
jp2 = new JPanel();
jl1 = new JLabel("用户管理界面");
jb1 = new JButton("添加用户");
jb1.addActionListener(new UserManagerClick());
jb2 = new JButton("删除用户");
jb2.addActionListener(new UserManagerClick());
jb3 = new JButton("修改用户");
jb3.addActionListener(new UserManagerClick());
jp1.add(jl1);
jp2.add(jb1);
jp2.add(jb2);
jp2.add(jb3);
// usermodel = new UserTableModel();
jtable = new JTable();
jsp = new JScrollPane(jtable);
this.add(jp1, BorderLayout.NORTH);
this.add(jp2, BorderLayout.SOUTH);
this.add(jsp);
}
private class UserManagerClick implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb1) {
// 添加
ad = new AddDialog();
} else if (e.getSource() == jb2) {
// 删除
} else if (e.getSource() == jb3) {
// 修改
}
}
}
private class AddDialog extends JDialog {
private JLabel jl1, jl2, jl3;
private JButton jb1, jb2;
private JPanel jp1, jp2, jp3, jp4;
private JTextField jtf1, jtf2;
private JPasswordField jpf;
private JDialog jdg = this;
public AddDialog() {
this.setSize(300, 200);
this.setModal(true); // 设置为模态对话框,不能操作主窗体。如果这里用JFrame就无法实现该功能了。
this.setLocation(jf.getX() + 50, jf.getY() + 50);
this.setTitle("添加用户信息");
this.setLayout(new GridLayout(4, 1));
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
jp4 = new JPanel();
jl1 = new JLabel("用 户 名:");
jl2 = new JLabel("用户密码:");
jl3 = new JLabel("用户昵称:");
jb1 = new JButton("添加用户");
jb1.addActionListener(new AddDialogClick());
jb2 = new JButton("重置数据");
jb2.addActionListener(new AddDialogClick());
jtf1 = new JTextField(20);
jtf2 = new JTextField(20);
jpf = new JPasswordField(20);
jp1.add(jl1);
jp1.add(jtf1);
jp2.add(jl2);
jp2.add(jpf);
jp3.add(jl3);
jp3.add(jtf2);
jp4.add(jb1);
jp4.add(jb2);
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.add(jp4);
this.pack();
this.setResizable(false);
this.setVisible(true);
}
private void reset() {
jtf1.setText("");
jtf2.setText("");
jpf.setText("");
}
private class AddDialogClick implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == jb1) {
// 添加用户
String username = jtf1.getText();
if (username == null || "".equals(username.trim())) {
// 属于哪个窗口就是当弹出提示对话框时,非属于的其他窗体全部隐藏不显示.
/*
* JOptionPane.showMessageDialog(jdg, "请输入正确的用户名!",
* "发现错误!", JOptionPane.ERROR_MESSAGE);
*/
// EmpUtil.showError(jdg, "请输入正确的用户名!");
return;// 必须得有return。虽然弹出提示对话框,但是程序还是继续往下执行的。
}
String password = new String(jpf.getPassword());
String nickname = jtf2.getText();
// User u = new User();
// u.setUsername(username);
// u.setPassword(password);
// u.setNickname(nickname);
// usermodel.getUd().add(u);
AddDialog.this.setVisible(false);
// ad.dispose();
} else if (e.getSource() == jb2) {
// 重置数据
reset();
}
} catch (Exception e1) {
// EmpUtil.showError(jdg, e1.getMessage());
}
}
}
}
public static void main(String[] args) {
JFrame frm = new JFrame();
UserPanel up = new UserPanel(frm);
frm.getContentPane().add(up);
frm.setSize(300, 400);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setLocationRelativeTo(null);
frm.setVisible(true);
}
}
解决方案三:
整个代码只有UserManagerClick里会初始化ad
如果直接执行AddDialogClick,ad就是null
解决方案四:
AddDialog 没有实例化
解决方案六:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at viewer.UserPanel$AddDialog$AddDialogClick.actionPerformed(UserPanel.java:141)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.WaitDispatchSupport$2.run(Unknown Source)
at java.awt.WaitDispatchSupport$4.run(Unknown Source)
at java.awt.WaitDispatchSupport$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.WaitDispatchSupport.enter(Unknown Source)
at java.awt.Dialog.show(Unknown Source)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at java.awt.Window.setVisible(Unknown Source)
at java.awt.Dialog.setVisible(Unknown Source)
at viewer.UserPanel$AddDialog.<init>(UserPanel.java:108)
at viewer.UserPanel$UserManagerClick.actionPerformed(UserPanel.java:59)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
解决方案七:
if (e.getSource() == jb1) {
// 添加
ad = new AddDialog();//弹出窗体,然后这里是不往下执行,只有窗体不显示了才往下执行。所以在弹出的时候,界面内操作时,ad还没有赋值完成,是null,你可以这句话前后加print看看执行过程
时间: 2025-01-31 01:43:36