我无法从我的数据库中添加项目到我的jcombobox

时间:2016-01-21 06:54:21

标签: java eclipse jcombobox

我已经花了好几个小时将数据插入jcombobox并且让我感到沮丧 有人可以帮我找到我的错误

这是我的代码

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;

import java.awt.GridLayout;
import java.awt.Font;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.JComboBox;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class StaffEditDelete extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_4;
    private JTextField textField_5;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    StaffEditDelete frame = new StaffEditDelete();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */

    Connect con;

//  String []alpha;
    Vector<String> alpha;
    JComboBox comboBox;

    public StaffEditDelete() {
        setTitle("Staff");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 450, 424);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.NORTH);

        JLabel lblStaff = new JLabel("Staff");
        lblStaff.setFont(new Font("Times New Roman", Font.BOLD, 24));
        panel.add(lblStaff);

        JPanel panel_1 = new JPanel();
        contentPane.add(panel_1, BorderLayout.CENTER);
        panel_1.setLayout(new GridLayout(7, 2, 10, 10));

        JLabel lblNewLabel = new JLabel("ID");
        panel_1.add(lblNewLabel);
        con.rs = con.executeQuery("Select * from Detail");
        try {
            while(con.rs.next())
            {
            //  System.out.println("True");
                comboBox.addItem(con.rs.getString(1));
            }
        } catch (Exception e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

        comboBox = new JComboBox();
        comboBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) 
            {
                String items = e.getSource().toString();
                con.rs = con.executeQuery("select * from Staff where [ID Staff ='"+items+"']");
                try {
                    textField.setText(con.rs.getString(2));
                    textField_1.setText(con.rs.getString(3));
                    textField_2.setText(con.rs.getString(4));
                    textField_3.setText(con.rs.getString(5));
                    textField_4.setText(con.rs.getString(6));
                    textField_5.setText(con.rs.getString(7));
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });
        panel_1.add(comboBox);

        JLabel lblNewLabel_1 = new JLabel("Nama");
        panel_1.add(lblNewLabel_1);

        textField = new JTextField();
        panel_1.add(textField);
        textField.setColumns(10);

        JLabel lblNewLabel_2 = new JLabel("Alamat");
        panel_1.add(lblNewLabel_2);

        textField_1 = new JTextField();
        panel_1.add(textField_1);
        textField_1.setColumns(10);

        JLabel lblNewLabel_3 = new JLabel("Nomor Telepon");
        panel_1.add(lblNewLabel_3);

        textField_2 = new JTextField();
        panel_1.add(textField_2);
        textField_2.setColumns(10);

        JLabel lblNewLabel_4 = new JLabel("Jabatan");
        panel_1.add(lblNewLabel_4);

        textField_3 = new JTextField();
        panel_1.add(textField_3);
        textField_3.setColumns(10);

        JLabel lblNewLabel_5 = new JLabel("Password");
        panel_1.add(lblNewLabel_5);

        textField_4 = new JTextField();
        panel_1.add(textField_4);
        textField_4.setColumns(10);

        JLabel lblNewLabel_6 = new JLabel("Gaji");
        panel_1.add(lblNewLabel_6);

        textField_5 = new JTextField();
        panel_1.add(textField_5);
        textField_5.setColumns(10);

        JPanel panel_2 = new JPanel();
        contentPane.add(panel_2, BorderLayout.SOUTH);

        JButton btnEdit = new JButton("Edit");
        btnEdit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (cekTelpon()) 
                {
                    int reply = JOptionPane.showConfirmDialog(null, "Edit "+comboBox.getSelectedItem().toString()+"?","Edit?",JOptionPane.YES_NO_OPTION);
                    if (reply == JOptionPane.YES_OPTION) 
                    {
                        con.executeUpdateStaff(comboBox.getSelectedItem().toString(), textField.getText(), textField_1.getText(), textField_2.getText(), textField_3.getText(), textField_4.getText(), textField_5.getText());
                        JOptionPane.showMessageDialog(null, "Data Berhasil di Edit");

                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Nomor Telepon Harus Angka", "ERROR", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        panel_2.add(btnEdit);

        JButton btnDelete = new JButton("Delete");
        btnDelete.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int reply = JOptionPane.showConfirmDialog(null, "Delete "+comboBox.getSelectedItem().toString()+"?","Delete?",JOptionPane.YES_NO_OPTION);
                if(reply == JOptionPane.YES_OPTION)
                {
                    con.executeDeleteStaff(comboBox.getSelectedItem().toString());
                    JOptionPane.showMessageDialog(null, "Data Berhasil di Delete");

                }
            }
        });
        panel_2.add(btnDelete);

        JButton btnReset = new JButton("Reset");
        btnReset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                comboBox.setSelectedIndex(0);
                con.rs = con.executeQuery("Select * from Staff where [ID Staff] = '"+ comboBox.getSelectedItem().toString()+"'");

                try {
                    textField.setText(con.rs.getString(2));
                    textField_1.setText(con.rs.getString(3));
                    textField_2.setText(con.rs.getString(4));
                    textField_3.setText(con.rs.getString(5));
                    textField_4.setText(con.rs.getString(6));
                    textField_5.setText(con.rs.getString(7));

                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }


            }
        });
        panel_2.add(btnReset);

        JButton btnCancel = new JButton("Cancel");
        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        panel_2.add(btnCancel);
    }


    public boolean cekTelpon()
    {
        int flag = 0;
        for (int i =0 ;i < textField_2.getText().length();i++)
        {
            if (textField_2.getText().charAt(i) == '0') 
            {
                flag ++;
            }
            if (textField_2.getText().charAt(i) == '1') 
            {
                flag ++;
            }
            if (textField_2.getText().charAt(i) == '2') 
            {
                flag ++;
            }
            if (textField_2.getText().charAt(i) == '3') 
            {
                flag ++;
            }
            if (textField_2.getText().charAt(i) == '4') 
            {
                flag ++;
            }
            if (textField_2.getText().charAt(i) == '5') 
            {
                flag ++;
            }
            if (textField_2.getText().charAt(i) == '6') 
            {
                flag ++;
            }
            if (textField_2.getText().charAt(i) == '7') 
            {
                flag ++;
            }
            if (textField_2.getText().charAt(i) == '8') 
            {
                flag ++;
            }
            if (textField_2.getText().charAt(i) == '9') 
            {
                flag ++;
            }
        }

        if (flag == 0) 
        {
            return true;
        } else 
        {
            return false;
        }
    }

}

这是我的错误

java.lang.NullPointerException

at StaffEditDelete.<init>(StaffEditDelete.java:85)

at StaffEditDelete$1.run(StaffEditDelete.java:44)

at java.awt.event.InvocationEvent.dispatch(Unknown Source)

at java.awt.EventQueue.dispatchEventImpl(Unknown Source)

at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)


at java.security.AccessController.doPrivileged(Native Method)


at java.security.AccessControlContext$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)

我已经尝试了几个小时,请帮我找到代码中的错误

1 个答案:

答案 0 :(得分:0)

  

显示java.lang.NullPointerException

     

在StaffEditDelete。(StaffEditDelete.java:85)

您正在使用JComboBox对象后初始化comboBox。所以这个陈述应该在你正在使用这个对象的while循环之前。

 comboBox = new JComboBox();
 //should be before this block
 try {
        while(con.rs.next())
        {
        //  System.out.println("True");
        //here you are using comboBox. so it should be initialized before this statement.
            comboBox.addItem(con.rs.getString(1));
        }
    } catch (Exception e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }//not after this block

还有一个问题是con对象尚未创建。所以也请检查一下。

请看一下这句话。

  con.rs = con.executeQuery("Select * from Detail");

此处conrs是什么。