swing中的java.lang.NullPointerException

时间:2013-11-05 16:19:22

标签: java swing

每当我运行程序时,我都会收到此错误。当按下某些按钮时也没有 根据听众的反应(我不知道为什么!)

  1. 我在一起注册了所有按钮
  2. 所有按钮都使用相同的动作侦听器类
  3. 只有第3和第4个按钮响应听众,但第1和第2按钮不响应

        Exception in thread "main" java.lang.NullPointerException
    at IntergrationPoint.<init>(IntergrationPoint.java:73)
    at MainLibrary.main(MainLibrary.java:23)
    

    以下是带来所有这些问题的原因。 。 (我认为也是SSCCE)

        import java.awt.Color;
        import java.awt.FlowLayout;
        import java.awt.GridBagConstraints;
        import java.awt.GridBagLayout;
        import java.awt.GridLayout;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;
        import static java.awt.image.ImageObserver.WIDTH;
        import java.sql.SQLException;
        import java.util.logging.Level;
        import java.util.logging.Logger;
        import javax.swing.JButton;
        import javax.swing.JFrame;
        import javax.swing.JPanel;
        import javax.swing.JTabbedPane;
        import javax.swing.UIManager;
    
        /*
         * To change this template, choose Tools | Templates
         * and open the template in the editor.
         */
    
        /**
         *
         * @author Yunus
         */
        public class IntergrationPoint extends JFrame{
            JPanel tabOneContent;
            JPanel tabOTwoContent;
            JPanel tabThreeContent;
            JPanel tabFourContent;
    
    JTabbedPane intergrationTabs;
    
    public JButton createAccount;
    public JButton borrowBook;
    public JButton returnBook;
    public JButton addBook;
    public JButton deleteBook;
    public JButton deleteMember;
    public JButton displayMembers;
    public JButton displayAvailableBooks;
    public IntergrationPoint()
    {
        setLookAndFeel();
        intergrationTabs = new JTabbedPane();
        setSize(761, 430);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        tabOneContent = bookOperations();
        tabOTwoContent = memberOperations();
    
        intergrationTabs.addTab("<html><body marginheight=30 marginwidth=20>Book Operations<body><html>", null,tabOneContent,"");
        intergrationTabs.addTab("<html><body marginheight=30 marginwidth=20>Member Operations<body><html>", null,tabOTwoContent,"");
        intergrationTabs.addTab("<html><body marginheight=30 marginwidth=20>Display Members<body><html>", null,memberOperations(),"Display all available member");
        intergrationTabs.addTab("<html><body marginheight=30 marginwidth=20>Display Books<body><html>", null,memberOperations(),"Display all available book");
        intergrationTabs.addTab("<html><body marginheight=30 marginwidth=20>Login - Logout<body><html>", null,memberOperations(),"Login - Logout");
    
        add(intergrationTabs);
        setVisible(true);
        setResizable(false);
    
        ButtonListener listener = new ButtonListener();
        // Register listener with buttons 
        borrowBook.addActionListener(listener);
        returnBook.addActionListener(listener);
        addBook.addActionListener(listener);
        deleteBook.addActionListener(listener);
    
    
    }
    
    class ButtonListener implements ActionListener {
        @Override /** Implement actionPerformed */
        public void actionPerformed(ActionEvent ev){
            if(ev.getSource() == borrowBook)
                new BorrowBook();
            else if(ev.getSource() == returnBook)
                new ReturnBook();
            else if(ev.getSource() == addBook)
                try {
                new AddBook();
            } catch (SQLException ex) {
                Logger.getLogger(IntergrationPoint.class.getName()).log(Level.SEVERE, null, ex);
            }
            else if(ev.getSource() == deleteBook)
                new DeleteBook();
    
         }
    }
    
    
    public JPanel bookOperations()
    {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    
        JPanel insidePanel = new JPanel(new GridBagLayout());
    
        GridBagConstraints constraints = new  GridBagConstraints();
        constraints.fill =  GridBagConstraints.BOTH;
    constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.gridwidth = 1;
        constraints.gridheight = 2;
        constraints.ipadx = 5;
    constraints.ipady = 45;
    
        borrowBook = new JButton("Borrow Book");
        returnBook = new JButton("Return Book");
        addBook = new JButton("Add Book");
        deleteBook = new JButton("Delete Book");
    
        constraints.gridx = 0;
        constraints.gridy = 0;
        insidePanel.add(borrowBook,constraints);
    
        constraints.gridx = 0;
        constraints.gridy = 2;
        insidePanel.add(returnBook,constraints);
    
        constraints.gridx = 0;
        constraints.gridy = 4;
        insidePanel.add(addBook,constraints);
    
        constraints.gridx = 0;
        constraints.gridy = 6;
        insidePanel.add(deleteBook,constraints);
    
        panel.add(insidePanel);
    
        return panel;
    }
    
    
    public JPanel memberOperations()
    {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    
        JPanel insidePanel = new JPanel(new GridBagLayout());
    
        GridBagConstraints constraints = new  GridBagConstraints();
        constraints.fill =  GridBagConstraints.BOTH;
    constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.gridwidth = 1;
        constraints.gridheight = 2;
        constraints.ipadx = 0;
    constraints.ipady = 45;
    
        borrowBook = new JButton("Add Member");
        returnBook = new JButton("Delete Member");
    
        constraints.gridx = 0;
        constraints.gridy = 0;
        insidePanel.add(borrowBook,constraints);
    
        constraints.gridx = 0;
        constraints.gridy = 2;
        insidePanel.add(returnBook,constraints);
    
        panel.add(insidePanel);
    
        return panel;
    }
    
    
    private void setLookAndFeel()
    {
        try
        {
    
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    
        }catch(Exception ex)
        {
                ex.getMessage();
        }
    }
    }
    
    
        import java.sql.SQLException;
        import ui.BorrowBookUI;
        import ui.ReturnBookUI;
    
        public class MainLibrary {
            public static void main(String[] args) throws SQLException
            {
                IntergrationPoint intergrationPoint = new IntergrationPoint();
            }
    
        }
    

1 个答案:

答案 0 :(得分:1)

按钮在memberOperations()bookOperations()中初始化。例如,您使用的是同一个成员borrowBook。但它会在每次调用memberOperations()时重新分配。因此,一旦进入ButtonListener的设置,您实际上是在Login - Logout选项卡中的按钮上设置监听器。因此,如果您切换到该标签并点击Add Member,则会触发BorrowBook()操作。

对于NPE,很难说因为发布的代码只是一个片段,它不包含所有代码,遗憾的是,不编译。请发布SSCCE