Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7020661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:20:49+00:00 2026-05-27T23:20:49+00:00

I’m trying to write an SAX XML Parser in Java and I keep getting

  • 0

I’m trying to write an SAX XML Parser in Java and I keep getting a null pointer exception that I can’t seem to figure out how to fix. Here is the stack trace:

Exception in thread "main" java.lang.NullPointerException
    at SAXParserExample.endElement(SAXParserExample.java:91)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at javax.xml.parsers.SAXParser.parse(Unknown Source)
    at javax.xml.parsers.SAXParser.parse(Unknown Source)
    at SAXParserExample.parseDocument(SAXParserExample.java:43)
    at SAXParserExample.runExample(SAXParserExample.java:29)
    at SAXParserExample.main(SAXParserExample.java:107)

Here is the main class for the SAX Parser:

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

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class SAXParserExample extends DefaultHandler{

    List<DataRow> myFiles;

    private String tempVal;

    //to maintain context
    private DataRow tempFile;


    public SAXParserExample(){
        myFiles = new ArrayList<DataRow>();
    }

    public void runExample() {
        parseDocument();
        printData();
    }

    private void parseDocument() {

        //get a factory
        SAXParserFactory spf = SAXParserFactory.newInstance();
        try {

            //get a new instance of parser
            SAXParser sp = spf.newSAXParser();

            //parse the file and also register this class for call backs
            sp.parse("./src/filelist.xml", this);

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

    /**
     * Iterate through the list and print
     * the contents
     */
    private void printData(){

        System.out.println("No of Files '" + myFiles.size() + "'.");

        Iterator<DataRow> it = myFiles.iterator();
        while(it.hasNext()) {
            System.out.println(it.next().toString());
        }
    }


    //Event Handlers
    public void startElement(String uri, String localName, String qName) throws SAXException {
        //reset
        tempVal = "";
        if(qName.equalsIgnoreCase("DATAROW")) {
            //create a new instance of Datarow
            tempFile = new DataRow();
        }
    }


    public void characters(char[] ch, int start, int length) throws SAXException {
        tempVal = new String(ch,start,length);
    }

    public void endElement(String uri, String localName, String qName) throws SAXException {

        if(qName.equalsIgnoreCase("DATAROW")) {
            //add it to the list
            myFiles.add(tempFile);

        }else if (qName.equalsIgnoreCase("ID")) {
            tempFile.setID(Integer.parseInt(tempVal));
        }else if (qName.equalsIgnoreCase("FILENAME")) {
            tempFile.setFileName(tempVal);
        }else if (qName.equalsIgnoreCase("SEARCHKEY")) {
            tempFile.setSearchKey(tempVal);
        }else if (qName.equalsIgnoreCase("DATEADDED")) {
            tempFile.setDateAdded(tempVal);
        }else if (qName.equalsIgnoreCase("APPLICATIONID")) {
            tempFile.setApplicationID(tempVal);
        }else if (qName.equalsIgnoreCase("DISPLAYFILENAME")) {
            tempFile.setDisplayFileName(tempVal);
        }
    }

    public static void main(String[] args){
        SAXParserExample spe = new SAXParserExample();
        spe.runExample();
    }
}

Here is the DataRow class used in the main class:

public class DataRow {

    private String fileName = "", searchKey = "", dateAdded = "", applicationID = "", displayFileName = "";

    private int id = 0;

    public DataRow(){

    }

    public DataRow(int id, String fileName, String searchKey, String dateAdded, String applicationID, String displayFileName) {
        this.id  = id;
        this.fileName = fileName;
        this.searchKey = searchKey;
        this.dateAdded = dateAdded;
        this.applicationID = applicationID;
        this.displayFileName = displayFileName;

    }
    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public int getID() {
        return id;
    }

    public void setID(int id) {
        this.id = id;
    }

    public String getSearchKey() {
        return searchKey;
    }

    public void setSearchKey(String searchKey) {
        this.searchKey = searchKey;
    }

    public String getDateAdded() {
        return dateAdded;
    }

    public void setDateAdded(String dateAdded) {
        this.dateAdded = dateAdded;
    }   

    public String getApplicationID() {
        return applicationID;
    }

    public void setApplicationID(String applicationID) {
        this.applicationID = applicationID;
    }

    public String getDisplayFileName() {
        return displayFileName;
    }

    public void setDisplayFileName(String displayFileName) {
        this.displayFileName = displayFileName;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("File Details - ");
        sb.append("ID:" + getID());
        sb.append(", ");
        sb.append("Filename:" + getFileName());
        sb.append(", ");
        sb.append("Search Key:" + getSearchKey());
        sb.append(", ");
        sb.append("Date Added:" + getDateAdded());
        sb.append(", ");
        sb.append("Application ID:" + getApplicationID());
        sb.append(", ");
        sb.append("Display Filename:" + getDisplayFileName());
        sb.append(".");

        return sb.toString();
    }
}

And here is the XML file I’m attempting to parse:

<DATAROW><ID>61</ID><FILENAME>TestFile.txt</FILENAME><SEARCHKEY>12345</SEARCHKEY><DATEADDED>2012-1-6 9.12.32.0</DATEADDED><APPLICATIONID>PCIS</APPLICATIONID><DISPLAYFILENAME>TestFile.txt</DISPLAYFILENAME></DATAROW><DATAROW><ID>44</ID><FILENAME>TestFile.txt</FILENAME><SEARCHKEY>12345</SEARCHKEY><DATEADDED>2012-2-5 14.39.50.0</DATEADDED><APPLICATIONID>PCIS</APPLICATIONID><DISPLAYFILENAME>TestFile.txt</DISPLAYFILENAME></DATAROW>

The XML file is formatted that way because eventually it’s going to be parsing metadata return from a database in the form of a single string.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:20:50+00:00Added an answer on May 27, 2026 at 11:20 pm

    I haven’t used SAX for quite some time, so may be wrong, but my guess is that that you should use the localName, not the qname here, since there are no qualified names (ie. the names that have some namespace like ‘namespace:name’) in your XML. Therefore, most probably the qname provided is null and, since you access it for comparison, you get an NPE.

    According to the javadoc (http://docs.oracle.com/javase/1.4.2/docs/api/org/xml/sax/helpers/DefaultHandler.html#endElement(java.lang.String, java.lang.String, java.lang.String) ), the qname provided may be empty if it is not available in the XML document.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.