Hi Guys,

I've got a problem where as I attempt to output, i get thrown a Null Pointer Exception.
I can read the values for orders[0] but nothing after. The DB is intact, and i get thrown the results as per the query generated in my code, so thats not the problem.

Any input would be great,
thanks!!

orders = new orderDetails[numOrders];
//orderID   CCF   phoneNumber   phoneType   IMEI   simNumber   pricePlan   MRO   orderDate
int i = 0;
while(res.next()){
                   
    orders[i] = new orderDetails();
                    
    orders[i].setOrderID(res.getInt("orderID"));
    orders[i].setCCF( res.getInt("CCF") );
    orders[i].setPhoneNum( res.getInt("phoneNumber"));
    orders[i].setPhoneType( res.getString("phoneType") );
    orders[i].setIMEI( res.getInt("IMEI"));
    orders[i].setSimNum( res.getInt("simNumber"));
    orders[i].setPlanPrice( res.getDouble("pricePlan"));
    orders[i].setMRO( res.getDouble("MRO"));
    orders[i].setOrderDate(res.getDate("orderDate"));
                    
    i++;
}

Recommended Answers

All 14 Replies

i get thrown a Null Pointer Exception.

What line is the error occurring on?
What variable is null?
What is the value of i when the error occurs?

What line is the error occurring on?
What variable is null?
What is the value of i when the error occurs?

It doesnt say which line the error is occuring, with regard to the class.
All of the variables are returned null pointers
Value of i is anything above 0 when the error actually occurs;

org.apache.jasper.JasperException: An exception occurred processing JSP page /viewClient.jsp at line 18

14.        orderDetails[] order = new orderDetails().getOrderDetails(clientID);
15.        //String order = new orderDetails().getOrderDetails(clientID);
16.
17.        if(order.length > 0){
18.            out.println(order[1].getPhoneType());
19.        }
17.        if(order.length > 0){
18.            out.println(order[1].getPhoneType());
19.        }

If order is of length 1 this code will always give an NPE (arrays are zero-based)

Was it intended to be a loop?, as in

for (int i = 0; i < order.length; i++ ) {
   out.println(order[i].getPhoneType());
}
17.        if(order.length > 0){
18.            out.println(order[1].getPhoneType());
19.        }

If order is of length 1 this code will always give an NPE (arrays are zero-based)

Was it intended to be a loop?, as in

for (int i = 0; i < order.length; i++ ) {
   out.println(order[i].getPhoneType());
}

I know for a fact that order.length in this case is 3. I know that all arays are zero based haha. I do have a loop below, im just using the if statement for initial testing to see if it were just that instance of orderDetails that was failing.

Given that i know that the order.length is 3, i should read through 0,1,2. However, it reads through 0, then returns the NPE for 1 and 2.

I'm clueless as to why this is happening. All the values for orders[0] return as they should, but not when the counter goes above 0.

ALSO, I ran the mem. function for a string return, and ran the query returned into my SQL CLI, and got returned all 3 rows as expected, so it is not an issue with the query returning 1 value, and me making a mistake and hard-coding in the values (all values are dynamic as should always be lol)

Sounds like the loop in your first posted code is only being executed once. Have you tried a print in that loop to see how many times it's executed?

Sounds like the loop in your first posted code is only being executed once. Have you tried a print in that loop to see how many times it's executed?

Hmmm, no i hadn't. Though now that I have, I'm VERY confused...
The loop isn't iterating through past int i = 0;

So it's creating the first instance of orderDetails, but nothing past that.
I get thrown back that there are 3 rows returned in my ResultSet, but its not iterating through them all..

- Could this be an issue with the scope of i?
If not, any thoughts why this could be happening?

I can't make any sensible comment because I can't see any of the code leading up to that loop. At least you now have a ckue as to where the problem lies.

I can't make any sensible comment because I can't see any of the code leading up to that loop. At least you now have a ckue as to where the problem lies.

I've included the code below haha; i've read through it line by line, and nothing that i can see is wrong.

public int getOrderDetails(int clientID){
        Connection con = null;
        Statement stmt = null;

        String sql = "";
        int i = 0;

        try{
            con = new database().getConnection();
            stmt = con.createStatement();

            try{
                sql = "SELECT * FROM `orderview` LEFT JOIN `clientsorders` USING(`orderID`) WHERE `clientsorders`.`clientID` = "+clientID;
                ResultSet res = stmt.executeQuery(sql);

                int numOrders = 0;
                while(res.next()){ numOrders++; }

                orders = new orderDetails[numOrders];
                res.beforeFirst();
                
                //orderID	CCF	phoneNumber	phoneType	IMEI	simNumber	pricePlan	MRO	orderDate
                while(res.next()){
                    orders[i] = new orderDetails();                          // Creates a new instance of orderDetails
                    
                    orders[i].setOrderID( res.getInt("orderID") );            // Sets the Order ID
                    orders[i].setCCF( res.getInt("CCF") );                   // Sets the CCF Identifier
                    orders[i].setPhoneNum( res.getInt("phoneNumber") );      // Sets the associated phone number
                    orders[i].setPhoneType( res.getString("phoneType") );    // Sets the associated phone type
                    orders[i].setIMEI( res.getInt("IMEI") );                 // Sets the IMEI Number
                    orders[i].setSimNum( res.getInt("simNumber") );          // Sets the SIM Number
                    orders[i].setPlanPrice( res.getDouble("pricePlan") );    // Sets the price plan
                    orders[i].setMRO( res.getDouble("MRO") );                // Sets the Monthly Repayment Option (MRO)
                    orders[i].setOrderDate(res.getDate("orderDate") );       // Sets the date of ordered.
                    
                    i++;                                                     // Increment counter
                }
                

            }catch(SQLException e){ e.printStackTrace(); }

            if(con != null){ con.close(); }
            if(stmt != null){ stmt.close(); }
            
        }catch(SQLException e){ e.printStackTrace(); }

        
        return i;             // RETURNS 0 WHEN SHOULD RETURN 2
    }

Can't see anything yet - still looking.
BTW: Is the record that you get the first or the last record?

Also: confused by your last post: if the returned value is 0 that implies the last line of loop is never executed. Yet the presence of (some?) data in array[0] implies the rest of the loop is executed once.

Is it possible that any of the set methods are throwing a runtime exception?
Try putting a print at lines 24 and 35 to be certain of what's going on.

A default ResultSet object ... has a cursor that moves forward only.

- which is incompatible with beforeFirst(). (although you should have got an SQL exception if that was the problem)

Hey guys,

I've gone through to check which line was returning an error, and it seems as though the following line is causing a run-time error.

// Sets the IMEI Number
orders[x].setIMEI( res.getInt("IMEI") );

However, I still can't tell why it is throwing back the RTE. This one has me stumped. The variable, getter and setter declarations are all fine, so its a mystery as to why it isn't processing it as it should.

When running the SQL Query in my CLI, it works fine and returns the IMEI number as expected.

Any thoughts as to why it would be throwing back the RTE?

guys, i think that i may have figured out why this isn't working. And it is SIMPLE!

Having declared this as an int(15) in my table, I checked the range of an unsigned int, and it max's out at '4294967295' so once i changed it to an unsigned bigint its problem solved =D

Thanks to those whose input was instrumental to reaching this point.

As a matter of interest - why didn't this generate an exception that got reported? Do you have any empty catch(Exeception e) {} clauses anywhere?

hey james,

I'm not too sure why this didn't generate an exception. I don't have any empty catch exceptions anywhere which is weird. To be honest, the only reason I realized that it was my variable type declaration was because I happened to look across at my DB table, and notice that the IMEI values were all the same where they should be unique, which triggered the subsequent thoughts.

I wish I had the answer though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.