0

I'm trying to send an ad to the viewAdController. The system out prints properly print the information of the ad, which means that it isn't null. I get a NullPointerException at this line:

vac.setAd(selectedAd);

Here's the rest of the code:

@FXML
private void handleOpenAd(ActionEvent event) {
    int adIndex = adsView.getSelectionModel().getSelectedIndex();
    try {
        selectedAd = ads[adIndex];
    } catch (Exception e) {
        errLbl.setText(errorString);
        return;
    }
    System.out.println(selectedAd.getTitle());
    System.out.println(selectedAd);

    FXMLLoader myLoader;
    myLoader = new FXMLLoader(getClass().getResource("ViewAd.fxml"));
    Tab tab = new Tab();
    tab.setText("View Ad");
    try {
        tab.setContent((Node) myLoader.load());
    } catch (IOException ex) {
        System.out.println(ex);
    }
    tabPane.getTabs().add(tab);
    ViewAdController vac = myLoader.<ViewAdController>getController();
    vac.setAd(selectedAd);
    try {
        vac.setCar(ClientController.getConnection().getCar(selectedAd.getAdId()));
    } catch (RemoteException ex) {
        Logger.getLogger(AdsController.class.getName()).log(Level.SEVERE, null, ex);
        throw new Error("No connection");
    }

    vac.setTabPane(tabPane, tab);
    tabPane.getSelectionModel().select(tab);
}

The setAd method from the viewAdController looks like this:

    public void setAd(Ad selectedAd) {
    titleLabel.setText(selectedAd.getTitle());
    descriptionArea.setText(selectedAd.getDescription());
    authorLbl.setText(selectedAd.getAuthor());
    this.author = selectedAd.getAuthor();
    imageView.setImage(selectedAd.getImageFX());
}

This has been working for a long time, and now it suddenly doesn't, and I'm not sure what has changed. Any help is appreciated.

The exceptions:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1762)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1645)
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: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.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$37/1147985808.run(Unknown Source)
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:1757)
... 51 more
Caused by: java.lang.NullPointerException
at GUI.AdsController.handleOpenAd(AdsController.java:131)
... 61 more
Jesper
  • 2,644
  • 4
  • 30
  • 65
  • The exception is at the line `vac.setAd(selectedAd);`, or at one of the lines in `setAd(...)` invoked from that line? – James_D Dec 14 '15 at 15:34
  • If it's really `vac.setAd(...)`, then `vac` must be `null`, which means either the FXML didn't load (you should see a one-line message from the `System.out.println(ex);` in your catch block, right before your NullPointerException stack trace), or your controller isn't properly specified. (I would change `System.out.println(ex);` to `throw new RuntimeException(ex);`, since that is an unrecoverable error.) – James_D Dec 14 '15 at 15:40
  • I don't understand why it's null. I'm using the exact same code to get the controller many other places, and also it has been working before, but for some reason it isn't anymore. – Jesper Dec 14 '15 at 15:50
  • 1
    If you're certain you're not getting an exception when you call `myLoader.load()` (I would really strongly advise making the change I suggested above to be sure of this), then post the FXML file (at least the portion that references the controller). Can you add some logging in the controller's `initialize()` method? – James_D Dec 14 '15 at 15:56
  • I did what you asked and now get an error at "tab.setContent((Node) myLoader.load());". I went to check the ViewAd.fxml and noticed an error inside the code. In the following line, #handleSendMessage is highlighted as the error with the message "Controller is not defined on root component": – Jesper Dec 14 '15 at 16:15
  • So you have figured it out, or not? I couldn't tell from your comment. If not, you need to post the FXML. – James_D Dec 14 '15 at 16:31
  • I think i can take it from here. Thanks – Jesper Dec 14 '15 at 16:43
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – fabian Dec 14 '15 at 17:07

0 Answers0