0

I am going to run a function in main class from another class. so its methods must be static so consequently all the variables inside it should be static too. it this method i am modifying chart objected that is imported from FXML created by scenebuilder. when i add chart objects as static objects, the program crashes with the following trace error.

what is the problem: this is what i am done for this:

@FXML
private Menu comPortsMenu;
@FXML
private   LineChart<Number,Number> chart1;
@FXML
private  LineChart<Number,Number> chart2;
@FXML
private  LineChart<Number,Number> chart3;
@FXML
private  LineChart<Number,Number> chart4;
public static    ArrayList<XYChart.Series<Number, Number>> series;

This is the secound class that runs a methode from firt one

 private void applyConfig(ActionEvent actionEvent) throws IOException {
   int x1=fullNameTotelemID.get(boxMap.get(chartOption1.getSelectionModel().getSelectedIndex()));
   int x2=fullNameTotelemID.get(boxMap.get(chartOption2.getSelectionModel().getSelectedIndex()));
   int x3=fullNameTotelemID.get(boxMap.get(chartOption3.getSelectionModel().getSelectedIndex()));
   int x4=fullNameTotelemID.get(boxMap.get(chartOption4.getSelectionModel().getSelectedIndex()));
   FXMLDocumentController.desiredCharts.set(0, x1);
   FXMLDocumentController.desiredCharts.set(0, x2);
   FXMLDocumentController.desiredCharts.set(0, x3);
   FXMLDocumentController.desiredCharts.set(0, x4);
   FXMLDocumentController.loadCharts();
    closeButtonAction(actionEvent);
}

and this is the static methode in the main class

 public static void loadCharts(){
     chart1.getData().removeAll();
            chart2.getData().removeAll();
            chart3.getData().removeAll();
            chart4.getData().removeAll();
            chart1.getData().add(series.get(desiredCharts.get(0)));
            chart2.getData().add(series.get(desiredCharts.get(1)));
            chart3.getData().add(series.get(desiredCharts.get(2)));
            chart4.getData().add(series.get(desiredCharts.get(3)));
            chart1.setTitle(chartOptions.get(desiredCharts.get(0)));
    chart2.setTitle(chartOptions.get(desiredCharts.get(1)));
    chart3.setTitle(chartOptions.get(desiredCharts.get(2)));
    chart4.setTitle(chartOptions.get(desiredCharts.get(3)));
}

this is the trace error:

 Executing /Users/apple/NetBeansProjects/JavaFXApplication6/dist/run1812641360/JavaFXApplication6.jar using platform /Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/Home/jre/bin/java
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:497)
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:497)
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:745)
Caused by: javafx.fxml.LoadException: 
     file:/Users/apple/NetBeansProjects/JavaFXApplication6/dist/run1812641360/JavaFXApplication6.jar!/javafxapplication6/FXMLDocument.fxml

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at javafxapplication6.JavaFXApplication6.start(JavaFXApplication6.java:22)
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)
Caused by: java.lang.NullPointerException
at javafxapplication6.FXMLDocumentController.initialize(FXMLDocumentController.java:441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 14 more
Exception running application javafxapplication6.JavaFXApplication6
Java Result: 1
a.ghad
  • 13
  • 6
  • Any chance you post the code that causes the error (`at javafxapplication6.FXMLDocumentController.initialize(FXMLDocumentController.java:441)`)? Also you're mistaken if you think the only way - or even the best way - of comunicating with a controller are `static` members. – fabian Mar 20 '16 at 10:23
  • You've already answered your own question: `@FXML`-annotated fields cannot be static. (It just doesn't make any sense to make them static; they are specifically members of the controller *instance* created by the `FXMLLoader`.) Just pass the instance of the controller created by the `FXMLLoader` to whatever class needs to call that method, or (better) [use a shared data model](http://stackoverflow.com/questions/32342864/applying-mvc-with-javafx). – James_D Mar 20 '16 at 12:37

0 Answers0