java - Javaservlet nullpointerexception -


i'm trying make simple login screen. has check password , username. there default user, set in init(). weird thing is, it's still giving me nullpointerexception on rd.forward();
explain? i'm sorry bad english, let me know if have elaborate more.

package servlets;  import java.io.bufferedreader; import java.io.file; import java.io.filereader; import java.io.ioexception; import java.io.printwriter; import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; import java.util.arraylist; import java.util.scanner; import javax.servlet.filterconfig; import javax.servlet.requestdispatcher; import javax.servlet.servletconfig; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse;  import classes.user;  public class loginservlet extends httpservlet { arraylist<user> alleusers = new arraylist<>(); private string defaultuser="", defaultpassword=""; private user userding;  public void init(servletconfig config) throws servletexception{     super.init(config);     defaultuser = config.getinitparameter("defaultuser");     defaultpassword = config.getinitparameter("defaultpassword");     user userding = new user(defaultuser, defaultpassword); }  protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {     string = req.getparameter("back");     string b = req.getparameter("username");     string c = req.getparameter("password");     object o = getservletcontext().getattribute("arraylist");     requestdispatcher rd = null;      if (a.equals("register")) {         rd = req.getrequestdispatcher("registerscherm.jsp");     }      if (a.equals("login")) {         system.out.println(b + " "+ c);         if(!b.equals(null) && !c.equals(null)){             if (o instanceof arraylist) {                     alleusers = (arraylist) o;                     alleusers.add(userding);                     if (alleusers != null) {                         (user u : alleusers) {                             if (b.equals(u.getusername()) && c.equals(u.getpassword())) {                                 getservletcontext().setattribute("naam", b);                                 rd = req.getrequestdispatcher("ingelogdscherm.jsp");                                 break;                             } else {                                 rd = req.getrequestdispatcher("loginscherm.jsp");                             }                         }                     }                 }             }else{                 rd = req.getrequestdispatcher("loginscherm.jsp");             }         }     rd.forward(req, resp);     //it gives error on forward     } } 

here's loginscreen jsp page:

<body>     <%         string username = "";         (cookie c : request.getcookies()) {             if (c.getname().equals("username")) {             username = c.getvalue();             break;         }     }  %> <div id="account"> <div id="inputbox">     <form action="loginservlet.do" method="post">         <input class="ltf" type="text" name="username" value="<%= username %>" /><br/>          <input class="ltf" type="password" name="password" /><br/>          <input type="submit" name="back" value="login" />          <input type="submit" name="back" value="register" />     </form> </div> 

okay think fixed it. removed of guards, helped. i'm still confused why didnt go through them, works now. thanks!

if value of a not equal "login" or "register" rd remains null rd.forward(req, resp); give nullpointerexception.
can debug printing value of a or can add rd.forward(req, resp); wherever use req.getrequestdispatcher("name.jsp"); below :

package servlets;  import java.io.bufferedreader; import java.io.file; import java.io.filereader; import java.io.ioexception; import java.io.printwriter; import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; import java.util.arraylist; import java.util.scanner; import javax.servlet.filterconfig; import javax.servlet.requestdispatcher; import javax.servlet.servletconfig; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse;  import classes.user;  public class loginservlet extends httpservlet { arraylist<user> alleusers = new arraylist<>(); private string defaultuser="", defaultpassword=""; private user userding;  public void init(servletconfig config) throws servletexception{     super.init(config);     defaultuser = config.getinitparameter("defaultuser");     defaultpassword = config.getinitparameter("defaultpassword");     user userding = new user(defaultuser, defaultpassword); }  protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {     string = req.getparameter("back");     string b = req.getparameter("username");     string c = req.getparameter("password");     object o = getservletcontext().getattribute("arraylist");     requestdispatcher rd = null;      if (a.equals("register")) {         rd = req.getrequestdispatcher("registerscherm.jsp");         rd.forward(req, resp);     }      if (a.equals("login")) {         system.out.println(b + " "+ c);         if(!b.equals(null) && !c.equals(null)){             if (o instanceof arraylist) {                     alleusers = (arraylist) o;                     alleusers.add(userding);                     if (alleusers != null) {                         (user u : alleusers) {                             if (b.equals(u.getusername()) && c.equals(u.getpassword())) {                                 getservletcontext().setattribute("naam", b);                                 rd = req.getrequestdispatcher("ingelogdscherm.jsp");                                 rd.forward(req, resp);                                 break;                             } else {                                 rd = req.getrequestdispatcher("loginscherm.jsp");                                 rd.forward(req, resp);                             }                         }                     }                 }             }else{                 rd = req.getrequestdispatcher("loginscherm.jsp");                 rd.forward(req, resp);             }         }     } } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -