• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException in JSP after calling Servlet method  RSS feed

 
Dan Parsons
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JSP that calls a method from a Servlet and it gives me a NullPointerException error in my Tomcat 5.5 Container on Windows.

Here is my Servlet Class:

The JSP (showDate.jsp):

Error messages:

Please advise how I can get this to work.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String myDate = new DateServlet().fetchDate(request);


Why on earth would you do something like this? Servlets are not intended to by instantiated by anything but the container. Why aren't you using a bean? (Or better yet, why not follow best practices and not do any Java in a JSP in the first place?)
 
Dan Parsons
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I originally tried this with a Bean class but couldnt use the request reference object because it doesnt seem to be available. Here is my attempt for the Bean:


Please advise if this is the right direction and what line should I change because the request object is not available when I try and compile it:



Thanks

[ July 21, 2007: Message edited by: Dan Parsons ]
[ July 21, 2007: Message edited by: Dan Parsons ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dan Parsons:
I originally tried this with a Bean class but couldnt use the request reference object because it doesnt seem to be available.


You're passing the request as a parameter. That makes it available no matter what.

Please advise if this is the right direction


Before doing that we need to figure out what you're trying to do in the first place. What is it? It looks like you are trying to get the date of the JSP file. Why? And your approach will not work if the JSP is traversed to as the result of a forward or include.
 
Dan Parsons
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, your info has helped alot because in future I will not display Java code in JSP AND I now will concentrate on using beans for displaying objects in JSP.

Before doing that we need to figure out what you're trying to do in the first place. What is it? It looks like you are trying to get the date of the JSP file. Why? And your approach will not work if the JSP is traversed to as the result of a forward or include.


Trying to output the last modified date in JSP page called showDate.jsp:


You're passing the request as a parameter. That makes it available no matter what
How can I use the request object reference in the bean class file (because it wont compile) or should I use something else in the fetchDate method?
[ July 21, 2007: Message edited by: Dan Parsons ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you trying to get at in your method? It's considered poor form to pass container objects like the request around. Better to get the data you need and pass that.
 
Dan Parsons
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I could get the last modified date and return it as a String in the fetchDate method.

This works if I just put it in a JSP with Scriplet:


I would rather not do it this way and use a Bean as you suggested and for giving me guidance to do it correctly in the future.
[ July 21, 2007: Message edited by: Dan Parsons ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Out of curiosity.
Why are you trying to get the last modified date of the JSP?
 
Dan Parsons
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want Last Modified to show in my JSP to give a Date and time of when the info was last modified/changed in the Web page.
 
Dan Parsons
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to keep this going but I ended up putting this in a Bean class and hard coding file name in the Bean class and it works but now I would like to have the file name passed as a condtion in the JSP Java Bean call.

Here is how it works with hardcoding the file name in the bean class and calling it in my JSP where it outputs the last modified date just as I wanted:

Java Bean class:



The above works but now how can I put the filename in the setProperty so I can have a variable in the JSP. I tried all the below and it just prints out a literal value: C:\\tomcathome\\anotherFile.jsp. Please advise.


Attempt in Java Bean:
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dan Parsons:
I want Last Modified to show in my JSP to give a Date and time of when the info was last modified/changed in the Web page.


That part I figured.
What I should have asked is, why?

To re-iterate and expand on what Bear said earlier, in most up to date Java web applications, the JSPs are only one part of the mechanism that handles the request/response chain. If a JSP is forwared to or included in another page, you're not going to be able to dynamically read this information. Also if your application has been deployed as a packed war file, then the method getRealPath, that you were using earlier to locate resources withing the app will return null; breaking your logic.

It sounds like you're going through a lot of trouble to build something that's only going to work in very specific cases.

Maybe if you let us know what you're trying to accomplish (the bigger picture) someone can suggest a better alternative.
 
Dan Parsons
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again for all your time on this!


Maybe if you let us know what you're trying to accomplish (the bigger picture) someone can suggest a better alternative.


I have three static JSP web pages where each has information on it and each time I update a page I would like the last modified date to show up on the page to show when that specific page was updated. This web app has just 3 static JSP web pages in it and does not use any MVC or other frameworks.
[ July 24, 2007: Message edited by: Dan Parsons ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!