11 Replies Latest reply: Dec 5, 2009 7:48 AM by PhHein RSS

    java.lang.NullPointerException javax.xml.parsers.DocumentBuilder.parse

    843789
      Hi all,

      i have a problem by solving an error in my code. The Code is mainly from Ian Darwin.

      The code i am running works with j2sdk1.4.2_04. But now i have to bring it to work with jdk1.6.0_13.

      The code parses xml documents. With small xml documents the code works. With large xml documents i get the following error while running the produced class file.

      Exception in thread "main" java.lang.NullPointerException
      at com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl.setChunkIndex(DeferredDocumentImpl.java:1944)
      at com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl.appendChild(DeferredDocumentImpl.java:644)
      at com.sun.org.apache.xerces.internal.parsers.AbstractDOMParser.characters(AbstractDOMParser.java:1191)
      at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.characters(XMLDTDValidator.java:862)
      at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:463)
      at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
      at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
      at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
      at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:225)
      at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:283)
      at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)
      at XParse.parse(XParse.java:38)
      at XParse$JFileChooserrv.<init>(XParse.java:119)
      at XParse.main(XParse.java:213)

      I know what a java.lang.NullPointerException mens. But i don't know where i have to look for. Specially i don't know what or where "com.sun.org.apache...." is.

      Is there a package that a have to add to the environment? Can some one tell my where i can find this package?

      I wrote the code for some years ago, 2006 or so. With the knew jdk1.6.0_13 some thinks chance in the environment. Couldn't find what exactly.

      The code has only 215 lines, but some how i can't add it to this Message, because Maximum allowed is only 7500.

      Is there an other Forum, which may is better for my question?
        • 1. Re: java.lang.NullPointerException javax.xml.parsers.DocumentBuilder.parse
          800308
          Nah... Just post your code... between a pair of &#123;code} tags... Use multiple posts to defeat that bullship 7500 char limit.

          We'll also need to know which line of your code triggers the problem... mark it in the source-code with: // <<<< ERROR

          If you don't know what I'm talking about then read through this article: [How To Read a Stack Trace|http://www.0xcafefeed.com/2004/06/of-thread-dumps-and-stack-traces/]

          And we may also need to know a more about your environment, test data, etc, etc... but lets cross that bridge when we come to it... Let's start with source code and the stacktrace.

          Cheers. Keith.
          • 2. Re: java.lang.NullPointerException javax.xml.parsers.DocumentBuilder.parse
            843789
            I could not find much about it.

            When i googled it, there was a bug raised for this exception. They have provided a workaround for this.

            You can try it.

            [http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6181020]

            Hope this helps.
            • 3. Re: java.lang.NullPointerException javax.xml.parsers.DocumentBuilder.parse
              800308
              Pradhip,

              That was a 1.4->1.5 regress bug, which was (apparently) going to be fixed by a imminent "merge" of the latest apache xerces code.
              Work Around      

              To avoid this problem set the following feature to 'false': "http://apache.org/xml/features/dom/defer-node-expansion".
              This will not effect the performance much.

              Evaluation      

              this is being accepted for JAXP 1.3.x.
              it is already fixed in Mustang as a result of the Apache merge.
              Posted Date : 2006-05-04 14:52:41.0

              As indicated previously, the issue has already been fixed in **jaxp 1.4/jdk6.** We currently do not have plan to update jaxp 1.3 > or jdk5 for this issue. The work-around helps to avoid the issue, so as using jaxp 1.4 in a jdk5 environment.
              Posted Date : 2007-06-16 00:46:12.0
              I can't imagine a recurrence of the same regression... though stranger things have certainly happened.

              Cheers. Keith.
              • 4. Re: java.lang.NullPointerException javax.xml.parsers.DocumentBuilder.parse
                843789
                Here is the code:
                import java.io.*;
                import javax.xml.parsers.*;
                import org.w3c.dom.*;
                import org.xml.sax.*;
                import javax.swing.*;
                import java.awt.event.*;
                import java.io.*;
                import java.util.*;
                import javax.swing.*;
                import java.awt.Container;
                import javax.swing.JTextArea;
                
                
                /**
                 * This code is mainly from @author Ian Darwin, ian@darwinsys.com
                 */
                public class XParse {
                
                     /** Convert the file */
                     public static void parse(File file, boolean validate) {
                          try {
                               System.err.println("");
                               String fileName = file.getAbsolutePath();
                               System.err.println("Parsing " + fileName + "...");
                
                               // Make the document a URL so relative DTD works.
                               //String uri = new File(fileName).getAbsolutePath();
                               //System.err.println(uri);
                               DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
                               if (validate)
                               f.setValidating(true);
                               DocumentBuilder p = f.newDocumentBuilder();
                               p.setErrorHandler(new MyErrorHandler(System.err));
                               //XmlDocument doc = XmlDocument.createXMLDocument(file);
                               boolean vaild =  p.isValidating();
                               if (vaild) {
                                    System.out.println("yes parsing");
                                    Document doc = p.parse(file); // <<<< ERROR
                               }
                               System.out.println("Parsed OK");
                
                          } catch (SAXParseException ex) {
                               System.err.println("+================================+");
                               System.err.println("|       *SAX Parse Error*        |");
                               System.err.println("+================================+");
                               System.err.println(ex.toString());
                               System.err.println("At line " + ex.getLineNumber());
                               System.err.println("+================================+");
                          } /**catch (RuntimeException ex) {
                               System.err.println("+================================+");
                               System.err.println("|       *SAX Parse Error*        |");
                               System.err.println("+================================+");
                               System.err.println(ex.toString());
                               //System.err.println("At line " + ex.getLineNumber());
                               //System.err.println("At line " + ex.getMessage());
                               System.err.println("+================================+");
                
                          }**/ catch (SAXException ex) {
                
                               System.err.println("+================================+");
                               System.err.println("|          *SAX Error*           |");
                               System.err.println("+================================+");
                
                               System.err.println(ex.toString());
                               System.err.println("+================================+");
                          /*}} catch (SAXNotRecognizedException  ex) {
                               System.err.println(" no SAX");*/
                
                          } catch (ParserConfigurationException ex) {
                               System.err.println(" ???");
                
                           } catch (IOException ex) {
                               System.err.println("+================================+");
                               System.err.println("|           *XML Error*          |");
                               System.err.println("+================================+");
                               System.err.println(ex.toString());
                          }
                     }
                
                
                private static class JFileChooserrv {
                
                     JFileChooserrv(JFrame f, boolean vverabreiten) {
                          String openfile;
                          String verror;
                          boolean validate = true;
                          final JFrame frame = f;
                          String vFilename = "Z:\\Boorberg\\parsen_vista\\daten";
                          //String vFilename = "C:\\";
                          File vFile = new File(vFilename);
                          final JFileChooser chooser = new JFileChooser(vFile);
                          JFileFilter filter = new JFileFilter();
                          filter.addType("xml");
                          filter.addType("sgml");
                          filter.addType("html");
                          filter.addType("java");
                          filter.setDescription("strukturfiles");
                          chooser.addChoosableFileFilter(filter);
                          boolean vjeas = true;
                          chooser.setMultiSelectionEnabled(vjeas);
                          int returnVal = chooser.showOpenDialog(frame);
                          if (returnVal == JFileChooser.APPROVE_OPTION) {
                               //Array  filearry[] = chooser.getSelectedFiles();
                               //if (vFile = chooser.getSelectedFiles()) {
                               //File  file[] = chooser.getSelectedFiles();
                               File  vfile[] = chooser.getSelectedFiles();
                               //String openfile = new String();
                               int vlenght = vfile.length;
                               if (vlenght>1) {
                                    int x=0;
                                    while (x< vlenght) {
                
                                              parse(vfile[x], validate);
                                              x = x +1;
                                    }
                               }
                               if (vlenght<=1) {
                                    File v2file = chooser.getSelectedFile();
                                         parse(v2file, validate);
                               }
                          } else {
                               System.out.println("You did not choose a filesystem           object.");
                          }
                     System.exit(0);
                     }
                }
                
                private static class JFileFilter extends javax.swing.filechooser.FileFilter {
                     protected String description, vnew;
                     protected ArrayList<String> exts = new ArrayList<String>();
                     protected boolean vtrue; 
                
                     public void addType(String s) {
                          exts.add(s);
                     }
                
                     /** Return true if the given file is accepted by this filter. */
                     public boolean accept(File f) {
                          // Little trick: if you don't do this, only directory names
                          // ending in one of the extentions appear in the window.
                          if (f.isDirectory()) {
                               return true;
                
                          } else if (f.isFile()) {
                               Iterator it = exts.iterator();
                               while (it.hasNext()) {
                                    if (f.getName().endsWith((String)it.next()))
                                         return true;
                               }
                          }
                
                          // A file that didn't match, or a weirdo (e.g. UNIX device file?).
                          return false;
                     }
                
                     /** Set the printable description of this filter. */
                     public void setDescription(String s) {
                          description = s;
                     }
                     /** Return the printable description of this filter. */
                     public String getDescription() {
                          return description;
                     }
                }
                
                
                private static class MyErrorHandler implements ErrorHandler {
                        // Error handler output goes here
                        private PrintStream out;
                
                        MyErrorHandler(PrintStream out) {
                            this.out = out;
                        }
                
                        /**
                         * Returns a string describing parse exception details
                         */
                        private String getParseExceptionInfo(SAXParseException spe) {
                            String systemId = spe.getSystemId();
                            if (systemId == null) {
                                systemId = "null";
                            }
                            String info = "URI=" + systemId +
                                " Line=" + spe.getLineNumber() +
                                ": " + spe.getMessage();
                            return info;
                        }
                
                        // The following methods are standard SAX ErrorHandler methods.
                        // See SAX documentation for more info.
                
                        public void warning(SAXParseException spe) throws SAXException {
                               //System.exit(0);
                            //out.println("Warning: " + getParseExceptionInfo(spe));
                        }
                
                        public void error(SAXParseException spe) throws SAXException {
                               //System.exit(0);
                            String message = "Error: " + getParseExceptionInfo(spe);
                            throw new SAXException(message);
                        }
                
                        public void fatalError(SAXParseException spe) throws SAXException {
                               //System.exit(0);
                            String message = "Fatal Error: " + getParseExceptionInfo(spe);
                            throw new SAXException(message);
                        }
                }
                     public static void main(String[] av) {
                          JFrame vframe = new JFrame("chose files to pars");
                          boolean vverabreiten = true;
                          boolean validate = true;
                          JFileChooserrv vdateienwaehlen = new JFileChooserrv(vframe, vverabreiten);
                     }
                }
                The Stack Trace i posted in the last Message. But i couldn't read it, i am not a programmer.
                • 5. Re: java.lang.NullPointerException javax.xml.parsers.DocumentBuilder.parse
                  843789
                  I have the problem, i don't know what :

                  To avoid this problem set the following feature to 'false': "http://apache.org/xml/features/dom/defer-node-expansion".

                  mens.

                  Do i have to add something in my code?

                  I also have problems to understand why the Sun Java distribution has this problem.

                  In your link they say "the issue has already been fixed in jaxp 1.4/jdk6".

                  My environment has the PATH to "C:\Program Files\Java\jdk1.6.0_13\bin", but it don't seem to be fixed.

                  But i am not know java well. My momentarily also link to "C:\Program Files\JavaFX\javafx-sdk1.1\bin;C:\Program Files\JavaFX\javafx-sdk1.1"

                  I just use this code for parsing xml documents.

                  Anyway thanks for your help, may you know what:

                  To avoid this problem set the following feature to 'false': "http://apache.org/xml/features/dom/defer-node-expansion".
                  means to chance in my code
                  • 6. Re: java.lang.NullPointerException javax.xml.parsers.DocumentBuilder.parse
                    843789
                    That was a 1.4->1.5 regress bug, which was (apparently) going to be fixed by a imminent "merge" of the latest apache xerces code.
                    I can't understand what exactly the "latest apache xerces code" means.

                    As i thought in the beginning, is there a package out there which i can/haveto add to my environment.

                    I don't understand, why the Stack Strace says something about

                    "com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl.setChunkIndex(DeferredDocumentImpl.java:1944)"

                    Because the code i wrote goes only to:

                    "at XParse.parse(XParse.java:38)"

                    The rest must be some environment problem., well i think so. But i hope someone can tell my, how to solve it.

                    Thanks so far for your help.
                    • 7. Re: java.lang.NullPointerException javax.xml.parsers.DocumentBuilder.parse
                      Tolls
                      rv11 wrote:
                      I don't understand, why the Stack Strace says something about

                      "com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl.setChunkIndex(DeferredDocumentImpl.java:1944)"

                      Because the code i wrote goes only to:

                      "at XParse.parse(XParse.java:38)"
                      Because your call to XParse.parse() results in a series of calls (as shown in the stack trace) eventually resulting in a call to DeferredDocumentImpl.setChunkIndex(). Nothing odd about that at all.
                      • 8. Re: java.lang.NullPointerException javax.xml.parsers.DocumentBuilder.parse
                        843789
                        Hi All,

                        if found now the solution:

                        As mentioned the Work Around:

                        To avoid this problem set the following feature to 'false': "http://apache.org/xml/features/dom/defer-node-expansion".
                        This will not effect the performance much."

                        Ends in this code:

                        f.setNamespaceAware(true);
                        try {
                        f.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
                        }
                        catch (ParserConfigurationException e) {
                        System.err.println("could not set parser feature");
                        }

                        I find this solution at http://xerces.apache.org/xerces2-j/features.html

                        Thank you to all that tried to help me.

                        See you Richard
                        • 9. Re: java.lang.NullPointerException javax.xml.parsers.DocumentBuilder.parse
                          843789
                          Hi Richard,

                          Is there any other solution for this work around?
                          But anyways, work around is working for me :).

                          Please let me know if anybody have solution other than this work around.

                          Thanks,
                          Sukku.
                          • 10. Re: java.lang.NullPointerException javax.xml.parsers.DocumentBuilder.parse
                            843789
                            Does any one know the cause for this issue? and Why it throws Null Pointer Exception? As i face the same kind here
                            • 11. Re: java.lang.NullPointerException javax.xml.parsers.DocumentBuilder.parse
                              PhHein
                              Stay in your thread. Locking.