Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Null Pointer Exception when using arrays

843838Mar 6 2006 — edited Mar 6 2006
Hey all,
I can't seem to figure out where this null pointer exception is coming from. I have a Data Access Object that has a getUsers() method that returns an array of user-defined type User. Whenever invoking the method from my jsp it gives me a null pointer exception at the ValidUsers\[i\].setUsername("asdf"); line.

I thought perhaps I wasnt getting a returned row, but the problem error still occurs if I pass it a hard coded string. Here is the code.
public User[] getUsers() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
        
        Connection conn = SQLServerDAOFactory.createConnection();
        PreparedStatement psValidUsers = conn.prepareStatement("Select * from tblUsers");
        ResultSet rsValidUsers = psValidUsers.executeQuery();
        
        User[] ValidUsers = new User[rsValidUsers.getFetchSize()];
        
        int i = 1;
        while (rsValidUsers.next()) {
            ValidUsers.setUsername("asdf");
ValidUsers[i].setPassword(rsValidUsers.getString("Password"));
ValidUsers[i].setFirstname(rsValidUsers.getString("Firstname"));
ValidUsers[i].setLastname(rsValidUsers.getString("Lastname"));
ValidUsers[i].setPhone(rsValidUsers.getString("Phone"));
ValidUsers[i].setExtension(rsValidUsers.getString("Extension"));
ValidUsers[i].setFax(rsValidUsers.getString("Fax"));
ValidUsers[i].setIsActive(rsValidUsers.getBoolean("Active"));
ValidUsers[i].setIsContractor(rsValidUsers.getBoolean("Contractor"));
i++;
}

return ValidUsers;
}

I know that null pointer exceptions are hard to pinpoint cause they happen at runtime, but if you guys have a method of debugging then I'm open for it. I'm not familiar with netbeans debugger.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 3 2006
Added on Mar 6 2006
11 comments
636 views