2 lhs472194266 lhs472194266 于 2015.07.22 12:28 提问

实在搞不懂,这里的错误。求助
 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);
会报空指针异常。

7个回答

danielinbiti
danielinbiti   Ds   Rxr 2015.07.22 13:03
已采纳
 if (e.getSource() == jb1) {
                // 添加
                ad = new AddDialog();//弹出窗体,然后这里是不往下执行,只有窗体不显示了才往下执行。所以在弹出的时候,界面内操作时,ad还没有赋值完成,是null,你可以这句话前后加print看看执行过程


            } 

danielinbiti
danielinbiti 回复中国好男人: 恩,模态和非模态的两种方式。如果有帮助,有时间就采纳一下
2 年多之前 回复
lhs472194266
lhs472194266 this.setModal(true); 原来是被这一句给阻塞住了,如果没有这句,我以上的问题就全部解决了
2 年多之前 回复
danielinbiti
danielinbiti 回复中国好男人: 因为你自己设置了模态方式的对话框 this.setModal(true); // 设置为模态对话框,不能操作主窗体。如果这里用JFrame就无法实现该功能了。设置成false就接着往下执行了
2 年多之前 回复
lhs472194266
lhs472194266 可是我在主窗体JFrame后面添加了输出,窗体没有关闭,后面语句继续执行了。public class RunFrame extends JFrame { public static void main(String[] args) { new ManagerFrame(); System.out.println("3333333333"); } }
2 年多之前 回复
lhs472194266
lhs472194266 是不是在GUI这部分内容都是这样。new出来的窗体若不关闭,该new方法就是还未彻底执行结束??
2 年多之前 回复
lhs472194266
lhs472194266 对,你说到点上了,就是这里导致的。这里是不往下执行。这句话与我以往的学习,既然都new出来,怎么会不往下执行呢。
2 年多之前 回复
tongyi55555
tongyi55555   2015.07.22 12:51

你确定时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);
    }
}

tongyi55555
tongyi55555 回复中国好男人: 因为你ad = new AddDialog();时会显示对话框,此时并没有给ad赋值,去掉AddDialog中的setVisible,然后在ad = new AddDialog();下增加ad.setVisible(true);即可。
2 年多之前 回复
lhs472194266
lhs472194266 AddDialog.this.setVisible(false);为什么这句改成ad.setVisible(false); 会报空指针异常。
2 年多之前 回复
lhs472194266
lhs472194266 已帖错误异常信息,求指导。
2 年多之前 回复
caozhy
caozhy   Ds   Rxr 2015.07.22 12:32

整个代码只有UserManagerClick里会初始化ad
如果直接执行AddDialogClick,ad就是null

lhs472194266
lhs472194266 换句话就是:在内部类中调用在外部类创建的内部类对象会爆空指针异常。
2 年多之前 回复
lhs472194266
lhs472194266 但是我是先执行的UserManagerClick 为ad赋值了,弹出一个JDialog窗口。然后在这个JDialog上又点击添加用户,触发AddDialogClick 的。。按理说这里ad应该已经是完成初始化了。怎么还说空指针啊。
2 年多之前 回复
ck87801
ck87801   2015.07.22 12:35

AddDialog 没有实例化

lhs472194266
lhs472194266 可是JDialog 对话框都创建出来,就是ad啊,怎么没实例化呢。
2 年多之前 回复
lhs472194266
lhs472194266   2015.07.22 12:50

图片说明

lhs472194266
lhs472194266   2015.07.22 13:01
 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)

sina_2831808769
sina_2831808769   Rxr 2015.07.25 13:58

if (e.getSource() == jb1) {
// 添加
ad = new AddDialog();//弹出窗体,然后这里是不往下执行,只有窗体不显示了才往下执行。所以在弹出的时候,界面内操作时,ad还没有赋值完成,是null,你可以这句话前后加print看看执行过程

Csdn user default icon
上传中...
上传图片
插入图片