(1) Introduction to the problem
Modify the item, and some features are described below:
Select the item you want to modify, click the Modify button, invoke Showmodifyitemservlet to make a series of settings, and forward to the item modification page item_modify.jsp.
In this process, an error occurs and the print stack is as follows:
April 01, 2012 5:35:52 PM Org.apache.catalina.core.StandardWrapperValve Invoke
Serious: Servlet.service () for Servlet [Showmodifyitemservlet] in context with path [/drp5.0] threw exception
Java.lang.NullPointerException
At Com.ys.drp.basedata.web.ShowModifyItemServlet.service (showmodifyitemservlet.java:43)
At Javax.servlet.http.HttpServlet.service (httpservlet.java:722)
At Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (applicationfilterchain.java:305) ...
(2) Problem analysis
You can see from the printed stack error:
Throw error: Servlet.service () for Servlet [Showmodifyitemservlet] in context with path [/drp5.0] threw exception
Error type: java.lang.NullPointerException, NULL pointer error
Where the error occurred: Com.ys.drp.basedata.web.ShowModifyItemServlet.service (showmodifyitemservlet.java:43)
The Showmodifyitemservlet code is as follows:
public class Showmodifyitemservlet extends Abstractitemservlet {
private datadictmanager Datadictmanager;
@Override public
void Init () throws Servletexception {
Datadictmanager = datadictmanager.getinstance ();
}
@Override
protected void Service (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
//Get Item code
String ItemNo = Request.getparameter ("ItemNo");
Query based on item code
. Item item = Itemmanager.finditembyid (ITEMNO); Set breakpoints here
//Set the item information to request
Request.setattribute ("item", item);
//...
Forwarding (passing Data)
Request.getrequestdispatcher ("/basedata/item_modify.jsp"). Forward (request, response);
}
At the breakpoint location shown above, set a breakpoint and run the following results:
Itemmanager = null, an object with a null pointer found, and an error in the empty pointer printed on the stack.
(3) Correct error
CTRL + left mouse button, enter Itemmanager, the code is as follows:
Public abstract class Abstractitemservlet extends HttpServlet {
protected itemmanager Itemmanager;
@Override public
void Init () throws servletexception {
beanfactory beanfactory = (beanfactory) This.getservletcontext (). getattribute ("Beanfactory");
Itemmanager = (Itemmanager) beanfactory.getserviceobject (Itemmanager.class);
}
Create the Itemmanager in the Init () method of the abstract class.
Looking at our code above, Showmodifyitemservlet inherits the abstract class, and Showmodifyitemservlet has its own init () method. Therefore, at run time, the Init () method of the subclass overrides the Init () method of the parent class, which appears above us.
Modify the Init () method of the subclass and join Super.init () as follows:
public void Init () throws Servletexception {
Datadictmanager = datadictmanager.getinstance ();
Super.init ();
}
The results of this operation are worth it:
(4) Results
The
continues to run and the page finally comes out.