Bom dia, Criei uma aplicação javafx, mas quando tento passar de scene para o outro da-me este erro java.lang.reflect.InvocationTargetException na chamada do fxml.
Já pesquisei bastante mas nada resolve o problema.
MainApp
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package gestorBiblioteca;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class MainApp extends Application {
public static ApplicationGPSD m_oApplication = new ApplicationGPSD();
@Override
public void start(Stage stage) throws IOException {
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("gestorBiblioteca/Menu.fxml"));
Scene scene = new Scene(root);
stage.setTitle("Gestor Biblioteca");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
MenuController
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package gestorBiblioteca;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
/**
* FXML Controller class
*
* @author filip
*/
public class MenuController implements Initializable {
@FXML
private Button verLivros;
@FXML
private Button addLivros;
private MainApp application;
/**
* Initializes the controller class.
* @param url
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// InicializarFicheiros initialize = new InicializarFicheiros();
// initialize.run();
}
public void setApp(MainApp application){
this.application = application;
}
public void gotoAddLivros(ActionEvent event) throws IOException {
Parent depositeView = FXMLLoader.load(getClass().getResource("gestorBiblioteca/AddLivros.fxml"));
Scene depositeViewScene = new Scene(depositeView);
Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
window.setScene(depositeViewScene);
window.show();
}
public void gotoVerLivros(ActionEvent event) throws IOException {
Parent depositeView = FXMLLoader.load(getClass().getResource("gestorBiblioteca/VerLivro.fxml"));
Scene depositeViewScene = new Scene(depositeView);
Stage window = (Stage) ((Node)event.getSource()).getScene().getWindow();
window.setScene(depositeViewScene);
window.show();
}
}
AddLivrosController
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package gestorBiblioteca;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXCheckBox;
import com.jfoenix.controls.JFXColorPicker;
import com.jfoenix.controls.JFXRadioButton;
import com.jfoenix.controls.JFXTextField;
import com.sun.javafx.css.converters.ColorConverter;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
/**
* FXML Controller class
*
* @author filip
*/
public class AddLivrosController implements Initializable {
private RegistarLivro m_lstLivros;
@FXML
private JFXTextField titulo;
@FXML
private JFXTextField autor;
@FXML
private JFXTextField ano;
@FXML
private Button voltar;
@FXML
private JFXButton addLivros;
@FXML
private Label areaText;
private Livros m_oLivro = MainApp.m_oApplication.getLivros();
private RegistarLivro m_oRegLivro = m_oLivro.getRegistoLivros();
@FXML
private JFXButton verLivros;
private MainApp application;
@FXML
private Button anima;
public void setApp(MainApp application){
this.application = application;
}
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
}
@FXML
public void gotoMenu(ActionEvent event) throws IOException{
Parent usertypes_page_parent = FXMLLoader.load(getClass().getResource("FXML.fxml"));
Scene usertypes_page_scene = new Scene(usertypes_page_parent);
Stage window2 = (Stage) ((Node)event.getSource()).getScene().getWindow();
window2.setScene(usertypes_page_scene);
window2.show();
}
@FXML
public void addLivro(ActionEvent Action)
{
if (ano.getText().isEmpty() || titulo.getText().isEmpty() || autor.getText().isEmpty()){
if (ano.getText().isEmpty()){
areaText.setText("Não se esqueça de preencher o ano");
areaText.setTextFill(Color.web("red"));
}
else if (titulo.getText().isEmpty()){
areaText.setText("Não se esqueça de colocar o titulo");
areaText.setTextFill(Color.web("red"));
}
else if (autor.getText().isEmpty()){
areaText.setText("Não se esqueça de preencher o autor");
areaText.setTextFill(Color.web("red"));
}
}
else {
String emp = "SIM";
Livros livro = m_oRegLivro.novoLivro(ano.getText(), titulo.getText(), autor.getText(), emp);
m_oRegLivro.registarLivro(livro);
areaText.setText("Livro adicionado com sucesso!");
areaText.setTextFill(Color.web("green"));
}
}
public void clearFields(){
ano.clear();
titulo.clear();
autor.clear();
}
public void verLivros(ActionEvent event) throws IOException{
Parent usertypes_page_parent = FXMLLoader.load(getClass().getResource("fxml/VerLivro.fxml"));
Scene usertypes_page_scene = new Scene(usertypes_page_parent);
Stage window2 = (Stage) ((Node)event.getSource()).getScene().getWindow();
window2.setScene(usertypes_page_scene);
window2.show();
}
}
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:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
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$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
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: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
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 gestorBiblioteca.MenuController.gotoAddLivros(MenuController.java:50)
... 58 more
Com “fonte” ele está pedindo o código-fonte da classe MenuController é comum em vez de falar toda vez “código-fonte” falar apenas fonte kk
Mas so de ver o StackTrace percebe-se que o erro está aqui: MenuController.gotoAddLivros
Traduzindo: location é exigido. Então falta passar o location. Por ele estar nulo, ele lança um NullPointerException.
Sobre este aqui, analisando o StackTrace, não ocorre no código que você escreveu. Tente resolver primeiramente o NullPointerException para ver se ja resolve seu problema.
Mas na verdade esse seria o correto, porque nunca que ele iria encontrar arquivo fxml na pasta gestorBiblioteca. Não tem nenhum arquivo fxml la kkk apenas na outra pasta.
Outra coisa que não sei se pode estar causando confusão no compilador é seu pacote ter o mesmo nome que seu projeto eu não faço isso, mas também não garanto que possa causar alguma confusão nele…