Reputation: 1
Following is the NullPointerException I'm getting in initialize method of controller class:
`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.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.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:352)
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$354(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
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: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:1769)
... 48 more
Caused by: javafx.fxml.LoadException:
file:/C:/Users/W7/Documents/NetBeansProjects/PivotNSE/dist/run2050028213/PivotNSE.jar!/pivotnse/popupFXML.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 pivotnse.PivotFXController.handleLoadButton(PivotFXController.java:173)
... 58 more
Caused by: java.lang.NullPointerException
at pivotnse.PivotFXController.initialize(PivotFXController.java:294)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 66 more`
What I am trying to do is, when myLoadButton is clicked, a new window pops up and shows in the Label of new window all the Symbols that are currently getting "downloaded"! (kind of like "real time update" to user). Just so the user knows, what's happening. Following is the Code:
public class PivotFXController implements Initializable {
final ObservableList<GetPivots> data = FXCollections.observableArrayList();
@FXML
private TableView<GetPivots> pivotTable;
@FXML
private TableColumn<GetPivots, String> symbolColumn;
@FXML
private TableColumn<GetPivots, String> pivotColumn;
@FXML
private TableColumn<GetPivots, String> r1Column;
@FXML
private TableColumn<GetPivots, String> r2Column;
@FXML
private TableColumn<GetPivots, String> r3Column;
@FXML
private TableColumn<GetPivots, String> s1Column;
@FXML
private TableColumn<GetPivots, String> s2Column;
@FXML
private TableColumn<GetPivots, String> s3Column;
@FXML
private Label statusLabel;
@FXML
private Button myLoadButton;
@FXML
private Button doneButton;
@FXML
private TextField symbolField;
private String SYMBOLS_FILE = "SYMBOLS.txt";
private static List<String> symbolList = new ArrayList<String>();
private boolean handleSymbol(String symbol) {
System.out.println(symbol + " is the input");
// statusLabel.setText("Downloading... " + symbol);
FetchDataNSE fdn = new FetchDataNSE();
if (fdn.getDataNSE(symbol).size() > 0) {
CalculatePivot cp = new CalculatePivot(fdn.getDataNSE(symbol));
GetPivots gp = new GetPivots(cp);
data.add(gp);
// statusLabel.setText(symbol + " " + "Downloaded!");
return true;
}
return false;
}
@FXML
private void handleLoadButton(ActionEvent event) throws IOException {
Stage stage;
Parent root;
if (event.getSource() == myLoadButton) {
//get reference to the button's stage
stage = (Stage) myLoadButton.getScene().getWindow();
//load up OTHER FXML document
root = FXMLLoader.load(getClass().getResource("popupFXML.fxml"));// <-- ****this line is causing problem**** I am getting NULLPOINTEREXCEPTION from 1st line in `initialize` method WHY?
stage.setScene(new Scene(root));
stage.setTitle("DOWNLOADING STATUS");
stage.initModality(Modality.APPLICATION_MODAL);
stage.showAndWait();
loadSymbols();
} else {
stage = (Stage) doneButton.getScene().getWindow();
root = FXMLLoader.load(getClass().getResource("pivotFXGUI.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
private void loadSymbols() {
data.clear();
symbolList.clear();
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(SYMBOLS_FILE));
while ((sCurrentLine = br.readLine()) != null) {
symbolList.add(sCurrentLine.trim());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
for (String sym : symbolList) {
statusLabel.setText("Downloading... " + sym);
handleSymbol(sym);
}
statusLabel.setText("LOADING SYMBOLS DONE!");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// 1st line below gives NullPointerException! why does that happen?
symbolColumn.setCellValueFactory(new PropertyValueFactory<>("symbolProperty"));
pivotColumn.setCellValueFactory(new PropertyValueFactory<>("pivotProperty"));
r1Column.setCellValueFactory(new PropertyValueFactory<>("r1Property"));
r2Column.setCellValueFactory(new PropertyValueFactory<>("r2Property"));
r3Column.setCellValueFactory(new PropertyValueFactory<>("r3Property"));
s1Column.setCellValueFactory(new PropertyValueFactory<>("s1Property"));
s2Column.setCellValueFactory(new PropertyValueFactory<>("s2Property"));
s3Column.setCellValueFactory(new PropertyValueFactory<>("s3Property"));
pivotTable.setItems(data);
// myLoadButton.fire();
// pivotTable.getColumns().addAll(symbolColumn,s3Column, s2Column,s1Column, pivotColumn, r1Column, r2Column, r3Column );
}
}
`
Why is the 1st line in the "initialize" method throwing NullPointerException!?
What am I doing incorrectly in handleLoadButton(..)
method and initialize(..)
method?
When I remove everything from initialize
method program opens up new window when I click myLoadButton, but then NullPointerException occurs when loadSymbols()
method is called...
ALSO:::
at pivotnse.PivotFXController.handleLoadButton(PivotFXController.java:173)
... 58 more
Caused by: java.lang.NullPointerException
at pivotnse.PivotFXController.initialize(PivotFXController.java:294)
line 173 is following:::
root = FXMLLoader.load(getClass().getResource("popupFXML.fxml"));
and line 294 is the 1st line in initialize()
method.
Upvotes: 0
Views: 2889
Reputation: 823
The relation between fxml documents and the controllers is 1:1 by design. In other words, each fxml document should have its own controller.
Right now you're trying to load popupFXML.fxml
with pivotnse.PivotFXController
as controller. So your initialize()
executes symbolColumn.setCellValueFactory()
while symbolColumn
wasn't injected, because popupFXML.fxml
doesn't have a symbolColumn
. Result: NPE.
You should make a seperate controller class for popupFXML.fxml
and reference it in your fxml like fx:controller="pivotnse.PopupFXMLController"
Upvotes: 1
Reputation: 10650
Probably "symbolColumn" has not been initialized. Maybe you have forgotten or misspelled the fx:id in your fxml-file.
Upvotes: 1