0

I don't know if this helps but if I run this code in which i have commented out the setText lines it returns "pointer ok" which suggests that the extCon.setStartValues is pointing to the correct method, although as you pointed out this is another instance of the from and not the one I actually want to populate.

package extrusion;

//import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

public class ExtrusionController {
 
 private double weight = 16.0;
 private double rpm = 30.0;
 private double time = 0.5;

     @FXML
     private TextField timeSecs;

     @FXML
     private TextField revsPerMin;

     @FXML
     private TextField sampleWeight;

//     @FXML
//     void 838383(ActionEvent event) {
//
//     }

    @FXML
    public void setStartValues(){
     System.out.println("pointer ok");
//     sampleWeight.setText("" + weight);
//     revsPerMin.setText("" + rpm);
//     timeSecs.setText("" + time);
     }
}

package extrusion;

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class ExtrusionApp extends Application {
 
    private AnchorPane extrusionForm;
 private ExtrusionController extrusionController;
 
 public void start(Stage primaryStage) {
  ExtrusionController extCon = new ExtrusionController();
  primaryStage.setTitle("ColorMatrix Extrusion");
  FXMLLoader loader = new FXMLLoader();
  loader.setLocation(getClass().getResource("ExtrusionForm.fxml"));
  try {
   extrusionForm = loader.load();
   extrusionController = loader.getController();
   extCon.setStartValues();
  } catch (IOException e) {
   e.printStackTrace();
  }
  Scene scene = new Scene(extrusionForm);
  primaryStage.setScene(scene);
  primaryStage.show();
 }

 public static void main(String[] args) {
  Application.launch(args);
 }

}

Thanks James. Here's the Class with extCon commented out and replaced in with extrusionController.setStartValues() which is still giving a null pointer exception. However if I run the original code I posted but comment out the three setText statements in the ExtrusionController class I no longer get the null pointer exception and the code seems to run ok, obviously not populating the TextFields however.

package extrusion;

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class ExtrusionApp extends Application {
 
    private AnchorPane extrusionForm;
 private ExtrusionController extrusionController;
 
 public void start(Stage primaryStage) {
  //ExtrusionController extCon = new ExtrusionController();
  primaryStage.setTitle("ColorMatrix Extrusion");
  FXMLLoader loader = new FXMLLoader();
  loader.setLocation(getClass().getResource("ExtrusionForm.fxml"));
  try {
   extrusionForm = loader.load();
   extrusionController = loader.getController();
   extrusionController.setStartValues();
  } catch (IOException e) {
   e.printStackTrace();
  }
  Scene scene = new Scene(extrusionForm);
  primaryStage.setScene(scene);
  primaryStage.show();
 }

 public static void main(String[] args) {
  Application.launch(args);
 }

}

Exception in Application start method
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 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
 at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
 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.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
 at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
 at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
 at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
 at extrusion.ExtrusionApp.start(ExtrusionApp.java:23)
 at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
 at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
 at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
 at java.security.AccessController.doPrivileged(Native Method)
 at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
 at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
 at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
 at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
 ... 1 more
Exception running application extrusion.ExtrusionApp

I'm new to Java and I'm really struggling with a null pointer exception to a TextField. I'm trying to populate the TextFields so when the app opens there is already some data in the fields. I know that I'm pointing to the correct method in the ExtrusionController, as I have tried a System.out.println command in the method and it displays ok. I think the problem might be with the references to the TextFields. I've read dozens of forum posts over the last couple of days but I'm going round in circles.

package extrusion;

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class ExtrusionApp extends Application {

    private AnchorPane extrusionForm;
    private ExtrusionController extrusionController;

    public void start(Stage primaryStage) {
        ExtrusionController extCon = new ExtrusionController();
        primaryStage.setTitle("ColorMatrix Extrusion");
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("ExtrusionForm.fxml"));
        try {
            extrusionForm = loader.load();
            extrusionController = loader.getController();
            extCon.setStartValues();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Scene scene = new Scene(extrusionForm);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }

}

package extrusion;

//import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

public class ExtrusionController {

    private double weight = 16.0;
    private double rpm = 30.0;
    private double time = 0.5;

        @FXML
        private TextField timeSecs;

        @FXML
        private TextField revsPerMin;

        @FXML
        private TextField sampleWeight;

//      @FXML
//      void 838383(ActionEvent event) {
//
//      }

    @FXML
    public void setStartValues(){
        System.out.println();
        sampleWeight.setText("" + weight);
        revsPerMin.setText("" + rpm);
        timeSecs.setText("" + time);
        }
}
I Campbell
  • 11
  • 3
  • 1
    Try using the `extrusionController` variable you get from parsing the xml file, not `extCon` that you create yourself. The textfields will remain null in that one if you do not link it to any layout xml file. – Malte Hartwig Oct 16 '17 at 14:51
  • Thanks Malte but when I change to extrusionContoller.setStartValues i still get a null pointer exception but i don't even get the println which would suggest that it's no longer pointing at that method. – I Campbell Oct 16 '17 at 15:07
  • Post the version that uses `extrusionController` (get rid of `extCon` entirely), along with the complete stack trace, in the question. Identify which line is actually throwing the exception. If `loader.getController()` is returning null, you will probably need to post your FXML file too. – James_D Oct 16 '17 at 15:09
  • Do you think you could tell us which line is line 23? Or do you not actually want help? – James_D Oct 16 '17 at 15:29
  • Sorry James, line 23 is .setStartValues – I Campbell Oct 16 '17 at 15:35
  • So obviously the controller you get from `loader.getController()` (i.e. `extrusionController`) is null. – James_D Oct 16 '17 at 15:38

1 Answers1

1

extCon and extrusionController contain 2 different controller instances. extrusionController is used when loading the fxml and objects from the fxml are injected to this instance.

However you call the setStartValues method for the extCon instance that is not used with a fxml and therefore contains null values in fields.

fabian
  • 80,457
  • 12
  • 86
  • 114
  • Thanks also Fabian but as i explained to Malte when I change to extrusionContoller.setStartValues i still get a null pointer exception but i don't even get the println which would suggest that it's no longer pointing at that method. – I Campbell Oct 16 '17 at 15:07