createStatement上的Java NullPointerException

时间:2013-01-19 04:46:18

标签: java nullpointerexception

  

可能重复:
  What can I do to avoid a NullPointerException?

我正在为餐馆开发一个应用程序。当我尝试初始化某个类时发生错误。这是代码。

public class DbItem {

private static Connection conn = null;

static {
    conn = DBConnection.connect();
}
public static ArrayList<Item> fetchAll() {
    ArrayList<Item> items = new ArrayList<>();
    String sql = "select * from itens";
    try {
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery(sql);
        while(rs.next()) {
            items.add(new Item(rs.getString("nome"), rs.getString("descricao"), rs.getFloat("preco")));
        }
        return items;
    } catch (SQLException ex) {
        Logger.getLogger(DbItem.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}
}

连接取自此类

public class DBConnection {

private static Connection conn;

public static Connection connect() {
    if(conn == null) {
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "restaurante_comandas_jonnathan";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "root";
        String password = "root";
        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url + dbName, userName, password);
            System.out.println("Connected to " + dbName + " with " + userName);
            JOptionPane.showMessageDialog(null, conn == null);
            return conn;
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) {
            e.printStackTrace();
        }
    }
    return null;
}

}

我尝试使用弹出框进行调试。我在return命令之前检查了连接是否为null,并且发现不是。然后,我在第一个脚本调用后检查,发现它确实是null。现在,如果一个对象在返回之前不是在返回之前如何才能为null?

编辑:

在这里。堆栈跟踪

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at model.data.DbItem.fetchAll(DbItem.java:48)
at model.Item.getAll(Item.java:76)
at view.PedidoForm.<init>(PedidoForm.java:22)
at view.Login$2.run(Login.java:99)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

1 个答案:

答案 0 :(得分:0)

我修改了您的代码并进行了一些更改。请试试这个

public class DBConnection {

private static Connection conn;

public static Connection connect() {
    if(conn == null) {
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "restaurante_comandas_jonnathan";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "root";
    try {
        Class.forName(driver).newInstance();
        conn = DriverManager.getConnection(url + dbName, userName, password);
        System.out.println("Connected to " + dbName + " with " + userName);
        JOptionPane.showMessageDialog(null, conn == null);            
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) {
        e.printStackTrace();
    }
}
return conn;
}

}