Я хочу изменить root сцены, когда она достигает определенной ширины. Поэтому я создал прослушиватель изменений, в котором я изменяю root, и я пытаюсь добавить прослушиватель изменений в свойство width сцены, но я получаю исключение nullPointerException во время выполнения. Я не могу понять, почему это происходит, я пробовал со сценой, но я получаю ту же ошибку.
Это код:
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import javafx.stage.Modality;
import javafx.scene.Scene;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import javafx.beans.value.ChangeListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javafx.collections.ObservableList;
import javafx.collections.FXCollections;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ObservableValue;
import java.lang.Number;
public class MainController
{
@FXML Button infoButton;
@FXML Button aboutButton;
@FXML ComboBox<Integer> numberCombo;
@FXML Label symbolLabel;
@FXML Label nameLabel;
@FXML Label massLabel;
@FXML Label ceLabel;
Scene mainScene;
SimpleIntegerProperty selectedElement = new SimpleIntegerProperty();
ObservableList<Integer> numberOList;
List<Integer> numberList = new ArrayList();
EventHandler<ActionEvent> buttonClick;
ChangeListener<Number> selectedChange;
ChangeListener<Number> widthChange;
@FXML
public void initialize()
{
setHandlers();
mainScene = infoButton.getScene();
mainScene.widthProperty().addListener(widthChange); //ERROR OCURRS HERE <-----
infoButton.addEventHandler(ActionEvent.ACTION, buttonClick);
aboutButton.addEventHandler(ActionEvent.ACTION, buttonClick);
Table.createTable();
for(int i = 0; i < Table.getTable().size(); i++)
numberList.add(Integer.parseInt(Table.getElement(i).getNumber()));
numberOList = FXCollections.observableList(numberList);
numberCombo.setItems(numberOList);
numberCombo.getSelectionModel().selectFirst();
setSelectedElement(0);
numberCombo.addEventHandler(ActionEvent.ACTION, buttonClick);
selectedElement.addListener(selectedChange);
}
public void setHandlers()
{
buttonClick = new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
FXMLLoader loader = new FXMLLoader();
try
{
if(event.getSource() == infoButton)
{
loader.setLocation(getClass().getResource("\\layout\\info.fxml"));
loader.load();
InfoController controller = loader.getController();
controller.setSelectedElement(selectedElement.get());
infoButton.getScene().setRoot(loader.getRoot());
System.out.println("Info button clicked...");
}
else if(event.getSource() == aboutButton)
{
Stage aboutStage = new Stage();
aboutStage.setScene(new Scene(FXMLLoader.load(getClass().getClassLoader().getResource("\\layout\\about.fxml"))));
aboutStage.setResizable(false);
aboutStage.setWidth(400);
aboutStage.setHeight(250);
aboutStage.initModality(Modality.WINDOW_MODAL);
aboutStage.initOwner((Stage) infoButton.getScene().getWindow());
aboutStage.show();
System.out.println("About button clicked...");
}
else if(event.getSource() == numberCombo)
{
setSelectedElement(numberCombo.getValue()-1);
}
}
catch(IOException exception)
{
exception.printStackTrace();
}
}
};
selectedChange = new ChangeListener<Number>()
{
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue)
{
numberCombo.getSelectionModel().select(selectedElement.get());
symbolLabel.setText(Table.getElement(selectedElement.get()).getSymbol());
nameLabel.setText(Table.getElement(selectedElement.get()).getName());
massLabel.setText(Table.getElement(selectedElement.get()).getMass());
ceLabel.setText(Table.getElement(selectedElement.get()).getCE());
}
};
widthChange = new ChangeListener<Number>() //CHANGE LISTENER FOR THE WIDTH PROPERTY
{
FXMLLoader loader = new FXMLLoader();
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue)
{
if(newValue.doubleValue() >= Double.valueOf(600))
{
try
{
loader.setLocation(getClass().getResource("\\layout\\widemain.fxml"));
loader.load();
infoButton.getScene().setRoot(loader.getRoot());
}
catch(IOException exception)
{
exception.printStackTrace();
}
}
}
};
}
public void setSelectedElement(int selectedElement)
{
this.selectedElement.set(selectedElement);
}
public void setSelectedElement(SimpleIntegerProperty selectedElement)
{
this.selectedElement = selectedElement;
}
}
И это исключение, которое он бросает :
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2571)
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 Main.start(Main.java:16)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
at java.lang.Thread.run(Unknown Source) Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
... 17 more Caused by: java.lang.NullPointerException
at MainController.initialize(MainController.java:53)
... 27 more