当前位置: 首页 > 知识库问答 >
问题:

拖放中的NPE

夏令秋
2023-03-14

我在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在Linux测试相同的代码时7_25我得到了这个错误堆栈:

    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)  

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

(以及更多)

< code > int incoming index = integer . parse int(drag board . getstring())的内容;为空

你能告诉我如何解决这个问题或重写代码吗?

共有3个答案

屈宏爽
2023-03-14

检查你的dragboard.hasString()。如果dragboard.getString()为空,它肯定应该返回FALSE。所以,你永远不会到达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;  
    } 
阙庆
2023-03-14

彼得,

我浏览这个链接是为了了解更多关于拖放功能的信息。-http://docs . Oracle . com/Java FX/2/drag _ drop/jfxpub-drag _ drop . htm从他们在那里给出的例子来看,你似乎做到了预期的事情。

来到你的实际代码:我会试着理解我是否在剪贴板内容对象中放入一个空值。.类扩展了哈希映射 - http://docs.oracle.com/javafx/2/api/javafx/scene/input/ClipboardContent.html。

如果要获取的nodeIndex不是DataFormat.PLAIN_TEXT,则clipboard.putString()可能不会将索引放入hashmap。请参阅javadoc中的语句-"将此值设置为null会有效地从剪贴板中清除它。"并确认是否没有发生这种情况。

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

我会打印这两样东西(“putString()”的输出和“nodeIndex”的值)来调查出了什么问题。

-此致,拉维。

高嘉熙
2023-03-14

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

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

检查拖板的输出。

 类似资料:
  • pre { white-space: pre-wrap; } 如果您能够通过您的 Web 应用简单地实现拖动和放置,您就会知道一些特别的东西。通过 jQuery EasyUI,我们在 Web 应用中可以简单地实现拖放功能。 在本教程中,我们将向您展示如何创建一个启用用户拖动和放置用户想买的商品的购物车页面。购物篮中的物品和价格将更新。 显示页面上的商品     <ul>         <li>

  • pre { white-space: pre-wrap; } 本教程向您展示如何使 HTML 元素可拖动,在本例中,我们将创建三个 DIV 元素然后启用他们的拖动和放置。 首先,我们创建三个 <div> 元素:     <div id="dd1"></div>     <div id="dd2"></div>     <div id="dd3"></div> 对于第一个 <div> 元素,我们

  • 问题内容: 嘿,我一直在阅读本教程,以了解PyQt4中的拖放方法。但是,我无法理解以下几点。如果某事能够使我更清楚,那将是很好的。 为什么会有单独的self.button.move()和e.setDropAction()self.button.move()实际不移动按钮本身吗?有人可以解释一下drag.setHotSpot和drag.start()的作用吗?谢谢。 问题答案: 该教程已严重过时。从

  • 拖放是一种非常流行的用户界面模式。它的概念很简单:点击某个对象,并按住鼠标按钮不放,将鼠标移动到另一个区域,然后释放鼠标按钮将对象“放”在这里。拖放功能也流行到了Web 上,成为了一些更传统的配置界面的一种候选方案。 拖放的基本概念很简单:创建一个绝对定位的元素,使其可以用鼠标移动。这个技术源自一种叫做“鼠标拖尾”的经典网页技巧。鼠标拖尾是一个或者多个图片在页面上跟着鼠标指针移动。 单元素鼠标拖尾

  • #div1, #div2 {float:left; width:100px; height:35px; margin:10px;padding:10px;border:1px solid #aaaaaa;} function allowDrop(ev) { ev.preventDefault(); } function drag(ev) { ev.dataTransfer.setData("Tex

  • 我试图使用HTML5拖放在我的网站。dragstart和dragend被解雇,但dragenter,draglet,dragover和下降不工作,直到我刷新页面。 侧边栏中树中的节点应该能够拖动到表中的单元格。树是用jsTree制作的。我已经在火狐、Chrome和IE上尝试过了。他们都有相同的行为。 边栏树(rhtml文件) 删除操作代码(咖啡脚本文件) 表(页面来源) 我试着拉小提琴,但效果很好