2016-01-21 44 views
0

我一直在嘗試了幾個小時將數據插入到JComboBox中及其frustates添加項目到我的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) 

香港專業教育學院嘗試了幾個小時,請幫我在代碼中找出錯誤

+0

哪一行是'StaffEditDelete.java:85'?我想,Satya的答案會完成這項工作,但是看這個問題的人需要知道哪一行是第85行? –

回答

0

顯示java.lang.NullPointerException

在StaffEditDelete。(StaffEditDelete.java:85)

您使用初始化comboBox對象後JComboBox。所以這個陳述應該在你使用這個對象的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這裏。

+0

感謝您的幫助 它的工作 和btw con是連接 –