-1

可能重复:
我能做些什么来避免 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;
}

}

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

编辑:

这里是。堆栈跟踪

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)
4

1 回答 1

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;
}

}

于 2013-01-19T07:27:38.560 回答