How to instantiate HttpRequest Object in class level

Discussion in 'Java' started by pradeepsarathy@gmail.com, Mar 10, 2006.

  1. Guest

    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;
    }

    }
    , Mar 10, 2006
    #1
    1. Advertising

  2. 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.
    Rastislav Komara, Mar 10, 2006
    #2
    1. Advertising

  3. Alex Hunsley Guest

    wrote:
    > 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
    Alex Hunsley, Mar 10, 2006
    #3
  4. Alex Hunsley Guest

    Rastislav Komara wrote:
    > 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".
    Alex Hunsley, Mar 10, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pabbu
    Replies:
    8
    Views:
    701
    Marc Boyer
    Nov 7, 2005
  2. =?Utf-8?B?SHVzYW0=?=

    Using HttpRequest Class

    =?Utf-8?B?SHVzYW0=?=, Sep 20, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    404
    Ben Schwehn
    Sep 20, 2007
  3. =?Utf-8?B?SHVzYW0=?=

    Add HttpRequest class to windows application

    =?Utf-8?B?SHVzYW0=?=, Sep 24, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    350
    Chris Fulstow
    Sep 25, 2007
  4. Colin Mc Mahon

    Instantiate class within a class

    Colin Mc Mahon, Aug 12, 2004, in forum: ASP General
    Replies:
    2
    Views:
    164
    Colin Mc Mahon
    Aug 14, 2004
  5. Dids
    Replies:
    2
    Views:
    280
Loading...

Share This Page