aspose file tools*
The moose likes XML and Related Technologies and the fly likes Parsing xml file using sax parser-NULLPOINTEREXCEPTION Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Parsing xml file using sax parser-NULLPOINTEREXCEPTION" Watch "Parsing xml file using sax parser-NULLPOINTEREXCEPTION" New topic
Author

Parsing xml file using sax parser-NULLPOINTEREXCEPTION

Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
i AM GETTING NULLPOINTER EXCEPTION WHEN I AM PARSING A XML FILE USING TESTPARSER.JAVA.
I need to parse a text file, text file has different records(each record has multiple rows). I need to pass insert these values into tables.
I created a xml format for the test file. I need to parse this xmlfile and load into a TestParser object. Based on this format I need to parse the text files.
the format of xml file(employeeformat.xml) is
<EMPLOYEEFORMAT>
<!--AP Invoice Record EBD Entity-->
<map name="xyz" delimiter="|" RecordType="00" TableName="EMPLOYEE">
<field name = "name" order_id = "1"column_name="EMPLOYEE.NAME" />
<field name = "address" order_id = "2" column_name="EMPLOYEE.ADDRESS" />
<field name = "state" order_id = "3" column_name="EMPLOYEE.STATE" />
<field name = "country" order_id = "4" column_name="EMPLOYEE.COUNTRY" />
<field name = "phoneno" order_id = "5" column_name="EMPLOYEE.PHONENO" />
<field name = "email" order_id = "6" column_name="EMPLOYEE.EMAIL" />
<field name = "age" order_id = "7" column_name="EMPLOYEE.AGE" />
<field name = "sex" order_id = "8" column_name="EMPLOYEE.SEX" />
<field name = "buyer" order_id = "9" column_name="EMPLOYEE.BUYER" />
<field name = "seller" order_id = "10" column_name="EMPLOYEE.SELLER" />
</map>
</EMPLOYEEFORMAT>

The code I am using to parse this is
import java.util.*;

import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.SAXParserFactory;
import org.apache.xerces.parsers.SAXParser;


public class Testparser extends DefaultHandler {
private static Testparser myself = null;
/** XML Parser Class */
public static final String
DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser";
/** XML source file to be parsed */
private String xmlsource = "C:/employeeformat.xml";
/** map list */
private HashMap mapList = null;

private static String MAP_ELEMENT = "map";
public static String MAP_NAME_ATTR = "name";
public static String RECORD_TYPE_ATTR = "RecordType";
public static String TABLE_NAME_ATTR = "TableName";
public static String DELIMITER_TYPE_ATTR = "delimiter";
private static String FIELD_ELEMENT = "field";
public static String FIELD_NAME_ATTR = "name";
public static String FIELD_LIST_ATTR = "fieldList";
public static String COLUMN_ATTR = "column_name";
public static String COLUMN_LIST_ATTR = "columnList";
public static String ORDER_ID_ATTR = "order_id";
public static String ORDER_ID_LIST = "orderidList";
private StringBuffer buffer = null; //xml buffer
private String mapName = null; //map name
private HashMap tableProps = null; //table props currently being parsed
private ArrayList fieldList = null; //field list currently being parsed
private HashMap fieldProps = null; //field props currently being parsed
private ArrayList columnList = null;
private ArrayList orderidList = null;
/**
* Constructor
*/
private TestParser() {
this(Globals.TestParserXMLFile);
//parse();
System.out.println("I am here");
}

/**
* Constructor that takes a xml file to parse
*/
private TestParser(String xml) {
super();
this.xmlsource = xml;
// this.xmlsource = Globals.TestParserXMLFile;
// parse();
}
/* public static void main(String[] args) throws Exception {
Globals.load();
System.out.println("I am in TestPArser main method");
TestParser DataEntity1 = new TestParser();
} */
/**
* returns instance of this class
*/
public synchronized static TestParser getInstance() {
if (myself == null) {
myself = new TestParser();
}
return myself;
}
public static void main(String[] args) throws Exception {

XMLReader p = XMLReaderFactory.createXMLReader( "org.apache.xerces.parsers.SAXParser" );
p.setContentHandler(loader);
p.parse("C:/employeeformat.xml");
System.out.println("I am in parse method");
}
/**
* gets file props for alias
*/
public synchronized HashMap get(String alias) {
return (HashMap)mapList.get(alias);
}

/**
* Start document
*/
public synchronized void startDocument() {
mapList = new HashMap();
};
/**
* Start element.
*/
public synchronized void startElement(String name, Attributes attrs) {
buffer = new StringBuffer();
// if map element, create new field list, reset delimiter, and get map props
if (name.equals(MAP_ELEMENT)) {
tableProps = new HashMap();
fieldList = new ArrayList();
mapName = (String)attrs.getValue(MAP_NAME_ATTR);
tableProps.put(RECORD_TYPE_ATTR,(String)attrs.getValue(RECORD_TYPE_ATTR));
tableProps.put(TABLE_NAME_ATTR, (String)attrs.getValue(TABLE_NAME_ATTR));
tableProps.put(MAP_NAME_ATTR,(String)attrs.getValue(MAP_NAME_ATTR));
tableProps.put(DELIMITER_TYPE_ATTR,(String)attrs.getValue(DELIMITER_TYPE_ATTR));
}
// if field element, create new field props and get field props
else if (name.equals(FIELD_ELEMENT)) {
fieldProps = new HashMap();
fieldProps.put(FIELD_NAME_ATTR,attrs.getValue(FIELD_NAME_ATTR));
fieldProps.put(COLUMN_ATTR,attrs.getValue(COLUMN_ATTR));
fieldProps.put(ORDER_ID_ATTR,attrs.getValue(ORDER_ID_ATTR));
}
}
/**
* Characters.
*/
public synchronized void characters(char ch[], int start, int length) {
buffer.append(ch, start, length);
}
/**
* End element.
*/
public synchronized void endElement(String name) {
String value = buffer.toString().trim();
// if table element, add table props to map list
if (name.equals(MAP_ELEMENT)) {
mapList.put(mapName,tableProps);
}
// if field element, add field props to field list
// add field list to map props
else if (name.equals(FIELD_ELEMENT)) {
fieldList.add(fieldProps);
tableProps.put(FIELD_LIST_ATTR,fieldList);
}
}
}
The problem is i am getting null pointer exception when i am using this code. Do you guys
have any idea why I am getting null pointer exception. Do you have better way of parsing the xml files.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Bhasker,
Your source code does not even compile
Cheer
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parsing xml file using sax parser-NULLPOINTEREXCEPTION
 
Similar Threads
xml, dtd, and parsing code
Problems Parsing an XML file - Need Help.
2 Synchronization blocks with same object
XML document is not getting parsed properly.
how is 'encapsulation' broken in this situation of mine ?