Hi guys, wonder if you could help me with a problem I am having. I am working on a app that parses an XML file, and when executed from my IDE(Netbeans) all works flawlessly, however when I deploy and execute my app via Webstart I get a NullPointerException on the following line of code:

Element root = doc.getDocumentElement();

The XML file is packaged in the jar with the rest of the application, and is validated using a DTD that is also in the jar.

I really can't suss this one out, any help would be greatly appreciated.

Also I have found something else out about my problem...

If I remove the encoding attribute from my XML document the same error occurs when executing via my IDE.

So it seems the problem is something to do with web start not reading this attribute some how?

Also I have found something else out about my problem...

If I remove the encoding attribute from my XML document the same error occurs when executing via my IDE.

So it seems the problem is something to do with web start not reading this attribute some how?

Try removing DOCTYPE as well namespace tags from xml.

Once you packed XML file into JAR/WAR/EAR use ClassName.class.getResourceAsStream() Method.

Here is an example:

Std.xml [XML File]

<?xml version="1.0" ?>
<Students>
     <Std ClassName="1st">
               <Roll>10</Roll>
               <Name>Mr. Raj</Name>
     </Std>
     <Std ClassName="3rd">
               <Roll>11</Roll>
               <Name>Mr. Rohan</Name>
     </Std>
</Students>

ReadStd.java

import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;

public class ReadStd {
    public static void main(String []args) throws Exception {
             InputStream ii=ReadStd.class.getResourceAsStream("Std.xml");

              DocumentBuilderFactory fact=DocumentBuilderFactory.newInstance();
              DocumentBuilder build=fact.newDocumentBuilder();
   
               Document doc=build.parse(ii);

                Element root=doc.getDocumentElement();
                System.out.println(root.getTagName());

                NodeList list=root.getChildNodes();
                System.out.println("Total Nodes : " + list.getLength());

                  int i;

                 for(i=0;i<list.getLength();i++) {
                           Node t=list.item(i);
                            if(t.hasAttributes())
                                   System.out.print("Node has an attribute");
                            System.out.println(t.getNodeName() + "    "  + t.getNodeType() +  "   " + t.getNodeValue()); 
                            if(t.hasChildNodes()) {
                                                  NodeList p=t.getChildNodes();
                                                   for(int j=0;j<p.getLength();j++) {
				Node t1=p.item(j);
                                		System.out.println(t1.getNodeName() + "    "  + t1.getNodeType() +  "   " + t1.getNodeValue()); 
                                                      } 
                              } 
                  }
   }
}

Read more about manifest and jar.
http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html
m.mf [ Manifest File]

Main-Class: ReadStd

Note: All files (.java, .xml, and m.mf) must be placed in common folder.

Now, compile ReadStd.java and create Jar.

>java ReadStd.java
>jar -cvfm kk.jar m.mf .

Try removing DOCTYPE as well namespace tags from xml.

I get the following errors if i do this:

run:
Warning: validation was turned on but an org.xml.sax.ErrorHandler was not set, which is probably not what is desired.  Parser will use a default ErrorHandler to print the first 10  errors.  Please call the setErrorHandler method to fix this.
Error: URI = "file:///Users/lee/Projects/Java/Capitalize/src/org/caine/lee/games/capitalize/resources/mapData.xml", Line = "5", : Document root element "MapData", must match DOCTYPE root "null". 
Error: URI = "file:///Users/lee/Projects/Java/Capitalize/src/org/caine/lee/games/capitalize/resources/mapData.xml", Line = "5", : Document is invalid: no grammar found. 
Exception in thread "main" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredTextImpl
        at org.caine.lee.games.capitalize.Helpers.getMapData(Helpers.java:79)
        at org.caine.lee.games.capitalize.GameCanvas.<init>(GameCanvas.java:49)
        at org.caine.lee.games.capitalize.Capitalize.setupGame(Capitalize.java:30)
        at org.caine.lee.games.capitalize.Capitalize.<init>(Capitalize.java:22)
        at org.caine.lee.games.capitalize.Capitalize.main(Capitalize.java:40)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

Once you packed XML file into JAR/WAR/EAR use ClassName.class.getResourceAsStream() Method.

Here is an example:

Std.xml [XML File]

<?xml version="1.0" ?>
<Students>
     <Std ClassName="1st">
               <Roll>10</Roll>
               <Name>Mr. Raj</Name>
     </Std>
     <Std ClassName="3rd">
               <Roll>11</Roll>
               <Name>Mr. Rohan</Name>
     </Std>
</Students>

ReadStd.java

import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;

public class ReadStd {
    public static void main(String []args) throws Exception {
             InputStream ii=ReadStd.class.getResourceAsStream("Std.xml");

              DocumentBuilderFactory fact=DocumentBuilderFactory.newInstance();
              DocumentBuilder build=fact.newDocumentBuilder();
   
               Document doc=build.parse(ii);

                Element root=doc.getDocumentElement();
                System.out.println(root.getTagName());

                NodeList list=root.getChildNodes();
                System.out.println("Total Nodes : " + list.getLength());

                  int i;

                 for(i=0;i<list.getLength();i++) {
                           Node t=list.item(i);
                            if(t.hasAttributes())
                                   System.out.print("Node has an attribute");
                            System.out.println(t.getNodeName() + "    "  + t.getNodeType() +  "   " + t.getNodeValue()); 
                            if(t.hasChildNodes()) {
                                                  NodeList p=t.getChildNodes();
                                                   for(int j=0;j<p.getLength();j++) {
				Node t1=p.item(j);
                                		System.out.println(t1.getNodeName() + "    "  + t1.getNodeType() +  "   " + t1.getNodeValue()); 
                                                      } 
                              } 
                  }
   }
}

Read more about manifest and jar.
http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html
m.mf [ Manifest File]

Main-Class: ReadStd

Note: All files (.java, .xml, and m.mf) must be placed in common folder.

Now, compile ReadStd.java and create Jar.

>java ReadStd.java
>jar -cvfm kk.jar m.mf .

If I parse using a inputstream I get the following error:

/Users/lee/Projects/Java/Capitalize/mapData.dtd (No such file or directory)

Which is the DTD the XML document refers too. Is there a way to set the DTD programatically rather than in the document?

> I really can't suss this one out, any help would be greatly
> appreciated.

A small example/project which demonstrates the problem at hand might help others in hacking on the issue. Without knowing the way you have organized your project, it would be difficult to come up with a solution.

> I really can't suss this one out, any help would be greatly
> appreciated.

A small example/project which demonstrates the problem at hand might help others in hacking on the issue. Without knowing the way you have organized your project, it would be difficult to come up with a solution.

I think I may have sussed it now, and will be posting the solution soon.

The strange thing is that the way I am getting resources(which I am using for images, sounds and the XML) works fine for images and sounds(via the IDE and via web start), but only works via the IDE for the XML :/

But like I said I think I may have sussed it. I will post the solution soon, in case others have a similar problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.