【问题标题】:In Java - how do I get a full stack trace在 Java 中 - 我如何获得完整的堆栈跟踪
【发布时间】:2020-12-03 23:28:08
【问题描述】:

目前我从代码运行中得到以下输出

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

【问题讨论】:

    标签: java java.util.logging printstacktrace


    【解决方案1】:

    “Caused by”表示这是一个嵌套异常。

    嵌套异常堆栈跟踪中的“... 30 more”表示这 30 行与主要异常中的相同。

    因此,只需查看主堆栈跟踪即可查看这些堆栈帧。


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

    深入了解这可能需要您调试生成堆栈跟踪的任何内容,或者编写 minimal reproducible example 以便其他人可以调试它。

    【讨论】:

    • 非常感谢您的回答。
    • 但是,堆栈跟踪不是来自我的任何捕获和 e.printStackTrace。它似乎只有在我包含 2 行时才会出现:-
    • 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);
    • 但是这两行似乎只打印了堆栈跟踪的一半——我很想添加代码以便打印完整的堆栈跟踪。
    • 已编辑原始问题以添加代码 - 正如上述答案所建议的那样
    猜你喜欢
    • 2022-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 2013-11-16
    • 2011-11-11
    相关资源
    最近更新 更多