File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Bean java.lang.NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Bean java.lang.NullPointerException" Watch "Bean java.lang.NullPointerException" New topic

Bean java.lang.NullPointerException

Lou Pelagalli
Ranch Hand

Joined: Nov 11, 2003
Posts: 150

I'm not sure if this is the correct forum, but I'll start here.

My bean titled DepartmentModel instance variables do not appear to persist between method calls.

In DepartmentModel below you'll see code to create an instance connection in the Constructor and method getResultSet().

When I established connection in only the constructor I was getting a NullPointerException in getResultSet() at stmt = con.createStatement();

After adding
con = DriverManager.getConnection(DB_URL, USER_NAME, PASSWORD);

to getResultSet() I'm getting a NullPointerException in method next() at recsLeft =;

Why would I need to establish a new connection at every method call? It seems that establishing a connection in the constructor would be enough since the connection is an instance variable.

Can somone show me how to correct this?

My code is below.



Lou Pelagalli
Ranch Hand

Joined: Nov 11, 2003
Posts: 150
I got it!

I wrote a quick and dirty test class for the bean and discovered that I forgot all about my jar files to connect to the database!

Putting the jar files in WEB-INF/lib solved everything!

The lessons:
1) Remember the jar files!
2) A quick and dirty test class can reveal problems tomcat won't.
3) Throw your exceptions back to the calling class instead of keeping them to yourself.
I agree. Here's the link:
subject: Bean java.lang.NullPointerException
It's not a secret anymore!