我已经尝试过寻找解决方案,但却无法在网上找到解决方案。所以我假设我是唯一一个遇到问题并发布代码和输出的人。
对话框显示并在选择文件时,返回null而不是文件。我已经尝试过强行解决这个问题,但还没有能够解决它。
包装样品;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import java.io.File;
import java.io.FileNotFoundException;
public class Main extends Application {
AlertBox alert = new AlertBox();
File fileSelected;
//FileChooser file = new FileChooser();
@Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("Java Code Generator");
primaryStage.isAlwaysOnTop();
primaryStage.setResizable(false);
primaryStage.centerOnScreen();
primaryStage.getIcons().add(new Image("file:C:\\Users\\Surya\\IdeaProjects\\UMLtoJava\\src\\sample\\codegen.png"));
Label label = new Label("Please choose an option:");
VBox layout2 = new VBox(25);
HBox layout = new HBox(20);
layout2.setAlignment(Pos.CENTER);
layout2.getChildren().add(label);
Button xml = new Button("Import XMI");
Button close = new Button("Close");
Label exc = new Label("Please select a file");
Button trial = new Button("Trial");
layout.setAlignment(Pos.CENTER);
layout.getChildren().add(xml);
layout.getChildren().add(close);
//layout.getChildren().add(trial);
layout2.getChildren().add(layout);
FileChooser.ExtensionFilter exts = new FileChooser.ExtensionFilter("XMI files (*.xmi)", "*.xmi");
FileChooser file = new FileChooser();
file.getExtensionFilters().add(exts);
xml.setOnAction(e -> {
fileSelected = file.showOpenDialog(primaryStage);
try {
String name = file.getInitialFileName();
System.out.println(name);
App gen = new App();
gen.app(fileSelected);
} catch (FileNotFoundException f) {
f.printStackTrace();
}
});
trial.setOnAction(e -> {
App abc = new App();
try {
File trialfile = new File("C:\\Users\\Surya\\Documents\\Java From UML\\XML UML\\trial.xmi");
abc.app(trialfile);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
});
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
boolean confirm = alert.alert("Confirm", "Are you sure you want to close?");
if (confirm) {
primaryStage.close();
}
}
});
close.setOnAction(e -> {
boolean confirm = alert.alert("Confirm", "Are you sure you want to close?");
if (confirm) {
primaryStage.close();
}
});
primaryStage.setScene(new Scene(layout2, 500, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package sample;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class App {
AlertBox alert = new AlertBox();
Button validate;
Button gen;
Button xmlfile;
String text;
StringBuilder input;
Scanner filein;
public void app(File file) throws FileNotFoundException {
Stage window = new Stage();
window.setTitle("UML to Java Code Gen");
window.setMaximized(true);
window.getIcons().add(new Image("file:C:\\Users\\Surya\\IdeaProjects\\UMLtoJava\\src\\sample\\codegen.png"));
Scene scene;
VBox layout = new VBox(100);
HBox inlayout = new HBox(20);
layout.setAlignment(Pos.CENTER);
inlayout.setAlignment(Pos.CENTER);
validate = new Button("Validate XMI");
gen = new Button("Generate Code");
gen.setDisable(true);
xmlfile = new Button("Import XMI");
inlayout.getChildren().addAll(validate, gen, xmlfile);
Scanner filein = new Scanner(file);
while(filein.hasNext()){
text = filein.nextLine();
input.append(text);
}
TextArea displayfile = new TextArea(input.toString());
displayfile.setWrapText(true);
displayfile.setPrefColumnCount(100);
layout.getChildren().add(inlayout);
layout.getChildren().add(displayfile);
scene = new Scene(layout);
window.setScene(scene);
window.show();
window.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
boolean confirm = alert.alert("Confirm", "Are you sure you want to close?");
if (confirm) {
window.close();
}
}
});
}
}
null
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at sample.App.app(App.java:44)
at sample.Main.lambda$start$0(Main.java:56)
at sample.Main$$Lambda$70/1220713843.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8216)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3724)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3452)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1728)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2461)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:348)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:273)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:382)
at com.sun.glass.ui.View.handleMouseEvent(View.java:553)
at com.sun.glass.ui.View.notifyMouse(View.java:925)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$38/597514034.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)