0

次のコードを使用して各列を並べ替えることができるインベントリ内の化学物質のリストの jtable を作成しました (chemicalTable は jTable の名前です)。

chemicalTable.setAutoCreateRowSorter(true);
TableRowSorter<TableModel> sorter1 
      = new TableRowSorter<TableModel>(chemicalTable.getModel());
chemicalTable.setRowSorter(sorter1);

次に、ユーザーが特定の文字を入力するたびにテーブルが更新されるように、jTextfield を使用して keyTyped Listener を含む検索ボックスを作成しました。そして、それは通常機能します。

検索ボックスの keyTypedListener でこのコードを使用しました。

DefaultTableModel dm = (DefaultTableModel) chemicalTable.getModel();

str = searchChemicalText.getText();

        try {
            String url = "jdbc:mysql://localhost/chemical inventory";
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(url, "root", "");


        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error Occurred.", "Error", JOptionPane.ERROR_MESSAGE);
        }

        int ctr = 0;


        while (ctr < chemicalTable.getRowCount()) {
            chemicalTable.getModel().setValueAt(null, ctr, 0);
            chemicalTable.getModel().setValueAt(null, ctr, 1);
            chemicalTable.getModel().setValueAt(null, ctr, 2);
            ctr++;

        }

        int count = 0;

        try {
            Statement stmt = conn.createStatement();
            String query = "Select * FROM chemicallist where name_of_reagent like '%" + str + "%'";
            ResultSet rs = stmt.executeQuery(query);

            if (rs.next()) {
                rs = stmt.executeQuery(query);

                while (rs.next()) {
                    String qty = null, qtyunit = null, chemstate = null, reagentName = null;

                    reagentName = rs.getString("name_of_reagent");
                    qty = rs.getString("quantity");
                    qtyunit = rs.getString("quantity_unit");
                    chemstate = rs.getString("state");
                    chemicalTable.getModel().setValueAt(reagentName, count, 0);
                    chemicalTable.getModel().setValueAt(qty + " " + qtyunit, count, 1);
                    chemicalTable.getModel().setValueAt(chemstate, count, 2);



                    if (count + 1 > chemicalTable.getRowCount() - 1) {
                        dm.setRowCount(chemicalTable.getRowCount() + 1);
                        dm.fireTableRowsInserted(0, 5);


                    }
                    count++;


                }
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error Occurred.", "Error", JOptionPane.ERROR_MESSAGE);


        }

私の問題は次のとおりです。最初に列 (col1、col2、または col3) をソートし、検索ボックスに文字を挿入すると、次のエラー メッセージが表示されます。

「イベントディスパッチ中に例外が発生しました: Java.lang.NullPointerException」

4

1 に答える 1

2

コード フラグメントをデバッグすることはできませんが、いくつかの点が際立っています。

  • 同じセクションで、TableModelasdmchemicalTable.getModel();を参照します。単一の参照を使用するか、それらが同じインスタンスを参照していることを確認してください。

  • をいじる代わりにsetRowCount()、いずれかのaddRow()方法を使用してください。

  • モデルを変更するDefaultTableModelメソッドが正しいイベントを発生させ、それに応じてテーブルが自動的に更新されます。これを自分で行う必要はありません。

于 2013-05-22T09:25:39.993 に答える