JComboBox: How to solve exception in JComboBox and ItemListener

193 views Asked by At
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

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)

I want to use database. First combobox item insert from databse than i select value from book_name_box then value insert in author_box then i select auther name then value show in other field like isbn_text, available_text, new_text, total_book_text

please tell me how to solve it.

1

There are 1 answers

0
Andrew Thompson On BEST ANSWER
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at AddBook.itemStateChanged(AddBook.java:235)

Given that trace relates to:

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

I would expect it is auther that is null. (By the way, the correct spelling is author.)

There are two potential approaches to this.

One starts with realizing that the ItemEvent might relate to an ItemEvent.DESELECTED event (the component will fire multiple events when the selection changes from one item to another). The current code might:

  1. Check for a SELECTED event and abort otherwise.
  2. Check for auther==null before the other checks in the same code line, and if null, abort.

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

But a better overall approach (IMO) is to use a listener that will only fire when an item is selected. In that case, see JComboBox.addActionListener(ActionListener).