Exception in thread "main" java.lang.NullPointerException
Hai friends,
Exception in thread "main" java.lang.NullPointerException
at Associatior.GenerateMyXML.createNodeElement(Genera teMyXML.java:136)
at Associatior.GenerateMyXML.createDOMTree(GenerateMy XML.java:112)
at Associatior.GenerateMyXML.GenerateXML(GenerateMyXM L.java:38)
at Associatior.GenerateMyXML.main(GenerateMyXML.java: 185)
this error leads to not genearate an XML file for me, I need to solve this error.
Could any 1 help me on it
Priya
Code:
package Associatior;
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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.Text;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import java.io.BufferedReader;
import java.io.FileReader;
public class GenerateMyXML {
//No generics
List myData;
Document dom;
public GenerateMyXML() throws FileNotFoundException {
//create a list to hold the data
myData = new ArrayList();
//initialize the list
loadData();
//Get a DOM object
createDocument();
}
public void GenerateXML() {
System.out.println("Program Started .. ");
createDOMTree();
printToFile();// generate and print to FILE called conceptxml.xml
System.out.println("XML Generated file successfully.");
}
// Loading data
private void loadData() throws FileNotFoundException {
BufferedReader s = null;
try {
// The file can be placed to the directory called D:\\myjava\\Weka\\Associatior\\text.txt in my case, though can modify the path in the code.
s = new BufferedReader(new FileReader("D:\\myjava\\text.txt"));
// Note that in between child and parent node there will be ‘a space’. More over, there won’t be any spaces in the names of the nodes.
// For example “Nog te rubriceren” should be renamed as “Nog_te_rubriceren” using underscores.
String s2 = s.readLine();
while (s2 != null) {
String[] s3 = s2.split(" ");
if (s3.length > 1) {
myData.add(new FindNodes(s3[0], s3[1]));
} else {
myData.add(new FindNodes(s2));
}
s2 = s.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (s != null) {
try {
s.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
private void createDocument() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
//create an instance of DOM
dom = db.newDocument();
} catch (ParserConfigurationException pce) {
// Any Error
System.out.println("Error while trying to instantiate DocumentBuilder " + pce);
System.exit(1);
}
}
private void createDOMTree() {
//create the root element <ConceptSet> is the Root ELE
Element rootEle = dom.createElement("ConceptSet");
dom.appendChild(rootEle);
Iterator it = myData.iterator();
while (it.hasNext()) {
FindNodes b = (FindNodes) it.next();
//For each FindNodes object create <FindNodes> element and attach it to root
Element NodeEle = createNodeElement(b);
rootEle.appendChild(NodeEle);
}
}
private Element createNodeElement(FindNodes b) {
Element NodeEle = dom.createElement("Concept");
Element SubNode = dom.createElement("Node");
Text authText = dom.createTextNode(b.getNode());
SubNode.appendChild(authText);
NodeEle.appendChild(SubNode);
/*
Element titleEle = dom.createElement("Parent");
Text titleText = dom.createTextNode(b.getParent());
titleEle.appendChild(titleText);
NodeEle.appendChild(titleEle);
return NodeEle;
*/
<ERROR>
// =========ERROR IS HERE======
//if clause
// Actually what i need is IF I DONT HAVE Parent node( ie not available) it should NOT be created for that , so what i did was like this
// I hope my coding on checking the parent NODe is in ERROR
<ERROR>
if (!(b.getParent().equals("")))
{
Element titleEle = dom.createElement("Parent");
Text titleText = dom.createTextNode(b.getParent());
titleEle.appendChild(titleText);
NodeEle.appendChild(titleEle);
return NodeEle;
}
else
{
return NodeEle;
}
}
// write to a File
private void printToFile() {
try {
//print
OutputFormat format = new OutputFormat(dom);
format.setIndenting(true);
// Either way to see the Generated XML file
//to generate output to console use this serializer
// XMLSerializer serializer = new XMLSerializer(System.out, format);
//to generate a file output use fileoutputstream instead of system.out
XMLSerializer serializer = new XMLSerializer(new FileOutputStream(new File("conceptxml.xml")), format);
serializer.serialize(dom);
} catch (IOException ie) {
ie.printStackTrace();
}
}
public static void main(String args[]) throws FileNotFoundException {
//create an instance
GenerateMyXML xce = new GenerateMyXML();
//run the GenerateXML to generate
xce.GenerateXML();
}
}
Code:
package Associatior ;
public class FindNodes {
private String parent;
private String node;
public FindNodes(String node , String parent ) {
this.node = node;
this.parent = parent;
}
public FindNodes( String node) {
this.node = node;
}
public String getNode() {
return node;
}
public void setNode(String node) {
this.node = node;
}
public String getParent() {
return parent;
}
public void setParent(String parent) {
this.parent = parent;
}
// tostring
public String toString() {
StringBuffer NodeP = new StringBuffer();
NodeP.append(" { FindNodes Details ");
NodeP.append("parent:" + getParent());
NodeP.append(", ");
NodeP.append("node:" + getNode());
NodeP.append(". } \n");
return NodeP.toString();
}
}