2

I have a piece of code, that throws NullPointerException sometimes. So far not really interesting. But the Exception occurs in a line, that does not reference any object.

    try
    {
      parser.parse(input);/*line 186*/ 
    }
    catch(Exception e)
    {
      //NPE happens in the next line?
      throw new SAXException("Error parsing document", e);/*line 190*/
    }

Here the Stacktrace

  java.lang.NullPointerException
    at com.tejoe.MyXMLParser.parse(MyXMLParser.java:190)
    at com.tejoe.MyXMLParser.parse(MyXMLParser.java:168)
    ....

It happened only twice in the last three months and the code run at least a hundred thousand times.

I already decompiled my code, to make sure the line information were correct and yes they are.

Additional Test

There seems to be something special with SAXException. I did the following test:

import org.xml.sax.SAXException;

public class Test
{
  public static void main(String[] args) throws Exception
  {
    new SAXException("Error", new NullPointerException()).printStackTrace();
  }
}

I got the following output

java.lang.NullPointerException
  at Test.main(Test.java:7)
Caused by: java.lang.NullPointerException
  ... 1 more

Solution: SAXException overrides toString method, to return the Name of the cause Exception.

Now I only wonder, that I did not get the caused by output in the production environment (AIX JAVA)

4

3 回答 3

1

自从您发布最后一个版本以来,您的代码必须已更改。这会弄乱行号,因为它们是基于编译时的代码。

自发布以来,您似乎已从该文件中删除了一些代码,这意味着行号比应有的大。

于 2013-05-15T11:53:20.027 回答
1

我编写了以下代码并生成了空指针异常。它出现在第 45 行,如下所示,

Exception in thread "main" java.lang.NullPointerException
    at inheritance.parent.Child.main(Child.java:45)

import org.xml.sax.SAXException;

public class Child  {

public static void main(String[] args) throws SAXException {

        try{
        Child c1 = null;
        c1.i=0;

        } catch(Exception e){
            throw new SAXException("Error", e); //Line : 45
        }
    }
}

您的代码的错误是正确的。你需要找到你得到空的对象。

于 2013-05-15T12:09:48.887 回答
0

更换您的线路:

throw new SAXException("Error parsing document", e);

e.printStackTrace();

你会找到正确的错误行。

于 2013-05-15T12:14:17.747 回答