Spring 3 -- NullPointerException from JDBCTemplate.getQueryForMap() -
i running custom tag in spring jsp. getting following error message.
java.lang.nullpointerexception com.dao.poll1dao.getvotes(poll1dao.java:27) com.tags.poll1tag.dotag(poll1tag.java:23) org.apache.jsp.web_002dinf.pages.pollpage_jsp._jspx_meth_mytag1_005fpoll1_005f0(pollpage_jsp.java:602) org.apache.jsp.web_002dinf.pages.pollpage_jsp._jspservice(pollpage_jsp.java:195) org.apache.jasper.runtime.httpjspbase.service(httpjspbase.java:70) javax.servlet.http.httpservlet.service(httpservlet.java:722) org.apache.jasper.servlet.jspservletwrapper.service(jspservletwrapper.java:432) org.apache.jasper.servlet.jspservlet.servicejspfile(jspservlet.java:390) org.apache.jasper.servlet.jspservlet.service(jspservlet.java:334) javax.servlet.http.httpservlet.service(httpservlet.java:722) org.springframework.web.servlet.view.internalresourceview.rendermergedoutputmodel(internalresourceview.java:238) org.springframework.web.servlet.view.abstractview.render(abstractview.java:262) org.springframework.web.servlet.dispatcherservlet.render(dispatcherservlet.java:1265) org.springframework.web.servlet.dispatcherservlet.processdispatchresult(dispatcherservlet.java:1016) org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:965) org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:859) org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:883) org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.java:781) javax.servlet.http.httpservlet.service(httpservlet.java:621) javax.servlet.http.httpservlet.service(httpservlet.java:722)
here poll1dao.getvotes().
public hashmap<string, object> getvotes() { // following line line 27, referenced in error message return (hashmap<string, object>) this.getjdbctemplate().queryformap( "select yes, no poll1 id = 1"); }
here poll1tag.dotag().
poll1dao poll1dao = new poll1dao(); public void dotag() throws jspexception, ioexception { pagecontext pagecontext = (pagecontext) getjspcontext(); boolean foundcookie = cookiefound(); pagecontext.setattribute("foundcookiepoll1", foundcookie); if (foundcookie) { // following line line 23, referenced in error message hashmap<string, object> poll1votes = poll1dao.getvotes(); pagecontext.setattribute("poll1yes", (int) poll1votes.get("yes")); pagecontext.setattribute("poll1no", (int) poll1votes.get("no")); } }
if using ioc container such spring's application context you should never create managed bean instances yourself. need pull instance out container - i.e. applicationcontext
. otherwise not initalized necessary dependencies.
now question should ask - how can applicationcontext
instance in jsp tag class. answer simple - how standard spring tags are doing that (i mean - go check link).
// example on how obtain bean jsp tag (ignoring context hierarchy) webapplicationcontextutils.getrequiredwebapplicationcontext(pagecontext.getservletcontext()). getbeansoftype(poll1dao.class).values().iterator().next();
Comments
Post a Comment