java.lang.NullPointerException with variable having a value (Eclipse) -
i have looked throughout web try find answer error, nothing have found matches error or fail understand relation.
here error running on eclipse:
java.lang.nullpointerexception ca.sheridancollege.controllers.cardealership.doget(cardealership.java:63) javax.servlet.http.httpservlet.service(httpservlet.java:622) javax.servlet.http.httpservlet.service(httpservlet.java:729) org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52)
and here code:
package ca.sheridancollege.controllers; import java.io.ioexception; import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; /** * servlet implementation class cardealership */ @webservlet("/cardealership") public class cardealership extends httpservlet { private static final long serialversionuid = 1l; /** * @see httpservlet#httpservlet() */ public cardealership() { super(); // todo auto-generated constructor stub } /** * @see httpservlet#doget(httpservletrequest request, httpservletresponse response) */ protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub string dburl= "jdbc:mysql://localhost:3306/sheridanusedcars"; string user = "root"; string password = "1234"; connection conn = null; try { conn = drivermanager.getconnection(dburl, user, password); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } string retrieve = "select manufacturerid, manufacturer, price car inner join manufacturer group manufacturerid"; preparedstatement ps = null; try { ps = conn.preparestatement(retrieve); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } resultset rs = null; try { rs = ps.executequery(); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } system.err.println("error executing "+ps.tostring()); try { while (rs.next()){ int id = rs.getint("manufacturerid"); string man = rs.getstring("manufacturer"); response.getwriter().append("<h1>"+id+" "+man+"</h1>"); } } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } /** * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response) */ protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub doget(request, response); } }
it seems me thinks rs null, because of try catch? cant seem going. really appreciated
the problem in here
resultset rs = null; try { rs = ps.executequery(); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } system.err.println("error executing "+ps.tostring()); try { while (rs.next()){
if call executequery throws exception, rs stay null. since try-catch block doesn't stop execution, it'll continue on despite rs being null, leading error @ line 63
Comments
Post a Comment