0

当我尝试将列名和行数据添加到表模型时,出现空指针异常。列名来自列表,我将它们作为数组添加到表模型中。但在那里我得到了例外:tableModel.addColumn(columnNames.toArray()); tableModel.addRow(rowData);

我的完整代码:

public class DBC extends JFrame{

static String tablo;
static JTextField tf = new JTextField(20);
static int columnCount;
static JPanel tfPanel = new JPanel();
static String[] sutunlar;
static JLabel sutunLabel;
static JPanel sutunPanel = new JPanel(new BorderLayout());
static JTable table;
static JScrollPane scrollPane;
static DefaultTableModel tableModel;
static Object[] columnNames;

public static void main(String[] args) throws Exception {

    Class.forName("com.mysql.jdbc.Driver");
    Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/project"
              ,"root","123456789");

    final Statement statement = connect.createStatement();

    JLabel tabloSec = new JLabel("Tablo Seçin:");
    final JComboBox<String> tablolar = new JComboBox<String>();
    final DatabaseMetaData md = connect.getMetaData();
    final ResultSet rs = md.getTables(null, null, "%", null);

    while (rs.next()) {
        tablolar.addItem(rs.getString(3));
    }

    tablolar.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent arg0) {

            tablo = tablolar.getSelectedItem().toString();

            try {

                 ResultSet rs2 = statement.executeQuery("SELECT * FROM "+tablo);
                 ResultSetMetaData rsmd = rs2.getMetaData();
                 columnCount = rsmd.getColumnCount();
                 List<String> columnNames = new ArrayList<String>();
                 sutunlar = new String[columnCount];

                 Object rowData[][] = {{""}};


                     for(int i=0;i<columnCount;i++){

                         sutunlar[i] = rsmd.getColumnLabel(i+1);
                         columnNames.add(sutunlar[i]);
                         tableModel.addColumn(columnNames.toArray());
                         tableModel.addRow(rowData);

                         }

                     tableModel = new DefaultTableModel(rowData, columnNames.toArray());
                     table = new JTable();
                     table.setModel(tableModel);
                     table.repaint();
                     scrollPane = new JScrollPane(table);
                     sutunPanel.add(scrollPane);
                     sutunPanel.revalidate();
                     sutunPanel.repaint();


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

    JButton ekle = new JButton("Ekle");
    ekle.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {

            try {
                switch(tablo){
                case "department":

                    statement.executeUpdate("INSERT INTO department(Name,Location) VALUES('"+tf.getText()+"')");
                case "employee":

                    statement.executeUpdate("INSERT INTO employee(Id,FirstName,LastName,Sex,Address,Email,Salary,BirthDate,JoinDate) VALUES('"+tf.getText()+"')");
                case "engineer":

                    statement.executeUpdate("INSERT INTO engineer(EngineerType) VALUES('"+tf.getText()+"')");
                case "manager":

                    statement.executeUpdate("INSERT INTO manager(Department) VALUES('"+tf.getText()+"')");
                case "project":

                    statement.executeUpdate("INSERT INTO project(Name,Number,Value) VALUES('"+tf.getText()+"')");
                case "secretary":

                    statement.executeUpdate("INSERT INTO secretary(TypingSpeed) VALUES('"+tf.getText()+"')");
                }

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

    JButton cik = new JButton("Çık");
    cik.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            System.exit(0);
        }
    });

    JPanel panel = new JPanel(new FlowLayout());

    panel.add(tabloSec);
    panel.add(tablolar);
    panel.add(sutunPanel);
    panel.revalidate();
    panel.add(ekle);
    panel.add(cik);

    JFrame frame = new JFrame("Deneme");
    frame.setSize(600,600);
    frame.setLocationRelativeTo(null);
    frame.add(panel);
    frame.setVisible(true);
  }
 }
4

2 回答 2

2

您需要tableModel在访问它之前进行实例化。

于 2013-03-25T22:23:07.607 回答
2

您尚未在 for 循环之前实例化您的tableModel Move this 代码 。tableModel = new DefaultTableModel(rowData, columnNames.toArray());

于 2013-03-25T22:26:03.140 回答