Witam,
mam problem, tworzę "Menadżer zadań" i utknąłem przy zmianie widoku podczas przyciśnięcia buttonu Dodaj Uzytkownika, treść błędu:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at controllers.PanelGlownyController$1.handle(PanelGlownyController.java:28)
at controllers.PanelGlownyController$1.handle(PanelGlownyController.java:1)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.control.MenuItem.fire(Unknown Source)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(Unknown Source)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$324(Unknown Source)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer$$Lambda$272/1586419481.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/1109371569.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Kod klasy PanelGlownyController:
package controllers;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import taskmanager.ZmianaSceny;
public class PanelGlownyController implements Initializable {
@FXML
private MenuBar menuBar;
@FXML
private MenuItem btnAddUser;
@FXML
private AnchorPane anchorPane;
public void konfigurujMenu() {
btnAddUser.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
Stage ownerStage = (Stage) menuBar.getScene().getWindow();
ZmianaSceny zs = new ZmianaSceny();
zs.changeViewModalityWindow("/FXMLAddUser.fxml", "Add user",
event, true, ownerStage);
}
});
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
konfigurujMenu();
}
}
i kod klasy ZmianaSceny:
package taskmanager;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.scene.Node;
public class ZmianaSceny {
public static Object object;
public ZmianaSceny() {
}
public Stage changeView(String pathToView, String title) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(
pathToView));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root1));
stage.setTitle(title);
stage.show();
return stage;
} catch (Exception ex) {
System.out.println(ex.toString());
return null;
}
}
public Stage changeViewModalityWindow(String pathToView, String title,
ActionEvent event, boolean menuBar, Stage... args) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(
pathToView));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root1));
stage.setTitle(title);
stage.initModality(Modality.WINDOW_MODAL);
if (!menuBar) {
stage.initOwner(((Node) event.getSource()).getScene()
.getWindow());
} else {
stage.initOwner(args[0].getScene().getWindow());
}
stage.showAndWait();
return stage;
} catch (Exception ex) {
System.out.println(ex.toString());
return null;
}
}
}
Wydaje mi się że coś jest nie tak w ifelse, a dokładniej w tym fragmenciku,
if (!menuBar) {
stage.initOwner(((Node) event.getSource()).getScene()
.getWindow());
} else {
stage.initOwner(args[0].getScene().getWindow());
}
no ale mogę się mylić, ślęczę nad tym błędem od paru godzin i nie potrafię wydumać co jest nie tak.
Dzięki za podpowiedzi