您的位置:首页 > 编程语言 > Java开发

JavaWeb-Servlet-异常:解决bug,getServletContext()时报错:java.lang.NullPointerException

2016-01-19 12:36 661 查看

重写Servlet的init()方法,要先调用父类的super.init(config)方法

UserManager应用中,要在应用加载时初始化InitCount这个Servlet,用于统计登录网站的人数,web.xml中加入这个servlet的1的属性,但是在tomcat启动时报错java.lang.NullPointerException;

网上查阅资料,发现重写的init()方法中,要先调用父类的super.init(config)方法。、

public class InitCount extends HttpServlet {
public void init(ServletConfig config) throws ServletException {

super.init(config);//这里要传入参数config

// 从文件中读出count的值,放入ServletContext
String filename = config.getInitParameter("filename");
String filepath = this.getServletContext().getRealPath("/resource/count.txt");
System.out.println(filepath);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  servlet tomcat 异常