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

问题描述 投票:0回答:2

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

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
2个回答
2
投票

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

嵌套异常堆栈跟踪中的“... 30 more”意味着这 30 行与主异常中的相同。

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


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

深入了解这个问题可能需要您调试生成堆栈跟踪的任何内容,或者编写一个最小的可重现示例以便其他人可以调试它。


0
投票

如今,Java 是 IT 行业中最重要的语言。由于新技术不断变化和发展,“全栈 Web 开发人员”多年来一直是最受欢迎的职位。每个企业都需要一致的用户体验,当一个人负责客户端和服务器端编程时,这一点很容易提供。两个人还意味着更高的收入、更多的资源和更多的想法。如果当前的就业市场趋势持续下去,在可预见的未来,全栈 Java 开发人员将成为就业市场的需求。 雇主已经开始重视具有多元化才能的个人。

Java 全栈开发人员

在世界各地都有很大的需求,就业市场预计将为他们创造或重新分配更多的就业机会。这种程度的工作机会表明了一条充满希望的职业道路。你获得的人才越有价值,你在找工作时的议价能力就越大。您可以在各种情况下充分发挥您的才能并展示您的故障排除专业知识。您将把自己打造为团队的核心人物,让您能够发展自己的职业生涯。它还可以帮助您在竞争中保持领先地位。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.