Tuesday, 19 April 2016

jsp - why i'm getting this error of HTTP Status 500?





I've created a simple JSP page and made a Servlet called UserController.java which is inside the package com.pro3.controller. In jsp page I have a form with get method. When I submit the form it fails with following exception:



HTTP Status 500 - javax.servlet.ServletException: Error instantiating servlet class com.pro3.controller.UserController



--------------------------------------------------------------------------------


type Exception report

message javax.servlet.ServletException: Error instantiating servlet class com.pro3.controller.UserController

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

exception

org.apache.jasper.JasperException: javax.servlet.ServletException: Error instantiating servlet class com.pro3.controller.UserController
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:502)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:412)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)


root cause

javax.servlet.ServletException: Error instantiating servlet class com.pro3.controller.UserController
org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:709)

org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:680)
org.apache.jsp.index_jsp._jspService(index_jsp.java:62)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)



root cause

java.lang.NullPointerException
java.util.Properties$LineReader.readLine(Properties.java:418)
java.util.Properties.load0(Properties.java:337)
java.util.Properties.load(Properties.java:325)
com.pro3.util.DbUtil.getConnection(DbUtil.java:26)
com.pro3.dao.UserDao.(UserDao.java:19)
com.pro3.controller.UserController.(UserController.java:27)
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
java.lang.reflect.Constructor.newInstance(Constructor.java:513)
java.lang.Class.newInstance0(Class.java:355)
java.lang.Class.newInstance(Class.java:308)
org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:709)
org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:680)
org.apache.jsp.index_jsp._jspService(index_jsp.java:62)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)


Anyone help me out this problem.



public class DbUtil 
{

public static Connection connection = null;

public static Connection getConnection()
{
if(connection!=null)
return connection;
else
{
try
{

Properties prop=new Properties();
InputStream inputStream=DbUtil.class.getClassLoader().getResourceAsStream("/db.properities");
prop.load(inputStream);

String driver=prop.getProperty("driver");
String url=prop.getProperty("url");
String user=prop.getProperty("user");
String password=prop.getProperty("password");

Class.forName(driver);

connection=DriverManager.getConnection(url,user,password);

}catch(ClassNotFoundException e){
e.printStackTrace();


This is Null Pointer Exception:



public class UserController extends HttpServlet 
{

private static final long serialVersionUID = 1L;
private static String INSERT_OR_EDIT="/user.jsp";
private static String LIST_USER="/listuser.jsp";
private UserDao dao;

public UserController()
{
super();
dao = new UserDao();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String forward="";
String action = request.getParameter("action");

if(action.equalsIgnoreCase("delete"))
{
int userId=Integer.parseInt(request.getParameter("userid"));
dao.deleteUser(userId);
forward=LIST_USER;

request.setAttribute("users", dao.getAllUsers());
}
else if(action.equalsIgnoreCase("edit"))
{
forward=INSERT_OR_EDIT;
int userId=Integer.parseInt(request.getParameter("userid"));
User user=dao.getUserById(userId);
request.setAttribute("user", user);
}
else if(action.equalsIgnoreCase("listUser"))

{
forward=LIST_USER;
request.setAttribute("users", dao.getAllUsers());
}
else
{
forward=INSERT_OR_EDIT;
}
RequestDispatcher view=request.getRequestDispatcher(forward);
view.forward(request, response);

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
User user=new User();
user.setFirstName(request.getParameter("firstname"));
user.setLastName(request.getParameter("lastname"));
try
{
Date dob = new SimpleDateFormat("dd/mm/yyyy").parse(request.getParameter("dob"));

user.setDob(dob);
}
catch(ParseException e)
{
e.printStackTrace();
}
user.setEmail(request.getParameter("email"));
String userid=request.getParameter("userid");
if(user==null||userid.isEmpty())
{

dao.addUser(user);
}
else
{
user.setUserid(Integer.parseInt(userid));
dao.updateUser(user);
}
RequestDispatcher view=request.getRequestDispatcher(LIST_USER);
request.setAttribute("users", dao.getAllUsers());
view.forward(request, response);

}
}





Pablo Lozano: He found some error, and then ill do it then, now i getting this kind of error.




HTTP Status 500 - java.lang.NullPointerException




-------------------------------------------------------------------------------- type




Exception report message java.lang.NullPointerException




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





exception




org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:502)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)





rootcause



java.lang.NullPointerException




com.pro3.dao.UserDao.getAllUsers(UserDao.java:74)
com.pro3.controller.UserController.doGet(UserController.java:51)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)

javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:709)
org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:680)
org.apache.jsp.index_jsp._jspService(index_jsp.java:62)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)




Answer



DbUtil.class.getClassLoader().getResourceAsStream("/db.properities");


Try to write db.properties instead of db.properities


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...