-3

为什么这段代码会导致 NullPointerException?

ArrayList<OrdersAttr> ls = new ArrayList<OrdersAttr>();
OrdersAttr myOrder = null;
String connString = ConnStr.connString;
String connString2 = ConnStr.connString2;

Connection conn = null;
Connection conn2 = null;
Statement stmnt = null;
Statement stmnt2 = null;
String selectString = null;
String insertString = null;
String insertString2 = null;
String updateString = null;
int orderId = 0;

conn = DriverManager.getConnection(connString);
stmnt = conn.createStatement();

selectString = "SELECT A.ORDERID AS ORDERID FROM APP.ORDERS a, APP.CUSTOMER b WHERE ORDERSTATUS = 'NEW ORDER' AND " + " branchid=" + branchid + "and a.CUSTEMAIL = b.EMAIL";

ResultSet rs = stmnt.executeQuery(selectString);
while (rs.next()) {
    orderId = rs.getInt("ORDERID");
    myOrder = new OrdersAttr();
    myOrder.id = rs.getInt("ORDERID");
    myOrder.date = rs.getString("datetime");
    myOrder.orderitems = rs.getString("orderitems");
    myOrder.orderquantity = rs.getString("orderquantity");
    myOrder.ordersizes = rs.getString("ordersizes");
    myOrder.address = rs.getString("floor_f") + " "
        + rs.getString("building_f") + " "
        + rs.getString("street_f") + " "
        + rs.getString("area_subdivision_district_f") + " "
        + rs.getString("city_f");
    myOrder.name = rs.getString("firstname") + " " + rs.getString("lastname");
    myOrder.status = rs.getString("ORDERSTATUS");
    myOrder.contact = rs.getString("CONTACT");

    conn2 = DriverManager.getConnection(connString2);
    stmnt2 = conn2.createStatement();

    insertString = "INSERT INTO APP.ORDERS VALUES ('" + myOrder.id + "','" + myOrder.date + "','" + myOrder.orderitems + "','" + myOrder.orderquantity + "','" + myOrder.ordersizes + "','" + myOrder.status + "')";
    insertString2 = "INSERT INTO APP.CUSTOMER VALUES ('" + myOrder.id + "','" + myOrder.name + "','" + myOrder.address + "','" + myOrder.contact + "')";

    stmnt2.executeUpdate(insertString);
    stmnt2.executeUpdate(insertString2);

    updateString = "UPDATE APP.ORDERS SET ORDERSTATUS = 'SENT TO LOCAL BRANCH'";
    stmnt.executeUpdate(updateString);
}

UPDATE String 被执行并更新数据库,但为什么它返回 NullPointerException 并且不执行 INSERT 语句?有人可以帮我解决这个问题。

4

2 回答 2

1

由于您无法获得堆栈跟踪(无法访问服务器日志?)您所能做的就是对每个可能的项目进行空检查。

例如

conn = DriverManager.getConnection(connString);
stmnt = conn.createStatement();
// Without having DriveManager source, how do you know conn can't be null?
// This is potentially unsafe.



ResultSet rs = stmnt.executeQuery(selectString);
while (rs.next()) {
// Could rs be null (e.g. if the select string had an SQL syntax problem)
于 2012-06-21T02:12:50.013 回答
0

如果没有堆栈跟踪或可编译代码,或者至少告诉我们崩溃发生在哪一行,您将无法获得直接答案。

但是,如果您这样做,您可能会弄清楚:

  1. 摆脱所有的= null。
  2. 将所有变量更改为最终变量。

这两件事将迫使您在使用它们之前分配变量,这意味着您不会意外忘记分配一个并将其与您现在提供的空值一起使用。

这可能意味着您需要添加一些新变量,最终变量只能分配一次。这通常是一件明智的事情——一个变量,一个目的。不要仅仅因为它们相似就重用变量。

此外,请确保您不要在 catch 中没有任何内容的情况下执行 try/catch,然后在 catch 之后有代码(您的代码看起来不是这种情况)。

这样做应该可以帮助您缩小问题范围,甚至可以解决它。

于 2012-06-21T02:11:37.557 回答