i'm getting above error message when trying display user table mysql db. can see, error generated displayusersservlet @ line 33 folloxing code : arraylist arr = userdb.selectusers();
java.lang.nullpointerexception user.displayusersservlet.doget(displayusersservlet.java:33) javax.servlet.http.httpservlet.service(httpservlet.java:621) javax.servlet.http.httpservlet.service(httpservlet.java:728) here classes:
package business;
import java.io.serializable; public class user implements serializable { private string firstname; private string lastname; private string emailaddress; public user() { firstname = ""; lastname = ""; emailaddress = ""; } public user(string first, string last, string email) { firstname = first; lastname = last; emailaddress = email; } public void setfirstname(string f) { firstname = f; } public string getfirstname() { return firstname; } public void setlastname(string l) { lastname = l; } public string getlastname() { return lastname; } public void setemailaddress(string e) { emailaddress = e; } public string getemailaddress() { return emailaddress; } }
servlet class
package user; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.arraylist; import business.user; import data.userdb; public class displayusersservlet extends httpservlet { private static final long serialversionuid = 1l; userdb userdb; protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo: arraylist of user objects database arraylist<user> arr = userdb.selectusers(); httpsession session = request.getsession(); // todo: set arraylist of user objects session attribute session.setattribute("users", arr); string url = "/users.jsp"; requestdispatcher dispatcher = getservletcontext() .getrequestdispatcher(url); dispatcher.forward(request, response); } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { doget(request, response); } }
dao
package data; import java.sql.*; import java.util.arraylist; import business.user; public class userdb { // todo: add code returns arraylist of user objects // corresponds rows in user table public arraylist<user> selectusers() { connectionpool pool = connectionpool.getinstance(); connection connection = pool.getconnection(); preparedstatement ps = null; resultset rs = null; string query = "select * user"; arraylist<user> arrusr = new arraylist<>(); try { ps = connection.preparestatement(query); rs = ps.executequery(); user user = null; while (rs.next()) { user = resultsetrowtousers(rs); if (user.getfirstname()!=null) arrusr.add(resultsetrowtousers(rs)); } } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } return arrusr; } public user resultsetrowtousers(resultset resultset) throws sqlexception { //user user = new user(); return new user(resultset.getstring(1), resultset.getstring(2), resultset.getstring(3)); } }
print stacktrace (eclipse console)
jul 11, 2013 2:12:53 pm org.apache.catalina.core.standardwrappervalve invoke severe: "servlet.service()" servlet displayusersservlet has generated exception java.lang.nullpointerexception @ user.displayusersservlet.doget(displayusersservlet.java:33) @ javax.servlet.http.httpservlet.service(httpservlet.java:621) @ javax.servlet.http.httpservlet.service(httpservlet.java:728) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:305) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:222) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:123) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:472) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:171) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:99) @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:947) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:118) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:408) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1009) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:589) @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:312) @ java.util.concurrent.threadpoolexecutor.runworker(unknown source) @ java.util.concurrent.threadpoolexecutor$worker.run(unknown source) @ java.lang.thread.run(unknown source)
userdb needs initialized?
Comments
Post a Comment