It's not a secret anymore!*
The moose likes Servlets and the fly likes Error not recognize Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Error not recognize " Watch "Error not recognize " New topic
Author

Error not recognize

Indravadan T Patel
Greenhorn

Joined: Sep 09, 2007
Posts: 29
Hi,


I have code like :

package indra;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class sample extends HttpServlet {
public String t1;
public String t2;
public void init(ServletConfig scf) throws ServletException {
t1 = scf.getServletContext().getInitParameter("indra");
t2 = scf.getInitParameter("Patel");
// String t1 = scf.getInitParameter("patel");
}
public sample() {
this(15);
System.out.println("I am Constructor in servlet");

}

public sample(int p) {

System.out.println("I am Constructor in servlet " + p);
// t1= getServletConfig().getServletContext().getInitParameter("indra");
}
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException {
tryBean tbean = new tryBean();
String ram = new String("ramesh");
ServletContext context = getServletContext();
context.setAttribute("indra", ram);

res.getWriter().write("this :" + this);
// obj=new sample(30);
res.getWriter().write("<br>");
res.getWriter().write("this :" + this);
res.getWriter().write("<br>");
res.getWriter().write("obj :" + obj);
res.getWriter().write("<br>");
res.getWriter().write("this.obj :" + this.obj);
res.getWriter().write("<br>");

// obj=new sample();
// res.getWriter().write("this.obj.obj :" + this.obj.obj);
res.getWriter().write("<br>");
res.getWriter().write("this :" + this);
res.getWriter().write("<br>");
res.getWriter().write("obj :" + obj);
res.getWriter().write("<br>");
res.getWriter().write("this.obj :" + this.obj);

res.getWriter().write("-----------------------");
// t1=getServletContext().getInitParameter("indra");
//res.getWriter().write(getServletContext().getInitParameter("indra").toString());
// res.getWriter().write(t1.toString());
// res.getWriter().write(t2.toString());
//res.getWriter().write(getServletConfig().getInitParameter("vadan").toString());
}

public sample obj;


}

end i got the error :

java.lang.NullPointerException
javax.servlet.GenericServlet.getServletContext(GenericServlet.java:159)
indra.sample.doGet(sample.java:35)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)

I can't carry out the solution for.

Regards,
Indravadan Patel
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41634
    
  55
Never, ever, put a constructor into a servlet (or call a servlet's constructor in your own code).

If you need to do any initializations, use the init(ServletContext) method. That's also where you can get (and store) a reference to the ServletContext object.
[ April 17, 2008: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

From the API for GenericServlet.init(ServletConfig config)

When overriding this form of the method, call super.init(config).


Therefore your init should look like this:
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

... and what Ulf says.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41634
    
  55
Oh right, it's init(ServletConfig), not init(ServletContext).

There's a bad chip in my memory somewhere.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error not recognize