0

I'm trying to use the MVC framework for my JavaFX project. I've implemented a model class and have made sure that I'm using the same model instance in all my other controllers, but I keep on getting NullPointerExceptions. I based my implementation of the model from this: Applying MVC With JavaFx . I've done my best to abstract the problem in the code below.

Model Class:

public class Model {

public void test(String s){
    System.out.println(s);
}

Within my main class, the start function:

public class Main extends Application {
    public static Stage primaryStage;
    Model model = new Model();

    public void start(Stage window) throws Exception{
    primaryStage = new Stage();
    window = primaryStage;

    Parent root = FXMLLoader.load(getClass().getResource("LibraryView.fxml"));
    Scene scene = new Scene(root);
    window.setTitle("App Title");

    LibraryController libraryController = new LibraryController();

    //Setting the model
    libraryController.initLibraryModel(model);

    window.setScene(scene);
    window.show();
}

I've narrowed the problem down to the Initialize functions I've created for each controller.

public class LibraryController implements Initializable{
private Model model;
public void initLibraryModel(Model model){
    //Checks to make sure that there is only one model instance
    if(this.model != null){
        System.out.println("Error: More then one model");
    }

    this.model = model;
}

//Occurs when the DELETE button is pressed
AddImage AI = new AddImage();
public void handleDeletePressed(){
    model.test("Library Model Working");
}

Any pointers and suggestions to fix this code would be much appreciated!

EDIT: I'm not sure if this is what you mean by stacktrace, but I believe this is it:

    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.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.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:380)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
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:1771)
    ... 49 more
Caused by: java.lang.NullPointerException
    at sample.LibraryController.setAddPicStage(LibraryController.java:56) //This is the line with the model.test() function
    ... 59 more
Community
  • 1
  • 1
  • 1
    What happens if `initLibraryModel` is never called? (And if that init method is an override, annotate it.) – chrylis -cautiouslyoptimistic- Oct 21 '16 at 04:59
  • could you post the exception stack trace? also where in your code does the null pointer exception occur? – Blip Oct 21 '16 at 05:05
  • I hope this is what you mean by stacktrace, and thank you for any help you can give me! – PlatinumGlasses Oct 21 '16 at 05:29
  • 2
    You load an FXML file using `FXMLLoader`. The `FXMLLoader` creates a controller instance what you can get by calling `getController()`. But you create a new controller instance manually and you set the model to that instance. In the end you have two controllers: one that is created by the `FXMLLoader` (which one is "associated with the GUI displayed") which has no model attached, and one, created manually by you, but which one has absolutely no relation with the displayed window. So when you interact with the window, you are using the first one, which has no model, hence the error. – DVarga Oct 21 '16 at 09:14
  • @DVarga This may be the reason, sorry if this is an obvious question, but whenever I try to use FXMLLoader.getController() in my Main class, I get a non-static reference error. But the function isn't static. – PlatinumGlasses Oct 21 '16 at 13:46
  • @DVarga So I was able to create a new FXMLLoader instance, and used that to get the controller, however I'm still getting a null pointer exception when the compiler reads the initial initialise line – PlatinumGlasses Oct 21 '16 at 13:59

1 Answers1

0

Where is the model object that you have created for the initLibraryModel function, the object is never instantiated so the null pointer exception.

Rahul Singh
  • 19,030
  • 11
  • 64
  • 86