Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bean java.lang.NullPointerException

 
Lou Pelagalli
Ranch Hand
Posts: 150
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 150
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic