this addbook.jsp when click on edit, throws null pointer exception
<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>spring mvc form handling</title> </head> <body> <h2>add book data</h2> <form:form method="post" action="/sdnext/save.html"> <table> <tr> <td><form:label path="bookcode">book code:</form:label></td> <td><form:input path="bookcode" value="${book.bookcode}" readonly="true"/></td> </tr> <tr> <td><form:label path="bookname">book name:</form:label></td> <td><form:input path="bookname" value="${book.bookname}" /></td> </tr> <tr> <td><form:label path="author">book author name:</form:label></td> <td><form:input path="author" value="${book.author}"/></td> </tr> <tr> <td><form:label path="dateofarrival">date of arrival:</form:label></td> <td><form:input path="dateofarrival" value="${book.dateofarrival}"/></td> </tr> <tr> <td><form:label path="price">price of book:</form:label></td> <td><form:input path="price" value="${book.price}"/></td> </tr> <tr> <td><form:label path="rackid">book rack id:</form:label></td> <td><form:input path="rackid" value="${book.rackid}"/></td> </tr> <tr> <td><form:label path="numberofbooks">number of books:</form:label></td> <td><form:input path="numberofbooks" value="${book.numberofbooks}"/></td> </tr> <tr> <td><form:label path="subjectcode">book subject code:</form:label></td> <td><form:input path="subjectcode" value="${book.subjectcode}"/></td> </tr> <tr> <td colspan="2"><input type="submit" value="submit"/></td> </tr> </table> </form:form> <c:if test="${!empty books}"> <h2>list books</h2> <table align="left" border="1"> <tr> <th>nameofbook</th> <th>bookcode</th> <th>authorname</th> <th>dateofarrival</th> <th>priceofbook</th> <th>bookrackid</th> <th>numberofbooks</th> <th>booksubjectcode</th> <th>actions on row</th> </tr> <c:foreach items="${books}" var="book"> <tr> <td><c:out value="${book.bookname}"/></td> <td><c:out value="${book.bookcode}"/></td> <td><c:out value="${book.author}"/></td> <td><c:out value="${book.dateofarrival}"/></td> <td><c:out value="${book.price}"/></td> <td><c:out value="${book.rackid}"/></td> <td><c:out value="${book.numberofbooks}"/></td> <td><c:out value="${book.subjectcode}"/></td> <td align="center"><a href="edit.html?id=${book.bookcode}">edit</a> | <a href="delete.html?id=${book.bookcode}">delete</a></td> </tr> </c:foreach> </table> </c:if> </body> </html>
after submition exception thrown. because in controller value of bookcode field not recieve in bean class object.
type exception report message request processing failed; nested exception java.lang.nullpointerexception description server encountered internal error (request processing failed; nested exception java.lang.nullpointerexception) prevented fulfilling request. exception org.springframework.web.util.nestedservletexception: request processing failed; nested exception java.lang.nullpointerexception org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:659) org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.java:552) javax.servlet.http.httpservlet.service(httpservlet.java:621) javax.servlet.http.httpservlet.service(httpservlet.java:722) root cause java.lang.nullpointerexception com.tcs.controller.bookcontroller.editbook(bookcontroller.java:76) sun.reflect.nativemethodaccessorimpl.invoke0(native method) sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) java.lang.reflect.method.invoke(method.java:601) org.springframework.web.bind.annotation.support.handlermethodinvoker.doinvokemethod(handlermethodinvoker.java:710) org.springframework.web.bind.annotation.support.handlermethodinvoker.invokehandlermethod(handlermethodinvoker.java:167) org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter.invokehandlermethod(annotationmethodhandleradapter.java:414) org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter.handle(annotationmethodhandleradapter.java:402) org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:771) org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:716) org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:647) org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.java:552) javax.servlet.http.httpservlet.service(httpservlet.java:621) javax.servlet.http.httpservlet.service(httpservlet.java:722) note full stack trace of root cause available in apache tomcat/7.0.29 logs.
this controller class gets value of bookcode field in edit mehod , method null;
package com.tcs.controller; import java.util.arraylist; import java.util.hashmap; import java.util.list; import java.util.map; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.validation.bindingresult; import org.springframework.web.bind.annotation.modelattribute; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.servlet.modelandview; import com.tcs.bean.bookbean; import com.tcs.model.book; import com.tcs.service.bookservice; @controller public class bookcontroller { @autowired private bookservice bookservice; @requestmapping(method=requestmethod.post, value = "/save") public modelandview savebook(@modelattribute("command") bookbean bookbean, bindingresult result) { book book = preparemodel(bookbean); bookservice.addbook(book); return new modelandview("redirect:/add.html"); } @requestmapping(value="/books", method = requestmethod.get) public modelandview listbooks() { map<string, object> model = new hashmap<string, object>(); model.put("books", preparelistofbean(bookservice.listbooks())); return new modelandview("bookslist", model); } @requestmapping(value = "/add", method = requestmethod.get) public modelandview addbook(@modelattribute("command") bookbean booksbean, bindingresult result) { map<string, object> model = new hashmap<string, object>(); model.put("books", preparelistofbean(bookservice.listbooks())); return new modelandview("addbook", model); } @requestmapping(value = "/index", method = requestmethod.get) public modelandview welcome() { return new modelandview("index"); } @requestmapping(value = "/delete", method = requestmethod.get) public modelandview deletebook(@modelattribute("command") bookbean bookbean, bindingresult result) { system.out.println(bookbean.getbookcode()); bookservice.deletebook(preparemodel(bookbean)); map<string, object> model = new hashmap<string, object>(); model.put("book", null); model.put("books", preparelistofbean(bookservice.listbooks())); return new modelandview("addbook", model); } @requestmapping(value = "/edit" , method=requestmethod.get) public modelandview editbook(@modelattribute("command") bookbean bookbean, bindingresult result) { map<string, object> model = new hashmap<string, object>(); system.out.println(bookbean.getbookcode()); model.put("book", preparebooksbean(bookservice.getbook(bookbean.getbookcode()))); model.put("books", preparelistofbean(bookservice.listbooks())); return new modelandview("addbook", model); } private book preparemodel(bookbean bookbean){ book book = new book(); book.setbookname(bookbean.getbookname()); book.setbookcode(bookbean.getbookcode()); book.setauthor(bookbean.getauthor()); book.setdateofarrival(bookbean.getdateofarrival()); book.setprice(bookbean.getprice()); book.setrackid(bookbean.getrackid()); book.setnumberofbooks(bookbean.getnumberofbooks()); book.setsubjectcode(bookbean.getsubjectcode()); return book; } private list<bookbean> preparelistofbean(list<book> books){ list<bookbean> beans = null; if(books != null && !books.isempty()) { beans = new arraylist<bookbean>(); bookbean bean = null; for(book book : books){ bean = new bookbean(); bean.setbookname(book.getbookname()); bean.setbookcode(book.getbookcode()); bean.setauthor(book.getauthor()); bean.setdateofarrival(book.getdateofarrival()); bean.setprice(book.getprice()); bean.setrackid(book.getrackid()); bean.setnumberofbooks(book.getnumberofbooks()); bean.setsubjectcode(book.getsubjectcode()); beans.add(bean); } } return beans; } private bookbean preparebooksbean(book book){ bookbean bean = new bookbean(); bean.setbookname(book.getbookname()); bean.setbookcode(book.getbookcode()); bean.setauthor(book.getauthor()); bean.setdateofarrival(book.getdateofarrival()); bean.setprice(book.getprice()); bean.setrackid(book.getrackid()); bean.setnumberofbooks(book.getnumberofbooks()); bean.setsubjectcode(book.getsubjectcode()); return bean; } }
this service class of controller
package com.tcs.service; import java.util.list; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import org.springframework.transaction.annotation.propagation; import org.springframework.transaction.annotation.transactional; import com.tcs.dao.bookdao; import com.tcs.model.book; @service("bookservice") @transactional(propagation = propagation.supports, readonly = true) public class bookserviceimpl implements bookservice { @autowired private bookdao bookdao; @transactional(propagation = propagation.required, readonly = false) public void addbook(book book) { bookdao.addbook(book); } public list<book> listbooks() { return bookdao.listbooks(); } public book getbook(int bookcode) { return bookdao.getbook(bookcode); } public void deletebook(book book) { bookdao.deletebook(book); } }
this setting of servlet.xml
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:property-placeholder location="classpath:resources/database.properties" /> <context:component-scan base-package="com.tcs" /> <tx:annotation-driven transaction-manager="hibernatetransactionmanager"/> <bean id="jspviewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="viewclass" value="org.springframework.web.servlet.view.jstlview" /> <property name="prefix" value="/web-inf/views/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname" value="${database.driver}" /> <property name="url" value="${database.url}" /> <property name="username" value="${database.user}" /> <property name="password" value="${database.password}" /> </bean> <bean id="sessionfactory" class="org.springframework.orm.hibernate3.annotation.annotationsessionfactorybean"> <property name="datasource" ref="datasource" /> <property name="annotatedclasses"> <list> <value>com.tcs.model.book</value> </list> </property> <property name="hibernateproperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> </props> </property> </bean> <bean id="hibernatetransactionmanager" class="org.springframework.orm.hibernate3.hibernatetransactionmanager"> <property name="sessionfactory" ref="sessionfactory" /> </bean> </beans>
this web.xml file
<?xml version="1.0" encoding="utf-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>sdnext</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/config/sdnext-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>sdnext</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
you edit method
@requestmapping(value = "/edit" , method=requestmethod.get) public modelandview editbook(@modelattribute("command") bookbean bookbean, bindingresult result) { map<string, object> model = new hashmap<string, object>(); system.out.println(bookbean.getbookcode()); // bookbean null model.put("book", preparebooksbean(bookservice.getbook(bookbean.getbookcode()))); model.put("books", preparelistofbean(bookservice.listbooks())); return new modelandview("addbook", model); }
accepts bookbean instance. spring create bean you, needs have request parameters map fields of object. you're making request through <a href>
anchor.
<a href="edit.html?id=${book.bookcode}">edit</a>
the parameter in request id
, doesn't match bookcode
field name. spring won't able create bean , therefore pass null
. can read more process here.
we can assume similar occurs deletebook
.
Comments
Post a Comment