How to instantiate HttpRequest Object in class level

P

pradeepsarathy

Hi all,
In the below code i am instantiating the request and session object at
the class level.But i am getting Null Pointer Exception while
instantiating.
Is it possible to instantiate a request object at class level.
I am doing so because,I want the request and session object to be
accessible in different methods of the same class.
Also all these methods(eg.method1()),will be called from SAX event
handler method(EndDocument() and character(char[] , int, int)),both of
which does not have provisions for passing the request obejct(or do
they?).

Please help me out.

Thanks in advance
-pradeep

import javax.servlet.http.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Session

public class xmlPocCmd extends AbstractCmd{
String onlyNumericElement = "";
String onlyAlphabetElement = "";
public static final String ONLYNUMERIC = "OnlyNumeric";
public static final String ONLYALPHABET = "OnlyAlphabet";

String[] errorArray = new String[2];

HttpServletRequest request; // Returns Null Pointer Exception
HttpSession session = request.getSession(false);

public String execute(HttpServletRequest req, HttpServletResponse res)
throws CmdException {
start(req);
return "xmlMarshallContainerPoc.jsp";
}
public method1(){
//Will accesss the request the session objects to set values;
}

}
 
R

Rastislav Komara

Well ... U are not instantienting HttpServletRequest!

This:
HttpServletRequest request; // Returns Null Pointer Exception
HttpSession session = request.getSession(false);

Is compiled to:
HttpServletRequest request = null;
HttpSession session = request.getSession(false);
in default constructor. Refer to Java documentation for fields
initialization.

U can't instantient HttpServletRequest. It is only interface and it's
implmentation is provided by Web/Application container. If u need
instance of session to store some information simply provide session
object in inicialisation or use some proper pattern to solve your
problem.
 
A

Alex Hunsley

Hi all,
In the below code i am instantiating the request and session object at
the class level.But i am getting Null Pointer Exception while
instantiating.

That's because you're not instantiating:

HttpServletRequest request; // Returns Null Pointer Exception

This line doesn't instantiate a HttpServletRequest instance - it merely
declares the type of a reference called 'request' that can be used to
refer to an HttpServletRequest instance (or a subclass instance). In
Java you use 'new' to make a new instance of an object:

Integer int = new Integer(7);

From your code it seems you don't have a very good understanding of
servlets. That can be fixed easily enough, but you have to go and do
some studying - I think that you need to work on your basic Java skills
before even thinking about things like Servlets. So forget servlets,
polish up your plain old Java knowledge first.
There's a tonne of free resources on the web for learning Java (e.g.
Sun's site) - go and reap the fruits!

alex
 
A

Alex Hunsley

Rastislav said:
Well ... U are not instantienting HttpServletRequest!

This:
HttpServletRequest request; // Returns Null Pointer Exception
HttpSession session = request.getSession(false);

Is compiled to:
HttpServletRequest request = null;
HttpSession session = request.getSession(false);

Speaking accurately: it's not, it's compiled into bytecode.
What you mean is that the JVM defaults to 'null' when an object
reference is declared without being assigned.
in default constructor. Refer to Java documentation for fields
initialization.

U can't instantient HttpServletRequest. It is only interface and it's

Please, use real english! It's "you".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top