0

I'm making program with javafx, but I can't solve my errors. java.lang.reflect.InvocationTargetException

I already search about this at this page, but I can't solve this...

this is my code at MlCalcController.java

public class MlCalcController implements Initializable {
    @FXML
    private Label FirstOneName;
    private TextField FirName;
    private TextField FirMl; 
    private TextField FirPrice; 
    private TextField FirPerprice;

    public void CalculateButtonAction (ActionEvent event) throws IOException{
        FXMLLoader loader = new FXMLLoader(getClass().getResource("MlCalc.fxml"));
        String name = FirName.getText();
        int ml = Integer.parseInt(FirMl.getText());
        int price = Integer.parseInt(FirPrice.getText());
        int div, sum;
        div = ml/100;
        sum = price/div;
        FirPerprice.setText(String.valueOf(sum));
        FirstOneName.setText(name);
    }

and this is fxml code

<AnchorPane id="AnchorPane" prefHeight="480.0" prefWidth="720.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="jproject.MlCalcController">
   <children>
      <HBox alignment="CENTER" layoutX="313.0" layoutY="369.0" prefHeight="35.0" prefWidth="287.0" AnchorPane.bottomAnchor="15.0" AnchorPane.rightAnchor="10.0">
         <children>
            <Button fx:id="CalculateButton" onAction="#CalculateButtonAction" prefHeight="35.0" prefWidth="75.0" text="Calculation">
               <HBox.margin>
                  <Insets left="10.0" right="10.0" />
               </HBox.margin></Button>
            <Button mnemonicParsing="false" prefHeight="35.0" prefWidth="75.0" text="Compare">
               <HBox.margin>
                  <Insets left="10.0" right="10.0" />
               </HBox.margin></Button>
            <Button mnemonicParsing="false" prefHeight="35.0" prefWidth="75.0" text="Go Home">
               <HBox.margin>
                  <Insets left="10.0" />
               </HBox.margin></Button>
         </children>

      <HBox layoutX="36.0" layoutY="76.0" prefHeight="35.0" prefWidth="635.0">
         <children>
            <TextField fx:id="FirName" prefHeight="54.0" prefWidth="93.0">
               <HBox.margin>
                  <Insets right="20.0" />
               </HBox.margin>
            </TextField>
            <TextField fx:id="FirMl" prefHeight="54.0" prefWidth="93.0">
               <HBox.margin>
                  <Insets right="20.0" />
               </HBox.margin>
            </TextField>
            <TextField fx:id="FirPrice" prefHeight="54.0" prefWidth="93.0">
               <HBox.margin>
                  <Insets right="140.0" />
               </HBox.margin>
            </TextField>
            <TextField fx:id="FirPerprice" prefHeight="35.0" prefWidth="143.0" />
         </children>
      </HBox>
 <Label fx:id="FirstOneName" layoutX="36.0" layoutY="224.0" prefHeight="64.0" prefWidth="268.0" text="Label" />

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    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:8411)
    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:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)
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:498)
    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:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 48 more
Caused by: java.lang.NullPointerException
    at jproject.MlCalcController.CalculateButtonAction(MlCalcController.java:34)
    ... 58 more

I want calculate drink price per 100ml. here's example First drink is 20$ at 1000ml. and this drink is 2$ at 100ml. I want result like this.

YunDS
  • 1
  • 1
  • I'd expect there to be a nested exception within the InvocationTargetException which will tell you more about what's going wrong. – Jon Skeet May 26 '19 at 07:44
  • I'm sorry for understanding your comment. sorry. I'm not sure about more codes? – YunDS May 26 '19 at 07:45
  • Post the full stacktrace. An InvocationTargetException occurs when a reflective call fails due to some other reason. Thus the full stack trace contains the base exception and reason. Without it we won't be able to help you out. – akortex May 26 '19 at 07:45
  • Oh sorry I'll edit it – YunDS May 26 '19 at 07:46
  • "Caused by: java.lang.NullPointerException" - so there you go. – Jon Skeet May 26 '19 at 07:50
  • 1
    Note you need an `@FXML` annotation on **each** (non-public) field which corresponds to an `fx:id` attribute in the FXML file. Maybe you're under the impression that your annotation is somehow being applied to each field under `FirstOneName`, but that's not how it works; you currently only have an `@FXML` annotation on the `FirstOneName` field. – Slaw May 26 '19 at 08:11
  • Well.. I deleted lines about label, but I think `price per 100ml` textfield cannot found. and also I added `@fxml` up the `CalculateButtonAction` method. – YunDS May 26 '19 at 08:25
  • There is no ` – fabian May 26 '19 at 08:37
  • and in the error logs, NullPointerException occured at `int ml = Integer.parseInt(FirMl.getText()); int price = Integer.parseInt(FirPrice.getText());` here. but I have no idea at this. do I have to set them like `int ml = NULL; int price = NULL;` ? – YunDS May 26 '19 at 08:39

0 Answers0