Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: NullPointer Exception problem

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default NullPointer Exception problem

    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerException;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    import java.lang.String;

    import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;
    import org.w3c.dom.Node;
    import org.w3c.dom.Element;
    import org.xml.sax.SAXException;
    import java.io.File;
    import java.io.IOException;


    public class twigstack {
    public static void main(String argv[]) {

    try {

    File fXmlFile = new File("d:\\twig.xml");
    //String filepath = "d:\\myproduct1.xml";
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
    doc.getDocumentElement().normalize();
    Element root=doc.getDocumentElement();
    //Element ar[];
    //Queue q=new Queue();
    //Queue q1=new Queue();
    Node[] ta=new Node[0];
    Node[] tf=new Node[0];
    Node[] tl=new Node[2];
    Node[] tj=new Node[1];
    Node[] td=new Node[1];
    t1 t=new t1();
    t2 tt=new t2();

    String st="//author[/fn/jane]/ln/doe";
    String sub=st.substring(2,8);
    String subf=st.substring(10,12);
    String subj=st.substring(13,17);
    String subl=st.substring(19, 21);
    String subd=st.substring(22, 25);
    //System.out.println(subf);
    //System.out.println(sub);

    if(root.hasChildNodes())
    {
    ta=t.mymethod(root,sub);
    tf=t.mymethod(root, subf);
    tl=t.mymethod(root, subl);
    }
    int n=ta.length;
    System.out.println(n);
    for (int i=0;i<n;i++)
    {
    Node l=ta[i];

    System.out.println(l.getNodeName());
    Element l1=(Element)l;
    System.out.println(l1.getAttribute("start"));
    //System.out.println("1");
    }
    tf=t.mymethod(root, subf);
    //System.out.println(tf[2].getNodeName());

    int n1=tf.length;
    //if(tf.)
    for (int j=0;j<n1;j++)
    {

    System.out.println(tf[j].getNodeName());
    Element el1=(Element)tf[j];
    //System.out.println("1");

    System.out.println(el1.getAttribute("start"));
    //System.out.println("1");
    }

    /*for (int j=0;j<tl.length;j++)
    {

    System.out.println(tl[j].getNodeName());
    Element el1=(Element)tl[j];
    //System.out.println("1");

    System.out.println(el1.getAttribute("start"));
    //System.out.println("1");
    }*/

    //System.out.println("1");

    }
    catch (Exception e) {
    e.printStackTrace();
    }


    }
    }







    //Another class:

    import org.w3c.dom.NodeList;
    import org.w3c.dom.Node;
    import org.w3c.dom.Element;
    public class t1 {

    Node[] mymethod (Node e,String sub)
    {
    //Element ele=(Element)e;
    Node[] copy=new Node[3];
    int j=0;
    NodeList n=e.getChildNodes();
    for(int i=0;i<n.getLength();i++)
    {

    Node f=n.item(i);
    if (f.getNodeType() == Node.ELEMENT_NODE)
    {
    //x++;
    //Element el = (Element) f;
    //System.out.println(f.getNodeName());
    String s=f.getNodeName();
    //System.out.println(sub);
    if(s.equalsIgnoreCase(sub))
    {
    copy[j]=f;
    Node k=copy[j];
    Element l =(Element)k;
    //System.out.println(l.getNodeName());
    j++;
    //System.out.println(s);
    }

    if (f.hasChildNodes())
    //x=el.getAttribute("start");

    mymethod(f,sub);
    else

    //System.out.println(" child of"+r+":"+p+":"+s1);
    continue;
    //x=ele.getAttribute("start");
    }
    }
    for (int i=0;i<j;i++)
    {

    System.out.println(copy[i].getNodeName());
    }
    //q.display();
    return copy;
    //x1--;
    //}

    }

    }




    //twig.xml
    <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
    - <allauthors end="16" level="0" start="1">
    - <author end="6" level="1" start="2">
    - <fn end="4" level="2" start="3">
    <jane end="4" level="3" start="4" />
    </fn>
    - <ln end="6" level="2" start="5">
    <poe end="6" level="3" start="6" />
    </ln>
    </author>
    - <author end="11" level="1" start="7">
    - <fn end="9" level="2" start="8">
    <john end="9" level="3" start="9" />
    </fn>
    - <ln end="11" level="2" start="10">
    <doe end="11" level="3" start="11" />
    </ln>
    </author>
    - <author end="16" level="1" start="12">
    - <fn end="14" level="2" start="13">
    <jane end="14" level="3" start="14" />
    </fn>
    - <ln end="16" level="2" start="15">
    <doe end="16" level="3" start="16" />
    </ln>
    </author>
    </allauthors>


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,942
    Thanks
    51
    Thanked 2,267 Times in 2,239 Posts

    Default Re: NullPointer Exception problem

    Please post the full text of any error messages you need help with.

    Also Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    KevinWorkman (June 25th, 2012)

  4. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointer Exception problem

    when i trying to access the nodename of "fn" value it gives nullpointerException.Plz help.

    <import org.w3c.dom.NodeList;
    import org.w3c.dom.Node;
    import org.w3c.dom.Element;
    public class t1 {
     
    Node[] mymethod (Node e,String sub)
    {
    //Element ele=(Element)e;
    Node[] copy=new Node[3];
    int j=0;
    NodeList n=e.getChildNodes();
    for(int i=0;i<n.getLength();i++)
    {
     
    Node f=n.item(i);
    if (f.getNodeType() == Node.ELEMENT_NODE)
    {
    //x++;
    //Element el = (Element) f;
    //System.out.println(f.getNodeName());
    String s=f.getNodeName();
    //System.out.println(sub);
    if(s.equalsIgnoreCase(sub))
    {
    copy[j]=f;
    Node k=copy[j];
    Element l =(Element)k;
    //System.out.println(l.getNodeName());
    j++;
    //System.out.println(s);
    }
     
    if (f.hasChildNodes())
    //x=el.getAttribute("start");
     
    mymethod(f,sub);
    else
     
    //System.out.println(" child of"+r+":"+p+":"+s1);
    continue;
    //x=ele.getAttribute("start");
    }
    }
    for (int i=0;i<j;i++)
    {
     
    System.out.println(copy[i].getNodeName());
    }
    //q.display();
    return copy;
    //x1--;
    //}
     
    }
     
    }
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerException;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    import java.lang.String;
     
    import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;
    import org.w3c.dom.Node;
    import org.w3c.dom.Element;
    import org.xml.sax.SAXException;
    import java.io.File;
    import java.io.IOException;
     
     
    public class twigstack {
    public static void main(String argv[]) {
     
    try {
     
    File fXmlFile = new File("d:\\twig.xml");
    //String filepath = "d:\\myproduct1.xml";
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
    doc.getDocumentElement().normalize();
    Element root=doc.getDocumentElement();
    //Element ar[];
    //Queue q=new Queue();
    //Queue q1=new Queue();
    Node[] ta=new Node[0];
    Node[] tf=new Node[0];
    Node[] tl=new Node[2];
    Node[] tj=new Node[1];
    Node[] td=new Node[1];
    t1 t=new t1();
    t2 tt=new t2();
     
    String st="//author[/fn/jane]/ln/doe";
    String sub=st.substring(2,8);
    String subf=st.substring(10,12);
    String subj=st.substring(13,17);
    String subl=st.substring(19, 21);
    String subd=st.substring(22, 25);
    //System.out.println(subf);
    //System.out.println(sub);
     
    if(root.hasChildNodes())
    {
    ta=t.mymethod(root,sub);
    tf=t.mymethod(root, subf);
    tl=t.mymethod(root, subl);
    }
    int n=ta.length;
    System.out.println(n);
    for (int i=0;i<n;i++)
    {
    Node l=ta[i];
     
    System.out.println(l.getNodeName());
    Element l1=(Element)l;
    System.out.println(l1.getAttribute("start"));
    //System.out.println("1");
    }
    tf=t.mymethod(root, subf);
    //System.out.println(tf[2].getNodeName());
     
    int n1=tf.length;
    //if(tf.)
    for (int j=0;j<n1;j++)
    {
     
    System.out.println(tf[j].getNodeName());
    Element el1=(Element)tf[j];
    //System.out.println("1");
     
    System.out.println(el1.getAttribute("start"));
    //System.out.println("1");
    }
     
    /*for (int j=0;j<tl.length;j++)
    {
     
    System.out.println(tl[j].getNodeName());
    Element el1=(Element)tl[j];
    //System.out.println("1");
     
    System.out.println(el1.getAttribute("start"));
    //System.out.println("1");
    }*/
     
    //System.out.println("1");
     
    }
    catch (Exception e) {
    e.printStackTrace();
    }
     
     
    }
    }

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,942
    Thanks
    51
    Thanked 2,267 Times in 2,239 Posts

    Default Re: NullPointer Exception problem

    it gives nullpointerException
    Please post the full text of any error messages you need help with. There is important information in the error message's text.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. exception handling problem
    By kbassi88 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 4th, 2012, 01:52 PM
  2. java.lang.nullpointer exception in beginner code
    By Bobba in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 6th, 2012, 05:37 PM
  3. Exception problem...plzz help
    By Arati2512 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 26th, 2012, 12:38 PM
  4. [SOLVED] Nullpointer exception when sending string
    By treshr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 26th, 2011, 04:36 AM
  5. Replies: 8
    Last Post: August 9th, 2011, 08:25 PM