0

I am some how trapped between 3 classes, and I can not find out why I get null value. I want to pass a file path after choosing to Draw button, and after clicking draw, a constructor from other class should get fired. I have a controller class with 2 constructors. One with String parameter and one empty. I have 2 other classes which are controllers for 2 fxml files. One is OverViewController.java (controller for overview.fxml) which controls a Draw button , the other one is rootlayout.java (controller for rootlayout.fxml) which controls menu bar on rootlayout.fxml.

RootLayoutController.java is a controller for rootlayout.fxml in which there is the menu bar. OverViewController.java is a controller for OverView.fxml.

Controller.java is just a class which interferes between view and model.

I choose the file here in RootLayoutController:

private OverViewController overview = new OverViewController();
....
    @FXML
private void handleOpen() {
    FileChooser fileChooser = new FileChooser();

    // Set extension filter
    FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(
            "3lgm2 files (*.z3lgm)", "*z3lgm");
    fileChooser.getExtensionFilters().add(extFilter);

    // Show save file dialog
    File file = fileChooser.showOpenDialog(main.getPrimaryStage());
    path = file.toString();
    if (path != null) {

         overview.setPath(path);//path is initialized here

    }

}

Then in OverViewController I want to new the controller.java constructor,and initialize fields, then method draw shall becalled, which creates the TreeTableView.

/**
 * Triggered by clicking on draw button
 * 
 * @param event
 */
public void treeTableDraw(ActionEvent event) {

    //FXMLLoader loader = new FXMLLoader();
    //RootLayoutController rlc = loader.getController();
    //String p = rlc.getPath();
    //solution above did not work either///

    new Controller(path);//path is empty here! although setPath method was successfully initialized in former class

    drawTable();

    numberOfFunctions = dc.getFuncAll().size();
    numberOfOrganizations = dc.getSortedAssignedOrg().size();
    funcLabel.setText(numberOfFunctions + "");
    orgLabel.setText(numberOfOrganizations + "");
    btnDraw.setDisable(true);

}

And this is the controller

public Controller() {

}

public Controller(String path) {
    ParsFile pars = new ParsFile();
    pars.parsingData(path);
    FunctionTypeOrg fft = new FunctionTypeOrg(pars);
    CombinedOrganisations cmo = new CombinedOrganisations(pars, fft);
    CombiFunctions fcf = new CombiFunctions(pars);
    cmo.findCombinedOrg();
    CombinedOrganisations.Tail tt = cmo.new Tail();
    TreeMap<String, Set<String>> results = new TreeMap<String, Set<String>>();
    tt.process(cmo.getMap_combiOrg(), results);

    funcTypeOrg = fft.findFuncType();
    orgAll = pars.getMap_Org();
    combiOrg = tt.getEndResults();
    combiFunc = fcf.findCombiFunctions();
    setNumberOfOrganizations(fft.getMap_FuncType().size());
    numberOfFunctions = pars.getCountAufgabe();


    funcAll = pars.getMap_Auf();
    DataConstructor dc = new DataConstructor(orgAll, funcTypeOrg, combiOrg,
            combiFunc, funcAll);

}

And Here is the error:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1770)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1653)
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:8390)
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:3758)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$209/665883.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:927)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
at com.sun.glass.ui.gtk.GtkApplication$$Lambda$41/19140780.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:497)
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:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1765)
... 54 more
   Caused by: java.lang.NullPointerException
   at java.io.File.<init>(File.java:277)
   at java.util.zip.ZipFile.<init>(ZipFile.java:121)
   at model.OpenFile.createDoc(OpenFile.java:29)
   at model.ParsFile.findUserDefHashCode(ParsFile.java:344)
   at model.ParsFile.parsingData(ParsFile.java:136)
   at controller.Controller.<init>(Controller.java:58)
   at view.OverViewController.treeTableDraw(OverViewController.java:109)
    ... 64 more
Iman
  • 769
  • 1
  • 13
  • 51
  • Please show us the error! – The Well Mar 16 '15 at 10:30
  • It looks like you are creating a new `OverviewController` instance, and initializing the `path` property of that, instead of initializing the `path` property of the instance the `FXMLLoader` created and associated with the UI. So the `path` property for the instance whose `treeTableDraw` method is invoked in response to the button press never has its `path` property initialized. – James_D Mar 16 '15 at 12:29
  • I thought when i make new in OverviewController, since it is a controller for underlying fxml file, it initializes the file too. shall I use RootLayoutController rlc = loader.getController(); ? – Iman Mar 16 '15 at 12:56
  • Yes, get the controller from the loader. – James_D Mar 16 '15 at 13:10
  • I still get nullpointer. now on rlc. Is this correct what I do? FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/RootLayout.fxml")); RootLayoutController rlc = loader.getController(); String p = rlc.getPath(); – Iman Mar 16 '15 at 13:21
  • The problem is the `OverviewController` instance. I assume `OverviewController` is the controller for some FXML file. You should get the instance associated with that loader, not create a new instance. But perhaps you need to explain the relationship between your various classes and FXML files, or (better) [create a simple example from scratch](http://stackoverflow.com/help/mcve) that demonstrates the problem. – James_D Mar 16 '15 at 14:08
  • I see. you are right about the OverviewController. I wrote some declaration about the relation between them. My problem is that i do not know how to get instance of loader correctly. – Iman Mar 16 '15 at 15:59

0 Answers0