2

Trying to figure out debug the null point exception occuring in my code from lyndas.com lectures written for the xml content from a link activated on localhost . following is the code of my View class and the AppMain class

View.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.net.*;
import java.util.ArrayList;

public class View extends JFrame implements ActionListener {

    private static final long serialVersionUID = 12345L;
    public ArrayList<String> titles = new ArrayList<String>();
    public ArrayList<String> descriptions = new ArrayList<String>();
    public ArrayList<String> links = new ArrayList<String>();
    public ArrayList<Integer> prices = new ArrayList<Integer>();
    public ArrayList<Number> lengths = new ArrayList<Number>();
    public JList list;
    public JComboBox combo;
    public JTextArea textArea = new JTextArea();
    public JLabel priceLabel = new JLabel();
    public JLabel lengthLabel = new JLabel();
    public JScrollPane textScroller;
    public View()
    {
        super("Backpack CA");
        setLayout(new FlowLayout());
        **loadData("http://localhost:8080/using_drivers/data.jsp");**           
    }

    public void actionPerformed(ActionEvent e) {            
    }

    public void loadData(String xmlURL)
    {
        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();                
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(new URL(xmlURL).openStream());
            doc.getDocumentElement().normalize();

            NodeList nodes = doc.getElementsByTagName("tour");              
            for (int temp = 0; temp < nodes.getLength(); temp++) {                  
               Node n = nodes.item(temp);
               if (n.getNodeType() == Node.ELEMENT_NODE) {       
                  Element e = (Element) n;    
                  titles.add(getTagValue("tourTitle", e));
                  descriptions.add(getTagValue("description", e));
                  links.add(getTagValue("link", e).replaceAll("\\s+", ""));
                  prices.add(Integer.parseInt(getTagValue("price", e)));
                  lengths.add(Integer.parseInt(getTagValue("length", e)));
                  **System.out.println(getTagValue("tourTittle", e));**
               }
            }               

          } catch (Exception e) {
            e.printStackTrace();
          }
    }

    private static String getTagValue(String sTag, Element eElement) {
        NodeList nlList; 
        **nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();**        
            Node nValue = (Node) nlList.item(0);         
        return nValue.getNodeValue();
     }

}

AppMain.java

import javax.swing.*;

public class AppMain {

    public static void main(String[] args) {
        **View testView = new View();**
        testView.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        testView.setSize(480,320);
        testView.setVisible(true);
    }    
}
1
  • 5
    At the very least, you may want to provide the stack trace. Bonus points if you talk about what exactly is going wrong and when, and what you've tried in order to fix it. Commented Jun 6, 2013 at 6:01

1 Answer 1

1

eElement.getElementsByTagName(sTag) or eElement.getElementsByTagName(sTag).item(0) return null. Try splitting that insturction to figure out:

NodeList nl = eElement.getElementsByTagName(sTag);
Node n = nl.item(0);
nlList = n.getChildNodes();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.