简体   繁体   中英

Null Pointer Exception in Servlet

I'm having a rather annoying issue with a shopping servlet I'm designing. I keep getting a null pointer exception when I try setting up an order object like this:

           int ccn=customer.getCcn();
            OrderList.Order order=null;
    ResultSet set=helper.selectOrder(custId);
    try{
        while (set.next()){
            orderno=set.getInt(1);
            orderDate=set.getString(2);
            shipDate=set.getString(3);
            custId=set.getString(4);
            order=list.new Order(orderno,custId,ccn);
            list.addOrder(order);

        }
        set.close();}
        catch(Exception e){
            System.out.println(e.getMessage());
        }

But if I do this, the exception goes away... but my list is full of duplicates.

            int ccn=customer.getCcn();
    ResultSet set=helper.selectOrder(custId);
    try{
        while (set.next()){
            orderno=set.getInt(1);
            orderDate=set.getString(2);
            shipDate=set.getString(3);
            custId=set.getString(4);
        }
        set.close();}
                    OrderList.Order order=list.new Order(orderno,custId,ccn);
                    list.addOrder(order);
        catch(Exception e){
            System.out.println(e.getMessage());
        }

Any help?

This is an rough, uneducated (if one can say that) guess, so here goes.

If list is definitely not null, then the easiest way is to add the new order directly in the list like so:

while (set.next()){
    orderno=set.getInt(1);
    orderDate=set.getString(2);
    shipDate=set.getString(3);
    custId=set.getString(4);
    list.addOrder(list.new Order(orderno,custId,ccn));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM