NPE拖放

时间:2013-08-04 13:53:40

标签: java javafx-2 javafx parseint javafx-8

我在Windows JVM 7_25中测试了这段代码,效果很好。

import java.util.Random;  
import javafx.application.Application;  
import javafx.collections.ObservableList;  
import javafx.event.EventHandler;  
import javafx.scene.Node;  
import javafx.scene.Scene;  
import javafx.scene.input.ClipboardContent;  
import javafx.scene.input.DragEvent;  
import javafx.scene.input.Dragboard;  
import javafx.scene.input.MouseEvent;  
import javafx.scene.input.TransferMode;  
import javafx.scene.layout.BorderPane;  
import javafx.scene.layout.FlowPane;  
import javafx.scene.layout.Pane;  
import javafx.stage.Stage;  
public class DraggableFlowPane extends Application {  
    @Override  
    public void start(Stage primaryStage) {  
        final FlowPane root = new FlowPane();  
        final Random rng = new Random();  
        final int NUM_NODES = 120;  
        for (int i = 0; i < NUM_NODES; i++) {  
            int red = rng.nextInt(256);  
            int green = rng.nextInt(256);  
            int blue = rng.nextInt(256);  
            Node node = createNode();  
            node.setStyle(String.format("-fx-background-color: rgb(%d, %d, %d);", red, green, blue));  
            root.getChildren().add(node);  
        }  
        primaryStage.setScene(new Scene(root, 600, 500));  
        primaryStage.show();  
    }  
    private Node createNode() {  
        final BorderPane bp = new BorderPane();  
        bp.setOnDragDetected(new EventHandler<MouseEvent>() {  
            @Override  
            public void handle(MouseEvent event) {  
                Dragboard db = bp.startDragAndDrop(TransferMode.MOVE);  
                ClipboardContent clipboard = new ClipboardContent();  
                final int nodeIndex = bp.getParent().getChildrenUnmodifiable()  
                        .indexOf(bp);  
                clipboard.putString(Integer.toString(nodeIndex));  
                db.setContent(clipboard);  
                event.consume();  
            }  
        });  
        bp.setOnDragOver(new EventHandler<DragEvent>() {  
            @Override  
            public void handle(DragEvent event) {  
                boolean accept = true;  
                final Dragboard dragboard = event.getDragboard();  
                if (dragboard.hasString()) {  
                    int incomingIndex = Integer.parseInt(dragboard.getString());  
                    int myIndex = bp.getParent().getChildrenUnmodifiable()  
                            .indexOf(bp);  
                    if (incomingIndex == myIndex) {  
                        accept = false;  
                    }  
                } else {  
                    accept = false;  
                }  
                if (accept) {  
                    event.acceptTransferModes(TransferMode.MOVE);  
                }  
            }  
        });  
        bp.setOnDragDropped(new EventHandler<DragEvent>() {  
            @Override  
            public void handle(DragEvent event) {  
                boolean success = false;  
                final Dragboard dragboard = event.getDragboard();  
                if (dragboard.hasString()) {  
                    int incomingIndex = Integer.parseInt(dragboard.getString());  
                    final Pane parent = (Pane) bp.getParent();  
                    final ObservableList<Node> children = parent.getChildren();  
                    int myIndex = children.indexOf(bp);  
                    final int laterIndex = Math.max(incomingIndex, myIndex);  
                    Node removedLater = children.remove(laterIndex);  
                    final int earlierIndex = Math.min(incomingIndex, myIndex);  
                    Node removedEarlier = children.remove(earlierIndex);  
                    children.add(earlierIndex, removedLater);  
                    children.add(laterIndex, removedEarlier);  
                    success = true;  
                }  
                event.setDropCompleted(success);  
            }  
        });  
        bp.setMinSize(50, 50);  
        return bp;  
    }  
    public static void main(String[] args) {  
        launch(args);  
    }  
}

当我使用JVM 7_25在Linux上测试相同的代码时,我得到了这个错误堆栈:

    Executing com.javafx.main.Main from /home/rcbandit/Desktop/test/DX-57DC/dist/run652386450/DX-57DC.jar using platform /opt/jdk1.7.0_25/bin/java  
    Exception in thread "JavaFX Application Thread" java.lang.NumberFormatException: null  
        at java.lang.Integer.parseInt(Integer.java:454)  
        at java.lang.Integer.parseInt(Integer.java:527)  
        at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:58)  
        at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:52)  
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)  
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)  
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)  
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)  
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)  
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)  
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)  
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)  
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)  
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)  
        at javafx.event.Event.fireEvent(Event.java:171)  
        at javafx.scene.Scene$DnDGesture.fireEvent(Scene.java:2631)  
        at javafx.scene.Scene$DnDGesture.processTargetEnterOver(Scene.java:2780)  
        at javafx.scene.Scene$DnDGesture.access$6100(Scene.java:2607)  
        at javafx.scene.Scene$DropTargetListener.dragEnter(Scene.java:2523)  
        at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$1.run(GlassSceneDnDEventHandler.java:63)  
        at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$1.run(GlassSceneDnDEventHandler.java:54)  
        at java.security.AccessController.doPrivileged(Native Method)  
        at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler.handleDragEnter(GlassSceneDnDEventHandler.java:54)  
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleDragEnter(GlassViewEventHandler.java:395)  
        at com.sun.glass.ui.View.handleDragEnter(View.java:692)  
        at com.sun.glass.ui.View.notifyDragEnter(View.java:1005)  
        at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystemImpl(Native Method)  
        at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystem(GtkDnDClipboard.java:24)  
        at com.sun.glass.ui.SystemClipboard.flush(SystemClipboard.java:28)  
        at com.sun.glass.ui.ClipboardAssistance.flush(ClipboardAssistance.java:34)  
        at com.sun.javafx.tk.quantum.QuantumClipboard.flush(QuantumClipboard.java:197)  
        at com.sun.javafx.tk.quantum.QuantumToolkit.startDrag(QuantumToolkit.java:1195)  
        at javafx.scene.Scene$DnDGesture.dragDetectedProcessed(Scene.java:2652)  
        at javafx.scene.Scene$DnDGesture.process(Scene.java:2713)  
        at javafx.scene.Scene$DnDGesture.access$8700(Scene.java:2607)  
        at javafx.scene.Scene$MouseHandler.process(Scene.java:3344)  
        at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)  
        at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)  
        at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)  
        at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)  
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)  
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)  
        at java.security.AccessController.doPrivileged(Native Method)  
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)  
        at com.sun.glass.ui.View.handleMouseEvent(View.java:528)  
        at com.sun.glass.ui.View.notifyMouse(View.java:922)  
        at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)  
        at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)  
        at java.lang.Thread.run(Thread.java:724)  
    Exception in thread "JavaFX Application Thread" java.lang.NumberFormatException: null  
        at java.lang.Integer.parseInt(Integer.java:454)  
        at java.lang.Integer.parseInt(Integer.java:527)  
        at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:58)  
        at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:52)  
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)  
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)  
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)  
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)  
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)  
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)  
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)  
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)  
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)  
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)  
        at javafx.event.Event.fireEvent(Event.java:171)  
        at javafx.scene.Scene$DnDGesture.fireEvent(Scene.java:2631)  
        at javafx.scene.Scene$DnDGesture.processTargetEnterOver(Scene.java:2780)  
        at javafx.scene.Scene$DnDGesture.access$6100(Scene.java:2607)  
        at javafx.scene.Scene$DropTargetListener.dragOver(Scene.java:2534)  
        at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$4.run(GlassSceneDnDEventHandler.java:126)  
        at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$4.run(GlassSceneDnDEventHandler.java:117)  
        at java.security.AccessController.doPrivileged(Native Method)  
        at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler.handleDragOver(GlassSceneDnDEventHandler.java:117)  
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleDragOver(GlassViewEventHandler.java:422)  
        at com.sun.glass.ui.View.handleDragOver(View.java:701)  
        at com.sun.glass.ui.View.notifyDragOver(View.java:1010)  
        at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystemImpl(Native Method)  
        at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystem(GtkDnDClipboard.java:24)  
        at com.sun.glass.ui.SystemClipboard.flush(SystemClipboard.java:28)  
        at com.sun.glass.ui.ClipboardAssistance.flush(ClipboardAssistance.java:34)  
        at com.sun.javafx.tk.quantum.QuantumClipboard.flush(QuantumClipboard.java:197)  
        at com.sun.javafx.tk.quantum.QuantumToolkit.startDrag(QuantumToolkit.java:1195)  
        at javafx.scene.Scene$DnDGesture.dragDetectedProcessed(Scene.java:2652)  
        at javafx.scene.Scene$DnDGesture.process(Scene.java:2713)  
        at javafx.scene.Scene$DnDGesture.access$8700(Scene.java:2607)  
        at javafx.scene.Scene$MouseHandler.process(Scene.java:3344)  
        at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)  
        at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)  
        at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)  
        at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)  
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)  
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)  
        at java.security.AccessController.doPrivileged(Native Method)  
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)  
        at com.sun.glass.ui.View.handleMouseEvent(View.java:528)  
        at com.sun.glass.ui.View.notifyMouse(View.java:922)  
        at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)  
        at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)  
        at java.lang.Thread.run(Thread.java:724)  
    Exception in thread "JavaFX Application Thread" java.lang.NumberFormatException: null  
        at java.lang.Integer.parseInt(Integer.java:454)  
        at java.lang.Integer.parseInt(Integer.java:527)  
        at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:58)  
        at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:52)  
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)  
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)  
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)  
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)  
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)  
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)  
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)  
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)  
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)  
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)  
        at javafx.event.Event.fireEvent(Event.java:171)  
        at javafx.scene.Scene$DnDGesture.fireEvent(Scene.java:2631)  
        at javafx.scene.Scene$DnDGesture.processTargetEnterOver(Scene.java:2780)  
        at javafx.scene.Scene$DnDGesture.access$6100(Scene.java:2607)  
        at javafx.scene.Scene$DropTargetListener.dragOver(Scene.java:2534)  
        at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$4.run(GlassSceneDnDEventHandler.java:126)  
        at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$4.run(GlassSceneDnDEventHandler.java:117)  
        at java.security.AccessController.doPrivileged(Native Method)  
        at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler.handleDragOver(GlassSceneDnDEventHandler.java:117)  
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleDragOver(GlassViewEventHandler.java:422)  
        at com.sun.glass.ui.View.handleDragOver(View.java:701)  
        at com.sun.glass.ui.View.notifyDragOver(View.java:1010)  
        at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystemImpl(Native Method)  
        at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystem(GtkDnDClipboard.java:24)  
        at com.sun.glass.ui.SystemClipboard.flush(SystemClipboard.java:28)  
        at com.sun.glass.ui.ClipboardAssistance.flush(ClipboardAssistance.java:34)  
        at com.sun.javafx.tk.quantum.QuantumClipboard.flush(QuantumClipboard.java:197)  
        at com.sun.javafx.tk.quantum.QuantumToolkit.startDrag(QuantumToolkit.java:1195)  
        at javafx.scene.Scene$DnDGesture.dragDetectedProcessed(Scene.java:2652)  
        at javafx.scene.Scene$DnDGesture.process(Scene.java:2713)  
        at javafx.scene.Scene$DnDGesture.access$8700(Scene.java:2607)  
        at javafx.scene.Scene$MouseHandler.process(Scene.java:3344)  
        at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)  
        at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)  
        at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)  
        at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)  
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)  
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)  
        at java.security.AccessController.doPrivileged(Native Method)  
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)  
        at com.sun.glass.ui.View.handleMouseEvent(View.java:528)  
        at com.sun.glass.ui.View.notifyMouse(View.java:922)  
        at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)  
        at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)  
        at java.lang.Thread.run(Thread.java:724)  
    Exception in thread "JavaFX Application Thread" java.lang.NumberFormatException: null  
        at java.lang.Integer.parseInt(Integer.java:454)  
        at java.lang.Integer.parseInt(Integer.java:527)  
        at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:58)  
        at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:52)  
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)  
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)  
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)  
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)  
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)  
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)  
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)  
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)  
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)  
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)  
        at javafx.event.Event.fireEvent(Event.java:171)  
        at javafx.scene.Scene$DnDGesture.fireEvent(Scene.java:2631)  
        at javafx.scene.Scene$DnDGesture.processTargetEnterOver(Scene.java:2780)  
        at javafx.scene.Scene$DnDGesture.access$6100(Scene.java:2607)  
        at javafx.scene.Scene$DropTargetListener.dragOver(Scene.java:2534)  
        at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$4.run(GlassSceneDnDEventHandler.java:126)  
        at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$4.run(GlassSceneDnDEventHandler.java:117)  
        at java.security.AccessController.doPrivileged(Native Method)  
        at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler.handleDragOver(GlassSceneDnDEventHandler.java:117)  
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleDragOver(GlassViewEventHandler.java:422)  
        at com.sun.glass.ui.View.handleDragOver(View.java:701)  
        at com.sun.glass.ui.View.notifyDragOver(View.java:1010)  
        at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystemImpl(Native Method)  
        at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystem(GtkDnDClipboard.java:24)  
        at com.sun.glass.ui.SystemClipboard.flush(SystemClipboard.java:28)  
        at com.sun.glass.ui.ClipboardAssistance.flush(ClipboardAssistance.java:34)  
        at com.sun.javafx.tk.quantum.QuantumClipboard.flush(QuantumClipboard.java:197)  
        at com.sun.javafx.tk.quantum.QuantumToolkit.startDrag(QuantumToolkit.java:1195)  
        at javafx.scene.Scene$DnDGesture.dragDetectedProcessed(Scene.java:2652)  
        at javafx.scene.Scene$DnDGesture.process(Scene.java:2713)  
        at javafx.scene.Scene$DnDGesture.access$8700(Scene.java:2607)  
        at javafx.scene.Scene$MouseHandler.process(Scene.java:3344)  
        at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)  
        at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)  
        at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)  
        at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)  
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)  
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)  
        at java.security.AccessController.doPrivileged(Native Method)  
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)  
        at com.sun.glass.ui.View.handleMouseEvent(View.java:528)  
        at com.sun.glass.ui.View.notifyMouse(View.java:922)  
        at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)  
        at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)  
        at java.lang.Thread.run(Thread.java:724)  
    Exception in thread "JavaFX Application Thread" java.lang.NumberFormatException: null  
        at java.lang.Integer.parseInt(Integer.java:454)  
        at java.lang.Integer.parseInt(Integer.java:527)  
        at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:58)  
        at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:52)  
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)  
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)  
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)  
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)  
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)  
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)  
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)  
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)  
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)  
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)  
        at javafx.event.Event.fireEvent(Event.java:171)  
        at javafx.scene.Scene$DnDGesture.fireEvent(Scene.java:2631)  
        at javafx.scene.Scene$DnDGesture.processTargetEnterOver(Scene.java:2780)  
        at javafx.scene.Scene$DnDGesture.access$6100(Scene.java:2607)  
        at javafx.scene.Scene$DropTargetListener.dragOver(Scene.java:2534)  
        at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$4.run(GlassSceneDnDEventHandler.java:126)  
        at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$4.run(GlassSceneDnDEventHandler.java:117)  
        at java.security.AccessController.doPrivileged(Native Method)  
        at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler.handleDragOver(GlassSceneDnDEventHandler.java:117)  
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleDragOver(GlassViewEventHandler.java:422)  
        at com.sun.glass.ui.View.handleDragOver(View.java:701)  
        at com.sun.glass.ui.View.notifyDragOver(View.java:1010)  
        at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystemImpl(Native Method)  
        at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystem(GtkDnDClipboard.java:24)  
        at com.sun.glass.ui.SystemClipboard.flush(SystemClipboard.java:28)  
        at com.sun.glass.ui.ClipboardAssistance.flush(ClipboardAssistance.java:34)  
        at com.sun.javafx.tk.quantum.QuantumClipboard.flush(QuantumClipboard.java:197)  
        at com.sun.javafx.tk.quantum.QuantumToolkit.startDrag(QuantumToolkit.java:1195)  
        at javafx.scene.Scene$DnDGesture.dragDetectedProcessed(Scene.java:2652)  
        at javafx.scene.Scene$DnDGesture.process(Scene.java:2713)  
        at javafx.scene.Scene$DnDGesture.access$8700(Scene.java:2607)  
        at javafx.scene.Scene$MouseHandler.process(Scene.java:3344)  
        at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)  
        at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)  
        at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)  
        at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)  
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)  
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)  
        at java.security.AccessController.doPrivileged(Native Method)  
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)  
        at com.sun.glass.ui.View.handleMouseEvent(View.java:528)  
        at com.sun.glass.ui.View.notifyMouse(View.java:922)  
        at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)  
        at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)  
        at java.lang.Thread.run(Thread.java:724)  
    Exception in thread "JavaFX Application Thread" java.lang.NumberFormatException: null  
        at java.lang.Integer.parseInt(Integer.java:454)  
        at java.lang.Integer.parseInt(Integer.java:527)  
        at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:58)  
        at com.dx57dc.main.DX57DC$2.handle(DX57DC.java:52)  
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)  
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)  
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)  
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)  
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)  
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)  
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)  
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)  
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)  
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)  
        at javafx.event.Event.fireEvent(Event.java:171)  
        at javafx.scene.Scene$DnDGesture.fireEvent(Scene.java:2631)  
        at javafx.scene.Scene$DnDGesture.processTargetEnterOver(Scene.java:2780)  
        at javafx.scene.Scene$DnDGesture.access$6100(Scene.java:2607)  
        at javafx.scene.Scene$DropTargetListener.dragOver(Scene.java:2534)  
        at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$4.run(GlassSceneDnDEventHandler.java:126)  
        at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler$4.run(GlassSceneDnDEventHandler.java:117)  
        at java.security.AccessController.doPrivileged(Native Method)  
        at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler.handleDragOver(GlassSceneDnDEventHandler.java:117)  
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleDragOver(GlassViewEventHandler.java:422)  
        at com.sun.glass.ui.View.handleDragOver(View.java:701)  
        at com.sun.glass.ui.View.notifyDragOver(View.java:1010)  
        at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystemImpl(Native Method)  
        at com.sun.glass.ui.gtk.GtkDnDClipboard.pushToSystem(GtkDnDClipboard.java:24)  
        at com.sun.glass.ui.SystemClipboard.flush(SystemClipboard.java:28)  

................................................................

(及更多)

int incomingIndex = Integer.parseInt(dragboard.getString());的内容为空

您能告诉我如何解决此问题或重写代码吗?

3 个答案:

答案 0 :(得分:5)

您似乎将null作为int传递,null无法转换为Integer(或int)

int incomingIndex = Integer.parseInt(dragboard.getString());  

检查dragboard.getString()的输出。

答案 1 :(得分:2)

彼得,

我正在浏览此链接,以了解有关拖放功能的更多信息。 - http://docs.oracle.com/javafx/2/drag_drop/jfxpub-drag_drop.htm。 从他们给出的例子来看,你似乎完成了预期的事情。

来到您的实际代码: 我会试着理解我是否在setOnDragDetected({})中将一个null放入ClipboardContent对象; ClipboardContent类扩展了HashMap - http://docs.oracle.com/javafx/2/api/javafx/scene/input/ClipboardContent.html

如果您尝试获取的nodeIndex不是DataFormat.PLAIN_TEXT,则clipboard.putString()实际上可能不会将索引放在hashmap中。请参阅语句 - “将此值设置为null可以有效地从剪贴板中清除它”。在javadoc中确认是否发生了这种情况。

如果'put'操作成功,putString方法将布尔值返回True。

我会打印这两件事('putString()'的输出和'nodeIndex'的值)来调查出了什么问题。

-Regards, 拉维。

答案 2 :(得分:1)

检查dragboard.hasString()。如果FALSE null ,它肯定会返回dragboard.getString()。因此,您永远不会到达int incomingIndex = Integer.parseInt(dragboard.getString());,因为您不会通过if (dragboard.hasString())

如果您无法找到问题,请粘贴Dragboard班级实施。

编辑:

这将更加强大:

private Node createNode() {  
        final BorderPane bp = new BorderPane();  
        bp.setOnDragDetected(new EventHandler<MouseEvent>() {  
            @Override  
            public void handle(MouseEvent event) {  
                Dragboard db = bp.startDragAndDrop(TransferMode.MOVE);  
                ClipboardContent clipboard = new ClipboardContent();  
                final int nodeIndex = bp.getParent().getChildrenUnmodifiable()  
                        .indexOf(bp);  
                clipboard.putString(Integer.toString(nodeIndex));  
                db.setContent(clipboard);  
                event.consume();  
            }  
        });  
        bp.setOnDragOver(new EventHandler<DragEvent>() {  
            @Override  
            public void handle(DragEvent event) {  
                boolean accept = true;  
                final Dragboard dragboard = event.getDragboard();  
                if (dragboard.hasString()) {  
                  try {
                    int incomingIndex = Integer.parseInt(dragboard.getString());  
                    int myIndex = bp.getParent().getChildrenUnmodifiable()  
                            .indexOf(bp);  
                    if (incomingIndex == myIndex) {  
                        accept = false;  
                    }  
                  } catch(java.lang.NumberFormatException e) {
                    //handle null or not number string in clipboard
                    accept = false;
                  }
                } else {  
                    accept = false;  
                }  
                if (accept) {  
                    event.acceptTransferModes(TransferMode.MOVE);  
                }  
            }  
        });  
        bp.setOnDragDropped(new EventHandler<DragEvent>() {  
            @Override  
            public void handle(DragEvent event) {  
                boolean success = false;  
                final Dragboard dragboard = event.getDragboard();  
                if (dragboard.hasString()) {  
                  try {
                    int incomingIndex = Integer.parseInt(dragboard.getString());  
                    final Pane parent = (Pane) bp.getParent();  
                    final ObservableList<Node> children = parent.getChildren();  
                    int myIndex = children.indexOf(bp);  
                    final int laterIndex = Math.max(incomingIndex, myIndex);  
                    Node removedLater = children.remove(laterIndex);  
                    final int earlierIndex = Math.min(incomingIndex, myIndex);  
                    Node removedEarlier = children.remove(earlierIndex);  
                    children.add(earlierIndex, removedLater);  
                    children.add(laterIndex, removedEarlier);  
                    success = true;  
                  } catch(java.lang.NumberFormatException e) {
                    //handle null or not number string in clipboard
                  }
                }  
                event.setDropCompleted(success);  
            }  
        });  
        bp.setMinSize(50, 50);  
        return bp;  
    }