2017-06-27 113 views
0

我正在試驗JavaFX標籤和組,通過鼠標拖動在屏幕上移動它們。新節點將從某些線程添加到動畫組。 但是,有時我會突然發現以下異常 - 我假設某些節點重疊時。但我不知道問題是什麼......因爲我的代碼沒有涉及。有誰知道這個例外和根本原因(使用JDK 1.8.0.-112)? 感謝JavaFX拋出ArrayIndexOutOfBoundsException

'JavaFX Application Thread' lambda$main$68 - Thread[JavaFX Application Thread,5,main] threw an uncaught exception: java.lang.ArrayIndexOutOfBoundsException: -1 
    at java.util.ArrayList.elementData(ArrayList.java:418) ~[?:1.8.0_112] 
    at java.util.ArrayList.get(ArrayList.java:431) ~[?:1.8.0_112] 
    at com.sun.javafx.collections.ObservableListWrapper.get(ObservableListWrapper.java:89) ~[jfxrt.jar:?] 
    at com.sun.javafx.collections.VetoableListDecorator.get(VetoableListDecorator.java:306) ~[jfxrt.jar:?] 
    at javafx.scene.Parent.updateCachedBounds(Parent.java:1591) ~[jfxrt.jar:?] 
    at javafx.scene.Parent.recomputeBounds(Parent.java:1535) ~[jfxrt.jar:?] 
    at javafx.scene.Parent.impl_computeGeomBounds(Parent.java:1388) ~[jfxrt.jar:?] 
    at javafx.scene.layout.Region.impl_computeGeomBounds(Region.java:3078) ~[jfxrt.jar:?] 
    at javafx.scene.Node.updateGeomBounds(Node.java:3579) ~[jfxrt.jar:?] 
    at javafx.scene.Node.getGeomBounds(Node.java:3532) ~[jfxrt.jar:?] 
    at javafx.scene.Node.getLocalBounds(Node.java:3480) ~[jfxrt.jar:?] 
    at javafx.scene.Node.updateTxBounds(Node.java:3643) ~[jfxrt.jar:?] 
    at javafx.scene.Node.getTransformedBounds(Node.java:3426) ~[jfxrt.jar:?] 
    at javafx.scene.Parent.getChildTransformedBounds(Parent.java:1732) ~[jfxrt.jar:?] 
    at javafx.scene.Parent.updateCachedBounds(Parent.java:1596) ~[jfxrt.jar:?] 
    at javafx.scene.Parent.recomputeBounds(Parent.java:1535) ~[jfxrt.jar:?] 
    at javafx.scene.Parent.impl_computeGeomBounds(Parent.java:1388) ~[jfxrt.jar:?] 
    at javafx.scene.Node.updateGeomBounds(Node.java:3579) ~[jfxrt.jar:?] 
    at javafx.scene.Node.getGeomBounds(Node.java:3532) ~[jfxrt.jar:?] 
    at javafx.scene.Node.getLocalBounds(Node.java:3480) ~[jfxrt.jar:?] 
    at javafx.scene.Node.impl_intersectsBounds(Node.java:5015) ~[jfxrt.jar:?] 
    at javafx.scene.Parent.impl_pickNodeLocal(Parent.java:705) ~[jfxrt.jar:?] 
    at javafx.scene.Node.impl_pickNode(Node.java:4914) ~[jfxrt.jar:?] 
    at javafx.scene.Scene$MouseHandler.pickNode(Scene.java:3899) ~[jfxrt.jar:?] 
    at javafx.scene.Scene$MouseHandler.access$1600(Scene.java:3485) ~[jfxrt.jar:?] 
    at javafx.scene.Scene.pick(Scene.java:1942) ~[jfxrt.jar:?] 
    at javafx.scene.Scene.access$6700(Scene.java:159) ~[jfxrt.jar:?] 
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3799) ~[jfxrt.jar:?] 
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485) ~[jfxrt.jar:?] 
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762) ~[jfxrt.jar:?] 
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494) ~[jfxrt.jar:?] 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381) ~[jfxrt.jar:?] 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295) ~[jfxrt.jar:?] 
    at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_112] 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417) ~[jfxrt.jar:?] 
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) ~[jfxrt.jar:?] 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416) ~[jfxrt.jar:?] 
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555) ~[jfxrt.jar:?] 
    at com.sun.glass.ui.View.notifyMouse(View.java:937) ~[jfxrt.jar:?] 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) ~[jfxrt.jar:?] 
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) ~[jfxrt.jar:?] 
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_112] 

我認爲,它是與添加方法的標籤(= BorderPane)。我介紹它是爲了縮短Java FX應用程序線程處理時間。這有什麼問題嗎?

private Queue<BacklogGroupAction> todoList = new ConcurrentLinkedQueue<>(); 

class BacklogGroupAction { 
    boolean add; 
    boolean visibleState; 
    BorderPane dv; 

    public BacklogGroupAction(boolean add, boolean visibleState, BorderPane dv) { 
     this.add = add; 
     this.visibleState = visibleState; 
     this.dv = dv; 
    } 

} 

private synchronized void processBacklogGroupList() { 
    if (Platform.isFxApplicationThread()) { 
     while (!todoList.isEmpty()) { 
      BacklogGroupAction p = todoList.poll(); 
      if (p.add) { 
       p.dv.setVisible(p.visibleState); 
       backlogGroup.getChildren() 
       .add(p.dv); 
      } else 
       backlogGroup.getChildren() 
       .remove(p.dv); 
     } 
     backlogGroup.applyCss(); 
     backlogGroup.layout(); 
    } else 
     logger.error("This is not FX Application Thread"); 
} 
+0

,因爲我的代碼是不參與的手段,你可以發佈相同的代碼,而無需代碼捲入空指針不會被拋出 –

+0

這種試圖改變一個'ObservableList'時通常發生綁定到JavaFX UI線程以外的線程的節點,或者在更改過程中操作列表時,通常在「Platform.runLater」中封裝有問題的調用可以解決這個問題。 (https://stackoverflow.com/questions/27769583/calling-clear-on-observablelist-causes-indexoutofboundsexception) – Itai

+0

@RahulSingh:是的,當然,我的代碼肯定會導致問題;)但是,它並沒有涉及在例外情況 – Defarine

回答

1

感謝您的全力幫助。 上面的代碼「processBacklogGroupList」實際上是正確的,工作...(所以爲什麼-1 ..?)

問題是在一個完全不同的地方,一個動畫定義不運行在JFX應用程序線程 - 但稍後發生異常。

所以感謝@sillyfly指着我真正的問題

這種試圖改變從比JavaFX的UI線程以外的線程綁定到 節點的ObservableList時,要不時 通常發生在更改期間操縱列表。在這兩種情況下,通常將 包含在Platform.runLater中的違規呼叫解決了這個問題。見例如 這個相關的問題。 - sillyfly 6月27日在10:03"

+0

請參閱https://bugs.openjdk.java.net/browse/JDK-8163078 如果Oracle會添加一些代碼來不拋出Nullpointer異常,出了代碼不在JFX主題上的事實。 –

+0

https://stackoverflow.com/a/45679685/1497139有更多鏈接 –

相關問題