• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet HTTP Status 500 - problem

 
stevan pivnicki
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to do a simple form input so I can fill the ArrayList but I keep getting HTTP Status 500.

How do I resolve this ?

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
javax.servlet.GenericServlet.getServletContext(Gen ericServlet.java:123)
example04.UpisUListu.doGet(UpisUListu.java:50)
javax.servlet.http.HttpServlet.service(HttpServlet .java:618)
javax.servlet.http.HttpServlet.service(HttpServlet .java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilt er(WsFilter.java:52)

note The full stack trace of the root cause is available in the Apache Tomcat/8.0.18 logs.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javax.servlet.GenericServlet.getServletContext(Gen ericServlet.java:123)
example04.UpisUListu.doGet(UpisUListu.java:50)


Is your servlet extending GenericServlet or HttpServlet?

What is your code for the init() method?

Bill
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
 
Stefan Evans
Bartender
Posts: 1822
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is funny about this exception is that it is in the call to getServletContext which is part of the engine code.

Can you provide the full exception from the log?
Are there any other messages in the log from startup etc?
(clutching at straws) - you don't have any of the servlet libraries in your WEB-INF/lib folder do you?
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the code! What do you have in this line?

 
stevan pivnicki
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the replies but I fixed the problem on my own.
I'm actually a newbie in servlet programming with Eclipse so I accidentally made a mistake with init() function.

 
stevan pivnicki
Greenhorn
Posts: 23
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this was the problem:
Since I'm new to the servlet technologies and Eclipse (i was using Netbeans until recently), while creating a servlet, I was clicking on the init check box so Eclipse would make init() method automatically.

The Eclipse made the init() method like this and that would cause me problems



so I change it to


and that solved my problem.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you are not using init() to actually do anything, simply remove it.
 
stevan pivnicki
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was using init() so I could place the ArrayList in that method.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What list? Why in init()?

init() should have nothing to do with form submission requests.
 
stevan pivnicki
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It went something like this:

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ACK!!!

DANGER!! DANGER!!

Never, ever, ever, use instance variables in a servlets. Declare your list inside the method.

Using instance variables makes the servlet non-thread-safe and multiple requests to your servlet will all stomp all over each other.
 
stevan pivnicki
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:ACK!!!

DANGER!! DANGER!!

Never, ever, ever, use instance variables in a servlets. Declare your list inside the method.

Using instance variables makes the servlet non-thread-safe and multiple requests to your servlet will all stomp all over each other.


Noted.

Thanks for the advice.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This means not only move its initialization inside the method, but its declaration as well.
 
stevan pivnicki
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that necessary only for ArrayLists or for all class declarations inside the servlet ?
 
Paul Clapham
Sheriff
Posts: 21872
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stevan pivnicki wrote:Is that necessary only for ArrayLists or for all class declarations inside the servlet ?


Bear said

Never, ever, ever, use instance variables in a servlets.


What made you think that advice only applied to ArrayList variables? Of course, it applies to all variables.
 
stevan pivnicki
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This made me think of it that it only applies to ArrayLists (notice the declaration of Products class):


 
Paul Clapham
Sheriff
Posts: 21872
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The advice applies to that, too. Even though that Products variable is only used in the init() method, which is guaranteed to be called only once and hence can't be a thread-safety issue, it tempts future programmers to use it in other methods. Even if that class wasn't a servlet, it's still a bad practice to declare instance variables which are actually only ever used as temporary variables in a single method.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Revised advice with more detail: never use instance variables of any kind in a servlet.

Read-only class variables used to define "constants" are fine.
 
Dave Tolls
Ranch Hand
Posts: 2271
23
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
DANGER!! DANGER!!


I will now picture you as the Robot for evermore.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


More info on why: because a single instance of a servlet vcan be used to process multiple simultaneous requests in separate threads, instance variables, of which there will be only one copy, will be used by all the threads. So one thread might set a variable to one value, another will set it to another value, then the first thread gets the wrong value, and well, all hell breaks loose.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!