-3

Why does this code cause a 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);
}

The UPDATE String is executed and it updates the database, but why does it return a NullPointerException and doesn't execute the INSERT statements? Could someone please help me with this problem.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
Arjel
  • 469
  • 2
  • 10
  • 16

2 Answers2

1

Since you can't get a stack trace (no access to server logs?) all you can do is null check every possible item.

e.g.

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)
John3136
  • 28,809
  • 4
  • 51
  • 69
0

Without the stack trace, or compilable code, or at least telling us what line the crash happens in you won't get a direct answer.

However if you do this you can probably figure it out:

  1. get rid of all of the = null.
  2. change all of your variables to final.

Those two things will force you to assign the variables before you use them, meaning you cannot accidentally forget to assign one and use it with the null value you are giving it now.

That might mean you need to add some new variables, final variables can only be assigned once. That is generally a sane thing to do - one variable, one purpose. Don't reuse variables just because they are similar.

Also, make sure you don't do a try/catch with nothing in the catch and then have code after the catch (doesn't look like that is the case with your code).

Doing that should help you narrow the issue down, it may even fix it.

TofuBeer
  • 60,850
  • 18
  • 118
  • 163