Java.lang.NullPointerException NULL pointer error debugging

Source: Internet
Author: User

(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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.