我是Javafx的新手。我正在通过开发一个简单的应用程序来学习Javafx。我正在尝试从javaFXML中的TextField获取值。我得到java.lang.reflect.InvocationTargetException
。
Javafx文件。
public class Manoj extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
FXMLDocument.fxml
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="480.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="manoj.FXMLDocumentController">
<children>
<AnchorPane layoutX="142.0" layoutY="36.0" prefHeight="361.0" prefWidth="360.0">
<children>
<Label fx:id="result" alignment="CENTER" contentDisplay="CENTER" layoutX="61.0" layoutY="289.0" opacity="0.51" prefHeight="77.0" prefWidth="169.0" text="Result Of Addition">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="26.0" layoutY="108.0" prefHeight="32.0" prefWidth="90.0" text="Number#1 ">
<font>
<Font size="14.0" />
</font>
</Label>
<Label alignment="TOP_CENTER" contentDisplay="CENTER" text="First Application" textAlignment="CENTER">
<font>
<Font name="Cambria" size="50.0" />
</font>
</Label>
<Label layoutX="26.0" layoutY="155.0" prefHeight="32.0" prefWidth="90.0" text="Number#2">
<font>
<Font size="14.0" />
</font>
</Label>
<Button fx:id="button" alignment="CENTER" contentDisplay="CENTER" layoutX="83.0" layoutY="216.0" onAction="#addNumber" prefHeight="44.0" prefWidth="125.0" text="Add Number">
<font>
<Font name="Cambria" size="14.0" />
</font>
</Button>
<TextField fx:id="textone" layoutX="141.0" layoutY="108.0" prefHeight="32.0" prefWidth="179.0" />
<TextField fx:id="texttwo" layoutX="141.0" layoutY="155.0" prefHeight="32.0" prefWidth="179.0" />
</children>
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" />
</padding>
</AnchorPane>
</children>
<padding>
<Insets top="20.0" />
</padding>
</AnchorPane>
FXMLDocumentController.java
public class FXMLDocumentController implements Initializable {
@FXML
private Label result;
private TextField textone;
public FXMLDocumentController() {
}
@FXML
private void addNumber(ActionEvent event) {
System.out.println("Clicked. !");
String str = textone.getText();
result.setText(str);
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
我正在使用带有场景构建器的netbeans进行开发。当我运行这个项目并单击Add Number按钮时,我得到了一个例外。
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1768)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1651)
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:204)
at javafx.scene.Node.fireEvent(Node.java:8175)
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:204)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3746)
at javafx.scene.Scene$MouseHandler.access$1800(Scene.java:3471)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1695)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2486)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:314)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:243)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:345)
at com.sun.glass.ui.View.handleMouseEvent(View.java:526)
at com.sun.glass.ui.View.notifyMouse(View.java:898)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1763)
... 47 more
Caused by: java.lang.NullPointerException
at manoj.FXMLDocumentController.addNumber(FXMLDocumentController.java:33)
... 57 more
请帮我摆脱这个例外。提前谢谢。
答案 0 :(得分:6)
感谢您的评论Jose Pereda。
只是一个简单的粗心错误。
正如Jose Pereda所说的那样,我为textone
添加了@FXML注释,它运行良好。
答案 1 :(得分:0)
package net.remark.abs.invoice;
import java.util.Date;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import net.remark.abs_bos.Invoice;
import net.rojer.core.db.DataBase;
import net.rojer.javafx.tool.Tools;
public class FrameInvoice extends AnchorPane{
@FXML
private TextField fldInvoiceNumber;
@FXML
private Label lblPrice;
public FrameInvoice(){
Tools.innitiateFxml(this);
}
@FXML
private void onSave (ActionEvent event){
Invoice invo = new Invoice();
invo.setDate(new Date());
invo.setTotal(0);
invo.setStat((true));
DataBase.begin();
DataBase.em().persist(invo);
DataBase.commit();
System.out.println("true");
}
}
答案 2 :(得分:0)
我看过很多关于这个错误的帖子,其中很多都失败了。对我来说也是如此。尝试再次制作fxml文件,让IDE创建控制器和css。保持默认控制器和css名称不变,并将新创建的“.fxml”文件附加到代码中。对我来说,它有效地复制了场景构建器中的内容,但没有使用xml代码。