java - JComboBox:如何解决 JComboBox 和 ItemListener 中的异常

标签 java database swing nullpointerexception jcombobox

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class AddBook extends JPanel implements ActionListener
{
    JLabel book_name_lbl,author_lbl,isbn_lbl,available_lbl,new_lbl,total_book_lbl,title_lbl;
    JTextField isbn_text,available_text,new_text,total_book_text;
    JComboBox book_name_box,author_box;
    JButton submit_btn,exit_btn;
        Connection conn;
        Statement smtp;
        ResultSet rs;
        PreparedStatement psmtp;
        String book,auther;
    public AddBook(Connection c)
    {
            conn = c;

        setLayout(new GridBagLayout());

        GridBagConstraints gbc1 = new GridBagConstraints();
        gbc1.insets = new Insets(10,10,10,10);

        title_lbl = new JLabel("Book Record Update");
        title_lbl.setFont(new Font("Goudy Old Style",Font.BOLD,30));
        gbc1.gridx = 0;
        gbc1.gridy = 0;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.gridwidth = 2;
        gbc1.insets = new Insets(10,3,30,3);
        gbc1.anchor = GridBagConstraints.CENTER;
        add(title_lbl,gbc1);

        book_name_lbl = new JLabel("Book Name :");
        gbc1.gridx = 0;
        gbc1.gridy = 1;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.gridwidth = 1;
        gbc1.insets = new Insets(10,10,10,10);
        gbc1.anchor = GridBagConstraints.WEST;
        add(book_name_lbl,gbc1);

        book_name_box = new JComboBox();
                book_name_box.addItem("Select book");
            try
        {
            smtp = conn.createStatement();
            rs = smtp.executeQuery("select BOOK_NAME from BOOK_NAME");

            while(rs.next())
            {
                String book_name = rs.getString("BOOK_NAME");
                                book_name_box.addItem(book_name);
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
               book_name_box.addActionListener(this);

        gbc1.gridx = 1;
        gbc1.gridy = 1;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.anchor = GridBagConstraints.EAST;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        add(book_name_box,gbc1);

        author_lbl = new JLabel("Author Name :");
        gbc1.gridx = 0;
        gbc1.gridy = 2;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.anchor = GridBagConstraints.WEST;
        add(author_lbl,gbc1);
// author combox add
                author_box = new JComboBox();
                author_box.addItem("Select Author");
                 author_box.addActionListener(this);
        gbc1.gridx = 1;
        gbc1.gridy = 2;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.anchor = GridBagConstraints.EAST;
        add(author_box,gbc1);

        isbn_lbl = new JLabel("ISBN :");
        gbc1.gridx = 0;
        gbc1.gridy = 3;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.anchor = GridBagConstraints.WEST;
        add(isbn_lbl,gbc1);

        isbn_text = new JTextField(15);
                isbn_text.setEditable(true);
                isbn_text.setEditable(true);
        gbc1.gridx = 1;
        gbc1.gridy = 3;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.anchor = GridBagConstraints.EAST;
        add(isbn_text,gbc1);

        new_lbl = new JLabel("New Book :");
        gbc1.gridx = 0;
        gbc1.gridy = 4;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.anchor = GridBagConstraints.WEST;
        add(new_lbl,gbc1);

        new_text = new JTextField(15);
        gbc1.gridx = 1;
        gbc1.gridy = 4;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.anchor = GridBagConstraints.EAST;
        add(new_text,gbc1);

        available_lbl = new JLabel("Available Book :");
        gbc1.gridx = 0;
        gbc1.gridy = 5;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.anchor = GridBagConstraints.WEST;
        add(available_lbl,gbc1);

        available_text = new JTextField(15);
                available_text.setEditable(false);
        gbc1.gridx = 1;
        gbc1.gridy = 5;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.anchor = GridBagConstraints.EAST;
        add(available_text,gbc1);

        total_book_lbl = new JLabel("Total Book :");
        gbc1.gridx = 0;
        gbc1.gridy = 6;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.anchor = GridBagConstraints.WEST;
        add(total_book_lbl,gbc1);

        total_book_text = new JTextField(15);
                total_book_text.setEditable(false);
        gbc1.gridx = 1;
        gbc1.gridy = 6;
        gbc1.ipadx = 0;
        gbc1.ipady = 0;
        gbc1.anchor = GridBagConstraints.EAST;
        add(total_book_text,gbc1);

        submit_btn = new JButton("Submit");
                //submit_btn.setEnabled(false);
        gbc1.gridx = 0;
        gbc1.gridy = 7;
        gbc1.ipadx = 20;
        gbc1.ipady = 0;
        gbc1.anchor = GridBagConstraints.WEST;
        add(submit_btn,gbc1);

        exit_btn = new JButton("Reset");
        gbc1.gridx = 1;
        gbc1.gridy = 7;
        gbc1.ipadx = 20;
        gbc1.ipady = 0;
        gbc1.anchor = GridBagConstraints.CENTER;
        gbc1.fill = GridBagConstraints.NONE;
        add(exit_btn,gbc1);

    }
    public void actionPerformed(ActionEvent e)
    {
        if((JComboBox) e.getSource()==book_name_box)
        {
                        try
                        {
                            while(author_box.getItemCount()!=0)
                            {
                author_box.removeItemAt(0);
                            }
                            author_box.addItem("Select Author");
                            book=(String) book_name_box.getSelectedItem();
                          if(!(book.equals("")) || !(book.equals("Select book")))
                            {

                                    psmtp = conn.prepareStatement("select AUTHOR_NAME from BOOK_TABLE where BOOK_NAME=?");
                                    psmtp.setString(0,book);
                                    rs=psmtp.executeQuery();
                                    while(rs.next())
                                    {
                                            String auther_name = rs.getString("AUTHOR_NAME").toString();
                                            author_box.addItem(auther_name);
                                    }
                            }
                            else
                            {
                                    isbn_text.setText("");
                                    new_text.setText("");
                                    available_text.setText("");
                                    total_book_text.setText("");
                                    //submit_btn.setEnabled(false);
                            }
                        }
                        catch(SQLException se)
                        {

                        }
        }
                else if((JComboBox) e.getSource()==author_box)
                {
                        try
                        {
                            auther=(String) author_box.getSelectedItem();
                          if(!(auther.equals("")) || auther!=null || !(auther.equals("Select Author")))
                            {
                                String query = "select ISBN, AVAILABLE_BOOK, TOTAL_BOOK from BOOK_TABLE where BOOK_NAME = ? and AUTHOR_NAME = ?";
                                psmtp=conn.prepareStatement(query);
                                psmtp.setString(1,book);
                                psmtp.setString(2, auther);
                                rs = psmtp.executeQuery();
                                while(rs.next())
                                {
                                    String isbn = rs.getString("ISBN").toString();
                                    int ava_book = rs.getInt("AVAILABLE_BOOK");
                                    int total_book = rs.getInt("TOTAL_BOOK");
                                    isbn_text.setText(isbn);
                                    available_text.setText(ava_book+"");
                                    total_book_text.setText(total_book+"");
                                }
                            }
                            else
                            {
                                System.out.println("3");
                                    isbn_text.setText("");
                                    new_text.setText("");
                                    available_text.setText("");
                                    total_book_text.setText("");
                            }
                        }
                        catch(SQLException se)
                        {

                        }
        }
    }
}

异常

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at AddBook.actionPerformed(AddBook.java:233)
        at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1242)
        at javax.swing.JComboBox.contentsChanged(JComboBox.java:1313)
        at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:100)
        at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:88)
        at javax.swing.DefaultComboBoxModel.removeElementAt(DefaultComboBoxModel.java:140)
        at javax.swing.JComboBox.removeItemAt(JComboBox.java:741)
        at AddBook.actionPerformed(AddBook.java:198)
        at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1242)
        at javax.swing.JComboBox.setSelectedItem(JComboBox.java:569)
        at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:605)
        at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:814)
        at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:273)
        at java.awt.Component.processMouseEvent(Component.java:6041)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
        at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(BasicComboPopup.java:480)
        at java.awt.Component.processEvent(Component.java:5806)
        at java.awt.Container.processEvent(Container.java:2058)
        at java.awt.Component.dispatchEventImpl(Component.java:4413)
        at java.awt.Container.dispatchEventImpl(Container.java:2116)
        at java.awt.Component.dispatchEvent(Component.java:4243)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
        at java.awt.Container.dispatchEventImpl(Container.java:2102)
        at java.awt.Window.dispatchEventImpl(Window.java:2440)
        at java.awt.Component.dispatchEvent(Component.java:4243)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

我想使用数据库。首先从数据库插入组合框项目,然后我从 book_name_box 中选择值,然后在 author_box 中插入值,然后我选择 auther name,然后在其他字段中显示值,如 isbn_text、available_text、new_text、total_book_text

请告诉我如何解决。

最佳答案

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at AddBook.itemStateChanged(AddBook.java:235)

鉴于跟踪涉及:

if(!(auther.equals("")) || !(auther.equals("Select Author")))

我希望它是 auther,即 null。 (顺便说一下,正确的拼写是 author。)

对此有两种可能的方法。

首先要意识到 ItemEvent 可能与 ItemEvent.DESELECTED 有关event (the component will fire multiple events when the selection changes from one item to another).当前代码可能:

  1. 检查 SELECTED 事件,否则中止。
  2. 在同一代码行中的其他检查之前检查 auther==null,如果 null,则中止。

    if(auther!=null && !(auther.equals("")) || !(auther.equals("Select Author")))
    

但更好的整体方法 (IMO) 是使用仅在选择项目时触发的监听器。在这种情况下,请参阅 JComboBox.addActionListener(ActionListener) .

关于java - JComboBox:如何解决 JComboBox 和 ItemListener 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30870000/

相关文章:

java - 如何刷新 Java 小程序?

java - 在 Java 中最大化 JInternalFrame

java - 为什么 libgdx SpriteBatch/BitmapFont 会破坏 Texture.bind?

java - Antlr4 : Evaluate math functions f(x)

java - 无法从java应用程序访问mysql数据库

database - 安装后在 AWS 环境中启动 Mongodb

java - Thread.sleep() 停止我的绘画?

启动画面启动时出现 java.lang.UnsupportedOperationException

java - 绘制到 Canvas - 不在其他项目上绘制

javascript - 如何在MongoDB中查询具有特定条件的数组