This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Keep getting Null Pointer Exception

 
Blake Edward
Ranch Hand
Posts: 105
4
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to build a small application at work. I have a DB set up and I have connected to it with SQL WorkBench and I wrote a simple program to read and write to it. Problem was that after so many iterations and so much editing the code seemed muddled, the DB was not being found, and I could not find my mistake. So, I built a small local version that does the same thing except it points to a local DB I made. I am using Tomcat because that is what the production environment uses. So the following code is a simple app that is based on a tutorial and on my needs. It is local on my machine only. I keep getting a NullPointerException at line 160.

When I run the file the page renders with a form that contains 18 <td> to be filled in. The page posts to itself, just to keep things simple. When I press the submit button I get an exception at line 160.

So, it seems that my connection to my DB works or I would get a SQLException at line 40.

It looks like data.setData is simply returning NULL at line 160, but I can't see my mistake and I can't figure out how to solve the issue. The method that I am using returns an int and I am wondering if that is the correct return type for this method.

Yes I am a novice and this is painful due to lack of mentorship at work. If you have any suggestions I would greatly value your feedback. Be as honest as you need to be.

Thanks!


 
Tapas Chand
Ranch Hand
Posts: 583
8
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all move the Java code to a Java file. Never write a single line of Java code in JSP. It is deprecated since around 14 years.
Then I feel it will be very easy for you to identify the root cause of the exception.
 
Blake Edward
Ranch Hand
Posts: 105
4
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point here is just to get everything on one page so it can be easily viewed. This jsp file replicates what I am trying to do. Whether it stays in this form is yet to be determined. If enough people agree about not using jsp's and can tell me why it is bad practice then that's advice I am willing to take. The initial/original version doesn't have the DB info embedded in it anyway, so it is split up and the page lives in the Web-INF folder and is password protected.

Thank you!
 
Maneesh Godbole
Saloon Keeper
Posts: 11329
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem is with DB connectivity. No body would like the read the JSP which frankly speaking is unrelated.

So start by writing a small code which connects to the DB and does some test CRUD operations.
Test it. If it works, compare it with your existing code. If not, post the full stack trace with the relevant code so we can take a look.
All this is usually called SSCCE
(Most probably, while writing the SSCCE you will figure out the problem yourself. )

Good advice by Tapas above. JSPs should be responsible for presentation not logic. Once you fix the DB issue, you might also want to consider refactoring your project accordingly.
 
Dave Tolls
Ranch Hand
Posts: 2100
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Blake Edward wrote:The point here is just to get everything on one page so it can be easily viewed. This jsp file replicates what I am trying to do. Whether it stays in this form is yet to be determined. If enough people agree about not using jsp's and can tell me why it is bad practice then that's advice I am willing to take. The initial/original version doesn't have the DB info embedded in it anyway, so it is split up and the page lives in the Web-INF folder and is password protected.

Thank you!


The structure of a Java web app is servlets do the work, JSPs display the results.

This has been the case for 15 years or more.

One of the main issues with code in JSPs is that you can't test them or debug them in any easy fashion.

For example, to take our code, if that was in a normal Java class, called by a servlet, you could then detach the class and test it on its own.
At the moment I am guessing you have to fire up the server to test code changes to that lot.
 
Maneesh Godbole
Saloon Keeper
Posts: 11329
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out this excellent article on why it is a good idea to separate view and logic.
http://www.javaranch.com/journal/200603/Journal200603.jsp#a5
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Blake Edward wrote:The point here is just to get everything on one page so it can be easily viewed. This jsp file replicates what I am trying to do. Whether it stays in this form is yet to be determined. If enough people agree about not using jsp's and can tell me why it is bad practice then that's advice I am willing to take. The initial/original version doesn't have the DB info embedded in it anyway, so it is split up and the page lives in the Web-INF folder and is password protected.

I agree with all other replies: a JSP page should only be used to display results not to connect to your database and execute queries. When I started in 2004 that was already the best practice and it didn't change.

And another few remarks:
1/ is there a specific reason why you are initializing your variables with an empty String using its default constructor instead of the more concise empty String literal ""?
2/ why are you doing the null check for every parameter? Why not simply get parameter value from request like thisIf the parameter does not exist, null will be returned (and will be written to the database table).
 
Blake Edward
Ranch Hand
Posts: 105
4
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Your problem is with DB connectivity. No body would like the read the JSP which frankly speaking is unrelated.

So start by writing a small code which connects to the DB and does some test CRUD operations.
Test it. If it works, compare it with your existing code. If not, post the full stack trace with the relevant code so we can take a look.
All this is usually called SSCCE
(Most probably, while writing the SSCCE you will figure out the problem yourself. )

Good advice by Tapas above. JSPs should be responsible for presentation not logic. Once you fix the DB issue, you might also want to consider refactoring your project accordingly.


All in all, there was some great advice here. I just really needed a push into the right direction. The rules of SSCCE was just that push. I kept whittling down my problem. I created a test program for my DB's. I first created one for the local DB which was successful, then created one for my actual DB. I started sifting through the errors and found that the issue was with the certification. Since the DB is confidential I need a cert and a key store. I had gone through this process but there was a problem with the URL string. Anyway, much thanks to everyone that responded. I think I can finally see the end of this project coming up.

Also, thanks for the advice regarding jsp's and keeping the code separate. I am amazed at how many tutorials and well respected books don't preach this philosophy.
 
Dave Tolls
Ranch Hand
Posts: 2100
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Blake Edward wrote:Also, thanks for the advice regarding jsp's and keeping the code separate. I am amazed at how many tutorials and well respected books don't preach this philosophy.


It's all too common, sadly.
It is possibly my most frequent rant...
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:It's all too common, sadly.

Absolutely! I'm really baffled how often a new topic has been created with a JSP containing some business logic code (often to connect to a database and execute a query). And for me that's really strange as it was already considered a (very) bad practice when I began as a Java developer (now more than 10 years ago).
 
Tapas Chand
Ranch Hand
Posts: 583
8
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:It's all too common, sadly...

Yes, absolutely.
In my case, in a practical exam during studies, I did the same thing, connected to DB from JSP and displayed results.
The examiner told me this is not the correct way to do even if it produced the required output and cut 50% of the marks.
It was a good lesson for me.
 
Blake Edward
Ranch Hand
Posts: 105
4
Java Netbeans IDE Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate all the remarks about how to correctly write a JSP. It really drives home the point in a positive way without making me feel to shameful.

Java Ranch Rocks The Code Forum World!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic