Results 1 to 5 of 5
  1. #1
    svpriyan is offline Member
    Join Date
    Mar 2009
    Location
    London
    Posts
    8
    Rep Power
    0

    Default 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

    Java 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();
        }
    }
    Java 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();
    	}
    }
    Last edited by svpriyan; 04-17-2009 at 01:03 AM. Reason: ecited

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    you have the line of code where your error is. null pointer exception means that you are performing an operation on an object that is null. just fix this

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    before anyone can help you, we need to know exactly where the error is. Most of us do not have time to look through the code and find the line where the error occurs. if you mark it with something obvious like //<<<<<ERROR HERE>>>>>, we should be able to help you. However, as emceenugget said, by reading the error message, it is obvious that one of your variables in the createNodeElement(FindNodes) method has not been initialized.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    svpriyan is offline Member
    Join Date
    Mar 2009
    Location
    London
    Posts
    8
    Rep Power
    0

    Default

    Thanks for ur concerns,
    I EDITED where the ERROR is;pls refer the code
    Actually what i need is IF I DON'T HAVE Parent node( ie not available in the txt file) it should not be created, so I made an If clause there;

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    all suggestions below assume the line
    if (!(b.getParent().equals("")));
    throws the exception

    first of all, to compare a string variable to a string you place the String first, not the varible. e.g:
    Java Code:
    if("myString".equals(myStringVariable));
    // or for does not equal
    if(!"myString".equals(myStringVariable));
    as for your error, I would guess that either the reversal in the clause caused it, or your initialization of b does not work properly. You create and initialize b in your createDOMTree() method with the line
    FindNodes b = (FindNodes) it.next();
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Replies: 15
    Last Post: 10-12-2010, 05:51 PM
  2. Exception in thread "main" java.lang.NullPointerException
    By farooqhussain786 in forum New To Java
    Replies: 10
    Last Post: 04-03-2009, 06:31 AM
  3. Exception in thread "main" java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 0
    Last Post: 02-24-2009, 01:19 PM
  4. Replies: 1
    Last Post: 02-17-2009, 10:54 AM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •