0

我正在尝试使用 swing 事件处理来访问数据库表记录,但是在按下按钮后它显示异常。我不知道出了什么问题。这是我的代码:

private void DisplayAllButtonActionPerformed(java.awt.event.Act ionEvent evt) {

    try { 

        String SQL = "SELECT * FROM OCCUPANTS";
        rs = stmt.executeQuery( SQL ); //line no 264

        while( rs.next( )) {
            int id_col = rs.getInt("ID");
            String id=Integer.toString(id_col);
            String room =rs.getString("ROOM");
            String occupant = rs.getString("OCCUPANT");

            DisplayArea.setText( id + " " + room + " " + occupant);
        }
   } 
   catch ( SQLException err ) {
        System.out.println( err.getMessage( ) );
   }
}

休闲是个例外:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at wumpus.WumpusGUI.DisplayAllButtonActionPerformed(W umpusGUI.java:264)
at wumpus.WumpusGUI.access$300(WumpusGUI.java:16)
at wumpus.WumpusGUI$4.actionPerformed(WumpusGUI.java: 164)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.jav a:6505)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
at java.awt.Component.processEvent(Component.java:627 0)
at java.awt.Container.processEvent(Container.java:222 9)
at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
at java.awt.Component.dispatchEvent(Component.java:46 87)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719 )
at java.awt.Component.dispatchEvent(Component.java:46 87)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:703)
at java.awt.EventQueue.access$000(EventQueue.java:102 )
at java.awt.EventQueue$3.run(EventQueue.java:662)
at java.awt.EventQueue$3.run(EventQueue.java:660)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:676)
at java.awt.EventQueue$4.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 673)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:97)
BUILD SUCCESSFUL (total time: 28 seconds)

这是全局初始化stmt

public class WumpusGUI extends javax.swing.JFrame {

    Connection con;
    Statement stmt;
    ResultSet rs;
    /**
     * Creates new form WumpusGUI
     */
    public WumpusGUI() {
        initComponents();
        DBConnect();
    }

    public void DBConnect() {
        try {
            String host = "jdbc:derby://localhost:1527/occupants";
            String uName = "ravi";
            String uPass= "ravi";
            Connection con = DriverManager.getConnection( host, uName, uPass );
            Statement stmt = con.createStatement();
        }  
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

谢谢。

4

4 回答 4

5

好吧,您还没有显示stmt声明的位置,但听起来它可能为空。您期望它在哪里设置为非空值?

关于这个有两点:

  • PreparedStatement每次执行查询时都应该使用单独的
  • 无论如何,您不应该在 UI 线程中执行数据库访问

(此外,您的方法名称违反了 Java 命名约定。)

编辑:从您的评论中,您得到:

public class WumpusGUI ... {
    Statement stmt;

    public void DBConnect() {
        ...
        Statement stmt = con.createStatement();
        ...
    }
}

你方法的那一行声明了一个新的局部变量——它没有为实例变量赋值,所以实例变量保持为空。

最小的“修复”是将该行更改为:

stmt = con.createStatement();

...但正如我上面所说,这不是正确的解决方法。理想情况下,您应该从GUI 类中获取所有这些数据库代码,但至少每次要执行它时都应该创建一个新语句。(然后在 finally 块中关闭它。)

编辑:此外,如果您在方法中遇到异常,您只是将其打印出来并愉快地继续......所以您的stmt变量仍然为空。不要捕获Exception一般情况,也不要捕获您之后真的无法继续的异常。

于 2012-08-01T09:49:29.193 回答
2

很简单:stmt还没有初始化。

于 2012-08-01T09:49:39.370 回答
0

您的 stamt 变量可能已使用 null 全局初始化,但您无法分配必须从连接中获取的值。检查您的代码。

于 2012-08-01T09:56:05.423 回答
0

在使用它之前,您必须调用 DBConnect 方法来初始化 stmt 变量。

于 2012-08-01T10:06:19.920 回答