Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NameValuePair array 1

Status
Not open for further replies.

aswolff

Programmer
Jul 31, 2006
100
US
Hello, I am trying to streamline my code to make use of a loop to populate a NameValuePair array. My code compiles..but I get a run-time error:
Exception in thread "main" java.lang.NullPointerException
at lawsonUtil.NVPFromXML(lawsonUtil.java:34)
at WebApplicant.main(WebApplicant.java:72)

code before which compiles and runs fine.....
Code:
	public static NameValuePair[] NVPFromXML(Document indoc)
	{
		indoc.getDocumentElement().normalize();
		NodeList nl = indoc.getElementsByTagName("*");
		for (int i=1; i<nl.getLength(); i++)  
		{
			Element elem = (Element) nl.item(i);
			System.out.println(elem.getNodeName() + "==>" + elem.getFirstChild().getNodeValue());
		}

		NameValuePair nvpPdl = new NameValuePair("_PDL","TEST");
    		NameValuePair nvpTkn = new NameValuePair("_TKN","PA31.1");
		NameValuePair nvpRtn = new NameValuePair("_RTN","DATA");
		NameValuePair nvpf1  = new NameValuePair("_f1","A");
		NameValuePair nvpf10  = new NameValuePair("_f10","3000");

		NameValuePair[] nvpout = {nvpPdl, nvpTkn, nvpRtn, nvpf1, nvpf10}; 
		return nvpout;
	}

code after which compiles but does not run....
Code:
	public static NameValuePair[] NVPFromXML(Document indoc)
	{
		indoc.getDocumentElement().normalize();
		NameValuePair[] nvpOut = null;

		NodeList nl = indoc.getElementsByTagName("*");
		for (int i=1; i<nl.getLength(); i++)  
		{
			Element elem = (Element) nl.item(i);
			System.out.println(elem.getNodeName() + "==>" + elem.getFirstChild().getNodeValue());
			nvpOut[i-1] = new NameValuePair(elem.getNodeName(), elem.getFirstChild().getNodeValue());  <===This is the line that fails
		}

	return nvpOut;
	}

I am sure it has to do with how I am populating my array..maybe I need to use the "fill" method to populate my array?

Thanks!
 
One last thing...I did try the below code to see if it would make a difference but I am still getting a Null Pointer Exception..

Code:
        for (int i=1; i<nl.getLength(); i++)
        {
            Element elem = (Element) nl.item(i);
            System.out.println(elem.getNodeName() + "==>" + elem.getFirstChild().getNodeValue());
            NameValuePair tmp = new NameValuePair(elem.getNodeName(), elem.getFirstChild().getNodeValue());
            Arrays.fill(nvpOut, i-1, i, tmp);
 
Listing 2:
NameValuePair[] nvpOut = null;

Here you define an array of type NameValuePair, but without any size.

nvpOut[i-1] is not accessible because nvpOut is null (you told so!).
Try this:
Code:
NameValuePair[] nvpOut =NameValuePair[nl.getLength()];



don't visit my homepage:
 
Yes. I realized my mistake right after I posted the listing.

Thanks for your response..you are right on target!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top