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
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
Comment