This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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
Author

Bean java.lang.NullPointerException

Lou Pelagalli
Ranch Hand

Joined: Nov 11, 2003
Posts: 150
    
    1
Hi,

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
Class.forName(DB_DRIVER_CLASS);
con = DriverManager.getConnection(DB_URL, USER_NAME, PASSWORD);


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

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

DepartmentModel.java


Servlet Welcome.java


WelcomeView.jsp
Lou Pelagalli
Ranch Hand

Joined: Nov 11, 2003
Posts: 150
    
    1
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.
 
 
subject: Bean java.lang.NullPointerException