英文:
Can not get the actual line in my code causing the error
问题
以下是您提供的代码段的翻译:
通过以下示例代码,我得到了大量的堆栈跟踪错误,我知道这些错误来自于 page.save(saveFolder)
这一行 - 但是在错误消息中并没有指出这一行是错误的位置。因此,当您的代码长度增加了10倍时,很难知道错误在哪里。
import com.gargoylesoftware.htmlunit.html.*;
import java.io.File;
import java.io.*;
import com.gargoylesoftware.htmlunit.WebClient;
public class download_to_send_to_stackoverflow {
public static void main(String args[]) {
System.out.println("Hello world!");
String url = "https://dont want to put actual web site down here";
HtmlPage page = null;
WebClient webClient = new WebClient();
webClient.getOptions().setRedirectEnabled(true);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setTimeout(30000);
webClient.setJavaScriptTimeout(30000); //例如:50秒
webClient.waitForBackgroundJavaScript(15000); // 添加于2017/1/24
webClient.waitForBackgroundJavaScriptStartingBefore(30000); // 添加于2017/1/24
try {
page = webClient.getPage(url);
File saveFolder = new File("spool/_");
page.save(saveFolder); // 这是导致错误的行。
} catch( Exception e ) {
}
}
}
以下是错误信息的翻译:
错误行如下:-
2020年8月19日 上午10:28:28 com.gargoylesoftware.htmlunit.javascript.DefaultJavaScriptErrorListener scriptException
严重: JavaScript执行期间出错
======= 异常开始 ========
异常类=[java.lang.RuntimeException]
com.gargoylesoftware.htmlunit.ScriptException: 调用setInnerHTML时出现异常
在 com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:929)
在 net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:619)
在 net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:537)
在 com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.callSecured(HtmlUnitContextFactory.java:354)
在 com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:858)
在 com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest.setState(XMLHttpRequest.java:213)
在 com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest.doSend(XMLHttpRequest.java:804)
在 com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest.access$000(XMLHttpRequest.java:105)
在 com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest$1.run(XMLHttpRequest.java:636)
在 net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:619)
在 net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:537)
在 com.gargoylesoftware.htmlunit.javascript.background.JavascriptXMLHttpRequestJob.run(JavascriptXMLHttpRequestJob.java:36)
在 com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManagerImpl.runSingleJob(JavaScriptJobManagerImpl.java:429)
在 com.gargoylesoftware.htmlunit.javascript.background.DefaultJavaScriptExecutor.run(DefaultJavaScriptExecutor.java:148)
在 java.lang.Thread.run(Thread.java:745)
原因是:java.lang.RuntimeException: 调用setInnerHTML时出现异常
在 net.sourceforge.htmlunit.corejs.javascript.MemberBox.invoke(MemberBox.java:214)
在 net.sourceforge.htmlunit.corejs.javascript.ScriptableObject$GetterSlot.setValue(ScriptableObject.java:323)
在 net.sourceforge.htmlunit.corejs.javascript.ScriptableObject.putImpl(ScriptableObject.java:2868)
... 以及更多错误栈信息
======= 异常结束 ========
问题是:我该如何让我的代码始终告诉我造成问题的代码行。
英文:
With the following example code I get a huge amount of stacktrace errors which I know comes from page.save(saveFolder)
line - but nowhere in the errors does it give this line as the error. So when you have code that is 10 times as long - it is hard to know where the error is.
import com.gargoylesoftware.htmlunit.html.*;
import java.io.File;
import java.io.*;
import com.gargoylesoftware.htmlunit.WebClient;
public class download_to_send_to_stackoverflow {
public static void main(String args[]) {
System.out.println("Hello world!");
String url = "https://dont want to put actual web site down here";
HtmlPage page = null;
WebClient webClient = new WebClient();
webClient.getOptions().setRedirectEnabled(true);
webClient.getOptions().setJavaScriptEnabled(true);
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 );
File saveFolder = new File("spool/_");
page.save(saveFolder); // this is the line that is causing the error.
} catch( Exception e ) {
}
}
}
There error lines are as follows : -
Aug 19, 2020 10:28:28 AM com.gargoylesoftware.htmlunit.javascript.DefaultJavaScriptErrorListener scriptException
SEVERE: Error during JavaScript execution
======= EXCEPTION START ========
Exception class=[java.lang.RuntimeException]
com.gargoylesoftware.htmlunit.ScriptException: Exception invoking setInnerHTML
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:929)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:619)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:537)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.callSecured(HtmlUnitContextFactory.java:354)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:858)
at com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest.setState(XMLHttpRequest.java:213)
at com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest.doSend(XMLHttpRequest.java:804)
at com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest.access$000(XMLHttpRequest.java:105)
at com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest$1.run(XMLHttpRequest.java:636)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:619)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:537)
at com.gargoylesoftware.htmlunit.javascript.background.JavascriptXMLHttpRequestJob.run(JavascriptXMLHttpRequestJob.java:36)
at com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManagerImpl.runSingleJob(JavaScriptJobManagerImpl.java:429)
at com.gargoylesoftware.htmlunit.javascript.background.DefaultJavaScriptExecutor.run(DefaultJavaScriptExecutor.java:148)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: Exception invoking setInnerHTML
at net.sourceforge.htmlunit.corejs.javascript.MemberBox.invoke(MemberBox.java:214)
at net.sourceforge.htmlunit.corejs.javascript.ScriptableObject$GetterSlot.setValue(ScriptableObject.java:323)
at net.sourceforge.htmlunit.corejs.javascript.ScriptableObject.putImpl(ScriptableObject.java:2868)
at net.sourceforge.htmlunit.corejs.javascript.ScriptableObject.put(ScriptableObject.java:536)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable.put(HtmlUnitScriptable.java:151)
at net.sourceforge.htmlunit.corejs.javascript.ScriptableObject.putProperty(ScriptableObject.java:2539)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.setObjectElem(ScriptRuntime.java:1721)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.setObjectElem(ScriptRuntime.java:1706)
at net.sourceforge.htmlunit.corejs.javascript.Interpreter.doSetElem(Interpreter.java:2460)
at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1488)
at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:1013)
at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:111)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:427)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:340)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3640)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$3.doRun(JavaScriptEngine.java:851)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:914)
... 14 more
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
Enclosed exception:
java.lang.RuntimeException: Exception invoking setInnerHTML
at net.sourceforge.htmlunit.corejs.javascript.MemberBox.invoke(MemberBox.java:214)
at net.sourceforge.htmlunit.corejs.javascript.ScriptableObject$GetterSlot.setValue(ScriptableObject.java:323)
at net.sourceforge.htmlunit.corejs.javascript.ScriptableObject.putImpl(ScriptableObject.java:2868)
at net.sourceforge.htmlunit.corejs.javascript.ScriptableObject.put(ScriptableObject.java:536)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable.put(HtmlUnitScriptable.java:151)
at net.sourceforge.htmlunit.corejs.javascript.ScriptableObject.putProperty(ScriptableObject.java:2539)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.setObjectElem(ScriptRuntime.java:1721)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.setObjectElem(ScriptRuntime.java:1706)
at net.sourceforge.htmlunit.corejs.javascript.Interpreter.doSetElem(Interpreter.java:2460)
at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1488)
at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:1013)
at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:111)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:427)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:340)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3640)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$3.doRun(JavaScriptEngine.java:851)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:914)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:619)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:537)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.callSecured(HtmlUnitContextFactory.java:354)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:858)
at com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest.setState(XMLHttpRequest.java:213)
at com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest.doSend(XMLHttpRequest.java:804)
at com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest.access$000(XMLHttpRequest.java:105)
at com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest$1.run(XMLHttpRequest.java:636)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:619)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:537)
at com.gargoylesoftware.htmlunit.javascript.background.JavascriptXMLHttpRequestJob.run(JavascriptXMLHttpRequestJob.java:36)
at com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManagerImpl.runSingleJob(JavaScriptJobManagerImpl.java:429)
at com.gargoylesoftware.htmlunit.javascript.background.DefaultJavaScriptExecutor.run(DefaultJavaScriptExecutor.java:148)
at java.lang.Thread.run(Thread.java:745)
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
======= EXCEPTION END ========
Question is : how can I get my code to always tell me which line of my code is causing the problem.
答案1
得分: 1
page.save(saveFolder)
不在堆栈跟踪中的原因是它启动了一个新的线程,异常是在新线程中抛出的。在新线程中,不会执行您的任何代码,因此它不会显示您的哪一行代码出错。
实际上,在这种情况下,JVM 无法知道您的哪一行代码是原因。您可能认为 page.save(saveFolder);
是原因,但更有可能是因为在调用 page.save(saveFolder);
之前忘记设置了一些参数。
您唯一能做的就是检查异常以查看出了什么问题,知道哪一行代码引起了异常不会对您有太大帮助。这就是为什么调试多线程程序很困难的原因。
英文:
The reason page.save(saveFolder)
is not on the stack trace is that it started a new thread and the exception is thrown in the new thread. In the new thread none of your code is executed so it won't show you which line of you code goes wrong.
In fact in this case JVM can't know which line of you code is the cause. you may think page.save(saveFolder);
is the cause but more likely it is because you forget to set some of the pararmeter before you call page.save(saveFolder);
.
The only thing you can do is check the exception to see what is going wrong, knowing which line of your code cause the exception won't help you much. This is why debugging multi-thread programe is difficult.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论