• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Keep getting Null Pointer Exception

 
Ranch Hand
Posts: 124
4
MySQL Database Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!


 
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 124
4
MySQL Database Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 124
4
MySQL Database Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 124
4
MySQL Database Clojure Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic