Null Pointer Exception for simple jdbc code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dev24
    New Member
    • Feb 2007
    • 8

    #1

    Null Pointer Exception for simple jdbc code

    Hi all,

    I am writing a simple method which when entered with an Int parameter performs an sql query, creates a result set, uses that resultset to get values from the object created by another method and display the result as an arraylist. The method and the object are as below:

    public static ArrayList getProjects(int userID)
    throws SQLException, Exception
    {
    ArrayList arrayList = null;

    getProjectsStmt .setInt(1, userID);
    ResultSet rs = getProjectsStmt .executeQuery() ;
    arrayList = new ArrayList();

    try
    {
    while (rs.next())
    {
    Projects p = createProject(r s);
    arrayList.add(p );
    rs.close();

    }
    }
    catch(Exception e)
    {
    System.err.prin tln(e);
    }

    return arrayList;

    }

    //Query is as follows
    private static final String GET_PROJECTS_ST MT = "SELECT projectID, name FROM project WHERE userID = ?";


    //createProject method that creates the Project object is as below

    private static Projects createProject(R esultSet rs)
    throws SQLException, Exception
    {

    int projectID = rs.getInt("proj ectID");
    int userID = rs.getInt("user ID");
    String name = rs.getString("n ame");
    String description = rs.getString("d escription");
    int createdate = rs.getInt("crea tedate");
    boolean visibility = rs.getBoolean(" visibility");

    return new Projects(projec tID, userID, name, description, createdate, visibility);
    }

    When i try and run this method in main to test it, i.e. getProject(1); the entry 1 exists in database and the results of the query also exists but I still get the error saying java.lang.NullP ointerException

    It points to the following lines:

    getProjectsStmt .setInt(1, userID); - line 6 in the getProjects method - above
    getProject(1) - in main where I try to test it

    Any ideas why???

    Thanks,

    dev
  • dev24
    New Member
    • Feb 2007
    • 8

    #2
    Originally posted by dev24
    Hi all,

    I am writing a simple method which when entered with an Int parameter performs an sql query, creates a result set, uses that resultset to get values from the object created by another method and display the result as an arraylist. The method and the object are as below:

    public static ArrayList getProjects(int userID)
    throws SQLException, Exception
    {
    ArrayList arrayList = null;

    getProjectsStmt .setInt(1, userID);
    ResultSet rs = getProjectsStmt .executeQuery() ;
    arrayList = new ArrayList();

    try
    {
    while (rs.next())
    {
    Projects p = createProject(r s);
    arrayList.add(p );
    rs.close();

    }
    }
    catch(Exception e)
    {
    System.err.prin tln(e);
    }

    return arrayList;

    }

    //Query is as follows
    private static final String GET_PROJECTS_ST MT = "SELECT projectID, name FROM project WHERE userID = ?";


    //createProject method that creates the Project object is as below

    private static Projects createProject(R esultSet rs)
    throws SQLException, Exception
    {

    int projectID = rs.getInt("proj ectID");
    int userID = rs.getInt("user ID");
    String name = rs.getString("n ame");
    String description = rs.getString("d escription");
    int createdate = rs.getInt("crea tedate");
    boolean visibility = rs.getBoolean(" visibility");

    return new Projects(projec tID, userID, name, description, createdate, visibility);
    }

    When i try and run this method in main to test it, i.e. getProject(1); the entry 1 exists in database and the results of the query also exists but I still get the error saying java.lang.NullP ointerException

    It points to the following lines:

    getProjectsStmt .setInt(1, userID); - line 6 in the getProjects method - above
    getProject(1) - in main where I try to test it

    Any ideas why???

    Thanks,

    dev

    Thanks a lot for the help but after few sleepless hours got the error of my stupid ways... i dont want diagram, project or user objects just plain result sets added to arrayList which is simple as 1, 2, 3!!

    Thanks for your help man...

    Comment

    Working...