在Java中-如何获取完整的堆栈跟踪

问题描述

当前我从代码运行中获得以下输出

Caused by: java.lang.NullPointerException
    at com.gargoylesoftware.htmlunit.html.HtmlScript$2.execute(HtmlScript.java:227)
    at com.gargoylesoftware.htmlunit.html.HtmlScript.onAllChildrenAddedToPage(HtmlScript.java:256)
    at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekodoMBuilder.endElement(HtmlUnitNekodoMBuilder.java:560)
    at org.apache.xerces.parsers.AbstractSAXParser.endElement(UnkNown Source)
    at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekodoMBuilder.endElement(HtmlUnitNekodoMBuilder.java:514)
    at net.sourceforge.htmlunit.cyberneko.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:1192)
    at net.sourceforge.htmlunit.cyberneko.HTMLTagBalancer.endElement(HTMLTagBalancer.java:1132)
    at net.sourceforge.htmlunit.cyberneko.filters.DefaultFilter.endElement(DefaultFilter.java:219)
    at net.sourceforge.htmlunit.cyberneko.filters.NamespaceBinder.endElement(NamespaceBinder.java:312)
    at net.sourceforge.htmlunit.cyberneko.HTMLScanner$ContentScanner.scanEndElement(HTMLScanner.java:3189)
    at net.sourceforge.htmlunit.cyberneko.HTMLScanner$ContentScanner.scan(HTMLScanner.java:2114)
    at net.sourceforge.htmlunit.cyberneko.HTMLScanner.scanDocument(HTMLScanner.java:937)
    at net.sourceforge.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:443)
    at net.sourceforge.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:394)
    at org.apache.xerces.parsers.XMLParser.parse(UnkNown Source)
    at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekodoMBuilder.parse(HtmlUnitNekodoMBuilder.java:760)
    at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekoHtmlParser.parseFragment(HtmlUnitNekoHtmlParser.java:158)
    at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekoHtmlParser.parseFragment(HtmlUnitNekoHtmlParser.java:112)
    at com.gargoylesoftware.htmlunit.javascript.host.Element.parseHtmlSnippet(Element.java:868)
    at com.gargoylesoftware.htmlunit.javascript.host.Element.setInnerHTML(Element.java:920)
    at com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement.setInnerHTML(HTMLElement.java:676)
    at sun.reflect.GeneratedMethodAccessor17.invoke(UnkNown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at net.sourceforge.htmlunit.corejs.javascript.MemberBox.invoke(MemberBox.java:188)
    ... 30 more

以上... 30 more表示我不知道导致问题的代码行。如何获取完整的堆栈跟踪。

此外,如果它似乎没有使用printstacktrace运行我的任何catch语句,但是如果我使用以下命令关闭了日志记录

java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF);

java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF);


**Have put in my full code below as suggested by the comments below . Did not put in the actual URL though**

import com.gargoylesoftware.htmlunit.html.*;
import java.io.File;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;



public class download_to_send_to_stackoverflow
{
     public static void main(String args[])
     {

      String url = "did not want to include the actual web site ";


      HtmlPage page = null;

      WebClient webClient = new WebClient();
      webClient.getoptions().setRedirectEnabled(true);  
      webClient.getoptions().setJavaScriptEnabled(true);  // tried making false - but didnt work
      webClient.getoptions().setCssEnabled(false);
      webClient.getoptions().setUseInsecureSSL(true);
          webClient.getoptions().setTimeout(30000);
      webClient.setJavaScriptTimeout(30000); //e.g. 50s
      webClient.waitForBackgroundJavaScript(15000) ;   // added 2017/1/24
      webClient.waitForBackgroundJavaScriptStartingBefore(30000) ;   // added 2017/1/24


          try
               {
               page = webClient.getPage( url );
           savePage_just_html(page );  
           }

          catch( Exception e )
               {
               System.out.println( "Exception thrown:----------------------------------------" + e.getMessage() );
               e.printstacktrace();
               }
     }




     protected static String savePage_just_html(HtmlPage page)
     {
          try
          {
            File saveFolder = new File("spool/_");
            page.save(saveFolder);   // this is the line causing the error with limit stack trace
          }



          catch ( Exception e )
          {
               System.out.println( "IOException was thrown: ---------------------------------- " + e.getMessage() );
               e.printstacktrace();
          }


          return "file.html";
     }



}



------------------------------------------------------


  page.save(saveFolder);   above is the problem line.  if I take it out I dont get the limited statcktrace errors but I also dont save the html page - which I want to do

- The question is - why does this line only print a limited stacktrace

解决方法

“由...引起”表示这是一个嵌套异常。

嵌套异常stacktrace中的“ ... 30更多”表示这30行与主要异常中的行相同。

因此,只需查看主要的堆栈跟踪以查看那些堆栈帧。


显然,您的异常堆栈跟踪是通过抑制(或删除)主要异常堆栈跟踪的方式生成的。令人费解,但这可能是由您正在使用的单元测试库之一完成的。

深入了解此问题可能涉及您要么调试生成堆栈跟踪的内容,要么编写minimal reproducible example,以便其他人可以对其进行调试。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...