I keep getting this error message when I try to run
----jGRASP exec: java DomParserExample

Exception in thread "main" java.lang.NumberFormatException: null

at java.lang.Integer.parseInt(Integer.java:615)
at DomParserExample.getIntValue(DomParserExample.java:152)
at DomParserExample.getAlbum(DomParserExample.java:111)
at DomParserExample.parseDocument(DomParserExample.java:89)
at DomParserExample.runExample(DomParserExample.java:35)
at DomParserExample.main(DomParserExample.java:177)

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

Here is my code that I have written:
Looks like the error is in line 107 but I cant figure it out.

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DomParserExample 
{

    //No generics
    List myAlbums;
    Document dom;


    public DomParserExample(){
        //create a list to hold the employee objects
        myAlbums = new ArrayList();
}

    public void runExample() 
   {

        //parse the xml file and get the dom object
        parseXmlFile();

        //get each employee element and create a Employee object
        parseDocument();

        //Iterate through the list and print the data
        printData();

    }


    private void parseXmlFile()
   {
        //get the factory
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        try {

               //Using factory get an instance of document builder
               DocumentBuilder db = dbf.newDocumentBuilder();

               //parse using builder to get DOM representation of the XML file
               dom = db.parse("albums.xml");


           }
         catch(ParserConfigurationException pce) 
         {
               pce.printStackTrace();
           }
         catch(SAXException se) 
         {
               se.printStackTrace();
           }
         catch(IOException ioe) 
         {
               ioe.printStackTrace();
           }
    }


    private void parseDocument()
   {
        //get the root elememt
        Element docEle = dom.getDocumentElement();

        //get a nodelist of <employee> elements
        NodeList nl = docEle.getElementsByTagName("album");
        if(nl != null && nl.getLength() > 0) 
         {
               for(int i = 0 ; i < nl.getLength();i++) 
            {

                //get the employee element
                Element el = (Element)nl.item(i);

                //get the Employee object
                Album e = getAlbum(el);

                //add it to list
                myAlbums.add(e);
               }
           }
   }


    /**
     * I take an employee element and read the values in, create
     * an Employee object and return it
     * @param empEl
     * @return
     */
    private Album getAlbum(Element albEl) 
   {

        //for each <employee> element get text or int values of 
        //name ,id, age and name
        String title = getTextValue(albEl,"Title");
        String artist = getTextValue(albEl,"Artist");
        int year = getIntValue(albEl,"Year");
        String category = albEl.getAttribute("category");

        //Create a new Employee with the value read from the xml nodes
        Album e = new Album(title,artist,year,category);

        return e;
    }


    /**
     * I take a xml element and the tag name, look for the tag and get
     * the text content 
     * i.e for <employee><name>John</name></employee> xml snippet if
     * the Element points to employee node and tagName is name I will return John  
     * @param ele
     * @param tagName
     * @return
     */
    private String getTextValue(Element ele, String tagName) 
      {
           String textVal = null;
           NodeList nl = ele.getElementsByTagName(tagName);
           if(nl != null && nl.getLength() > 0) {
               Element el = (Element)nl.item(0);
               textVal = el.getFirstChild().getNodeValue();
        }

        return textVal;
    }


    /**
     * Calls getTextValue and returns a int value
     * @param ele
     * @param tagName
     * @return
     */
    private int getIntValue(Element ele, String tagName) 
   {
        //in production application you would catch the exception
        return Integer.parseInt(getTextValue(ele,tagName));
    }

    /**
     * Iterate through the list and print the 
     * content to console
     */
    private void printData()
      {

        System.out.println("No of Albums '" + myAlbums.size() + "'.");

        Iterator it = myAlbums.iterator();
        while(it.hasNext()) {
            System.out.println(it.next().toString());
        }
    }


    public static void main(String[] args)
   {
        //create an instance
        DomParserExample dpe = new DomParserExample();

        //call run example
        dpe.runExample();
    }

}

I'm sorry its line 171 that I think the error is in

the actual error is, null means: there is no value assigned to the variable.
so, if you call a method and ask them to parse 'null' to a numeric value, it 'll fail.

No, the error can not have been thrown at line 171. it happened, as your error messages said, on this line:
at DomParserExample.getIntValue(DomParserExample.java:152)

return Integer.parseInt(getTextValue(ele,tagName));

as for the comment: //in production application you would catch the exception
so, you know for a fact this line throws an exception... You even put a comment about it .... why only in a production environment? would you risk putting code you never tested before in production?

In any decent application, production or not, firstly, you avoid exceptions being thrown. apparently: getTextValue(ele, tagName)) returns null.
either test for null first, or make sure it has a value.

I think getTextValue() this method is returning a value which can't be parsed as an Integer thats why you are getting the Number format exception.

Looking at the next line in the stack dump, the parseInt was called from line 111, so it is the "Year" tag that has a null value

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.