NullPointerException

Hello guys,

I get this error when I try this code out but I don't know what's causing it. If you know the answer, please let me know.

This is the JSP page:

<%@ page contentType="text/html"%>

<%@ page import="javax.xml.parsers.*"%>

<%@ page import="org.w3c.dom.*"%>

<%@ page import="dombean.*" %>

<jsp:useBean id="domparser" class="dombean.MyDOMParserBean" />

<%

Document doc = domparser.getDocument("c:/kris/stocks.xml");

NodeList nl = doc.getElementsByTagName("message");

%>

<html>

<body>

<%= nl.item(0).getFirstChild().getNodeValue() %>

</body>

</html>

This is the XML stocks.xml:

<?xml version="1.0" encoding="UTF-8"?>

<portfolio>

<stock>

<symbol>SUNW</symbol>

<name>Sun Microsystems</name>

<price>17.1</price>

</stock>

<stock>

<symbol>AOL</symbol>

<name>America Online</name>

<price>51.05</price>

</stock>

<stock>

<symbol>IBM</symbol>

<name>International Business Machines</name>

<price>116.10</price>

</stock>

<stock>

<symbol>MOT</symbol>

<name>MOTOROLA</name>

<price>15.20</price>

</stock>

</portfolio>

-

and this is the bean being used:

package dombean;

import javax.xml.parsers.*;

import org.w3c.dom.*;

import java.io.*;

publicclass MyDOMParserBean

implements java.io.Serializable{

public MyDOMParserBean(){

}

publicstatic Document

getDocument(String file)throws Exception{

// Step 1: create a DocumentBuilderFactory

DocumentBuilderFactory dbf =

DocumentBuilderFactory.newInstance();

// Step 2: create a DocumentBuilder

DocumentBuilder db = dbf.newDocumentBuilder();

// Step 3: parse the input file to get a Document object

Document doc = db.parse(new File(file));

return doc;

}

}

Thanks for the help! Kris.

[3119 byte] By [XIII_] at [2007-9-26 4:35:13]
# 1

A stack trace would help locate your problem.

Try putting a System.out.println() statement or two in your JSP.

Like this:

<%

System.out.println("About to get Document");

Document doc =

domparser.getDocument("c:/kris/stocks.xml");

System.out.println("About to get Element");

NodeList nl = doc.getElementsByTagName("message");

System.out.println("Got both");

%>

Depending on what prints, you should be able to narrow down where the code crashes.

WynEaston at 2007-6-29 17:52:12 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...