StringIndexOutOfBoundsException JavaFx WebEngine

时间:2018-01-02 17:08:42

标签: java javafx webengine stringindexoutofbounds

以下代码生成StringIndexOutofBoundsException,我不确定原因:

public class Browser extends Application {

@Override
public void start(Stage primaryStage) {

    WebPage currentPage = new WebPage();
    WebView webView = currentPage.getWebView();
    WebEngine webEngine = currentPage.createWebEngine(primaryStage);
    webEngine.load("http://algonquincollege.com");

    BorderPane root = new BorderPane();
    root.setCenter(webView);

    Scene scene = new Scene(root, 800, 500);
    primaryStage.setScene(scene);
    primaryStage.show();
}

public static void main(String[] args) {
    Application.launch(args);
}

我在Eclipse中遇到以下错误:

java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.substring(String.java:1963)
at com.sun.javafx.webkit.prism.WCGraphicsPrismContext.drawString(WCGraphicsPrismContext.java:960)
at com.sun.webkit.graphics.GraphicsDecoder.decode(GraphicsDecoder.java:290)
at com.sun.webkit.graphics.WCRenderQueue.decode(WCRenderQueue.java:91)
at com.sun.webkit.graphics.WCRenderQueue.decode(WCRenderQueue.java:102)
at com.sun.webkit.graphics.WCImage.flushRQ(WCImage.java:52)
at com.sun.javafx.webkit.prism.RTImage.lambda$getPixelBuffer$77(RTImage.java:163)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
at com.sun.javafx.tk.RenderJob.run(RenderJob.java:58)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:125)
at java.lang.Thread.run(Thread.java:745)

java.lang.StringIndexOutOfBoundsException: String index out of range: 7
at java.lang.String.substring(String.java:1963)
at com.sun.javafx.webkit.prism.WCGraphicsPrismContext.drawString(WCGraphicsPrismContext.java:960)
at com.sun.webkit.graphics.GraphicsDecoder.decode(GraphicsDecoder.java:290)
at com.sun.webkit.graphics.WCRenderQueue.decode(WCRenderQueue.java:91)
at com.sun.webkit.graphics.WCRenderQueue.decode(WCRenderQueue.java:102)
at com.sun.webkit.graphics.WCImage.flushRQ(WCImage.java:52)
at com.sun.javafx.webkit.prism.RTImage.lambda$getPixelBuffer$77(RTImage.java:163)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
at com.sun.javafx.tk.RenderJob.run(RenderJob.java:58)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:125)
at java.lang.Thread.run(Thread.java:745)

java.lang.StringIndexOutOfBoundsException: String index out of range: 6
at java.lang.String.substring(String.java:1963)
at com.sun.javafx.webkit.prism.WCGraphicsPrismContext.drawString(WCGraphicsPrismContext.java:960)
at com.sun.webkit.graphics.GraphicsDecoder.decode(GraphicsDecoder.java:290)
at com.sun.webkit.graphics.WCRenderQueue.decode(WCRenderQueue.java:91)
at com.sun.webkit.graphics.WCRenderQueue.decode(WCRenderQueue.java:102)
at com.sun.webkit.graphics.WCImage.flushRQ(WCImage.java:52)
at com.sun.javafx.webkit.prism.RTImage.lambda$getPixelBuffer$77(RTImage.java:163)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
at com.sun.javafx.tk.RenderJob.run(RenderJob.java:58)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:125)
at java.lang.Thread.run(Thread.java:745)

如何防止这种情况或处理此异常?我认为它可能与URL有关,因为其他网站加载正常而没有错误。

我尝试在start方法中添加Thread.setDefaultUncaughtExceptionHandler,但它不起作用。

1 个答案:

答案 0 :(得分:1)

我不确定你的错误是什么我也无法运行你的代码但是这对我有用

@Override
public void start(Stage primaryStage) {

    WebView webView = new WebView();
    WebEngine webEngine = webView.getEngine();
    webEngine.load("http://algonquincollege.com");

    BorderPane root = new BorderPane();
    root.setCenter(webView);

    Scene scene = new Scene(root, 800, 500);
    primaryStage.setScene(scene);
    primaryStage.show();
}