0

I'm using MCV pattern technology with Netbeans and Tomcat.So im a the part where i try to populate my Database when a person signs up but i get Null exception at doGet and doPost.So here is the code :

Controller

public class Controller extends HttpServlet {
protected HashMap validActions = new HashMap();

@Override
public void init() throws ServletException {
    try {
        ResourceBundle bundle = ResourceBundle.getBundle("Action");
        Enumeration e = bundle.getKeys();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            String value = bundle.getString(key);
            try {
                ActionHandler action = (ActionHandler) Class.forName(value).newInstance();
                validActions.put(key, action);
            } catch (Exception exc) {
                exc.printStackTrace();
            }
        }
    } catch (NullPointerException e) {
        System.out.println(e);
    } catch (MissingResourceException e) {
        System.out.println(e);
    } catch (Exception e) {
        System.out.println(e);
    }
}

//When the GET HTTP method is used, just call the doPost Method
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
}

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");
    //response.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charset=UTF-8");
    request.setCharacterEncoding("UTF-8");
    //Check if the current event is a valid application Event
    String action = validateAction(request);

    //GET the appropriate event handler for the requested event. If you can't, then get Get the ERROR_HANDLER
    ActionHandler handler = getActionHandler(action);
    try {
        //Ask the handler to process the request.
        handler.process(getServletContext(), request, response);
    } catch (Exception e) {
        request.setAttribute("error", e);
        handler = getActionHandler(Conf.ERROR);
    }
    //Forward the Response to the appropriate Viewer.
    handler.forward(request, response);
}

//Method that validates and returns the event requrested by the HTTP Request
//If no event was set, then it returns UNKNOWN_EVENT
protected String validateAction(HttpServletRequest request) {
    String action = request.getServletPath();
    if (action == null || "" == action) {
        return action = "HOME.show";
    }else{
        action = action.substring(1);
    }
    if (!validActions.containsKey(action)) {
        action = Conf.UNKNOWN_ACTION;
    }
    return action;
}

/* Method that returns the assigned ActionHandler for a given Action
 * If no ActionHandler is assigned, then it returns the ActionHandler of the 
 * Home.show action */
protected ActionHandler getActionHandler(String e) {
    ActionHandler h;
    try {
        h = (ActionHandler) validActions.get(e);
    } catch (Exception exc) {
        h = (ActionHandler) validActions.get(Conf.UNKNOWN_ACTION);
    }
    return h;
}

}

SignUpActionHandlerDO

public class SignUpActionHandlerDO extends ActionHandler {
private String viewer= "/View/home.jsp";        
private Person newPerson = null;

@Override
public String getViewer() {        
    return viewer;
}


@Override
public Object getModel() {
    return newPerson;
}

    public SignUpActionHandlerDO() {        
}
@Override
public void process(ServletContext sc, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
   String Fname = request.getParameter("Fname");
   String Lname = request.getParameter("Lname");
   String email = request.getParameter("email");
   String pass = request.getParameter("pass");

    PersonManager PM = new PersonManager();
    try{
        newPerson = PM.createPerson(Fname,Lname,email,pass);

    }catch(IOException ex){
        viewer = "/View/signup.jsp";
        request.setAttribute("error", ex.getMessage());
    }
}




}

SignUp.jsp

<form class="form-signup" action="SIGNUPdo.show" method="POST">


at Controller.Control.Controller.doPost(Controller.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2463)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2452)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

So any ideas on what is wrong here??

NLD
  • 1
  • 5
  • You should include the stacktrace. Also, catching `NullPointerException`s is a bad idea. – GriffeyDog Jun 08 '15 at 15:11
  • i've tried to find an answer in other posts but there are different implementations so i couldnt work this out..It seems the problem is at line 70 : handler.forward(request, response); – NLD Jun 08 '15 at 15:16
  • You're asking like "Why did I get a NullPointerException?". You didn't ask like "Why is variable X null?". The duplicate answers the former question, which should help you in figuring the X part of the later question (which is way much more useful to find out the real problem). – BalusC Jun 08 '15 at 15:49
  • if you leave a comment in your code on the line that's throwing the exception, that would be helpful – Sam I am says Reinstate Monica Jun 08 '15 at 16:41

0 Answers0