473,406 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Null Pointer Exception for simple jdbc code

8
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(rs);
arrayList.add(p);
rs.close();

}
}
catch(Exception e)
{
System.err.println(e);
}

return arrayList;

}

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


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

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

int projectID = rs.getInt("projectID");
int userID = rs.getInt("userID");
String name = rs.getString("name");
String description = rs.getString("description");
int createdate = rs.getInt("createdate");
boolean visibility = rs.getBoolean("visibility");

return new Projects(projectID, 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.NullPointerException

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
Feb 28 '07 #1
1 5318
dev24
8
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(rs);
arrayList.add(p);
rs.close();

}
}
catch(Exception e)
{
System.err.println(e);
}

return arrayList;

}

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


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

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

int projectID = rs.getInt("projectID");
int userID = rs.getInt("userID");
String name = rs.getString("name");
String description = rs.getString("description");
int createdate = rs.getInt("createdate");
boolean visibility = rs.getBoolean("visibility");

return new Projects(projectID, 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.NullPointerException

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...
Mar 2 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: Denis Petronenko | last post by:
Hello, in the following code i have segmentaion fault instead of exception. Why? What i must to do to catch exceptions in such situation? Used compiler: gcc version 3.3.6 (Debian 1:3.3.6-13) ...
3
by: Alexander Behnke | last post by:
Hello, I am currently facing a problem with a calloc function call which returns a NULL pointer while in a debugging environment. If I start the same executable from the shell (not via the...
6
by: dev24 | last post by:
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...
2
by: bevis | last post by:
I'm new to sql server and mysql but this seems like it should be a pretty straight forward jdbc connection. But I have spent almost 2 days just trying to get a jdbc connection. Please help if you...
1
by: ajos | last post by:
hi evrybdy, the problem is:- i had this running before in jsp but when i changed the jsp page using struts tags there occoured a problem... when i enter values in the 2 text boxes and click enter...
0
by: star111792 | last post by:
helo all i am working with JSP and EJBs. i have written a simple code for verifying username and password of user by using session beans. my problem is that i am getting the following exception: ...
0
by: neuraljay | last post by:
hi everyone. I am trying to build a jsp using webwork, hibernate and mysql. And right now I am doing its unit testing. Everytime I run the test I am getting this error: Error JDBC exception on...
5
by: iamdennisthomas | last post by:
Hi Guys i was developing a action servlet which is actulally getting the data from a form and putting it in the db but while executing i am getting a null pointer exception /* * Generated by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.