Dúvidas sobre primeiro projeto em JavaFX

5 respostas Resolvido
programaçãojava
B

Estou começando os estudos em JavaFX utilizando a ide Netbeans + SceneBuilder em conjunto e logo no primeiro projeto estou enfrentando um erro que não consigo decifrar.

Criei um “Novo Projeto >>> Aplicação FXML” e utilizei o SceneBuilder apenas para dar nomes (id’s) aos dois componentes contidos na tela, um botão e um label.

Porem ao executar a aplicação tem sido exibido o seguinte erro:

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$launchApplication6(LauncherImpl.java:182)

at java.lang.Thread.run(Thread.java:745)

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 teste.Teste.start(Teste.java:22)

at com.sun.javafx.application.LauncherImpl.lambda$launchApplication13(LauncherImpl.java:863)

at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(PlatformImpl.java:326)

at com.sun.javafx.application.PlatformImpl.lambda$null4(PlatformImpl.java:295)

at java.security.AccessController.doPrivileged(Native Method)

at com.sun.javafx.application.PlatformImpl.lambda$runLater5(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$null9(WinApplication.java:191)

 1 more

Exception running application teste.Teste

C:\Users\ALLINONE\Documents\NetBeansProjects\Teste\nbproject\build-impl.xml:1051: The following error occurred    while executing this line:

C:\Users\ALLINONE\Documents\NetBeansProjects\Teste\nbproject\build-impl.xml:805: Java returned: 1

FALHA NA CONSTRUÇÃO (tempo total: 7 segundos)

O meu projeto tem apenas a classe Principal chamada Teste com o seguinte código:

package teste;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author ALLINONE
 */
public class Teste extends Application {
     
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
         
      //  Scene scene = new Scene(root);
         
        stage.setScene(new Scene(root));
        stage.show();
    }
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
     
}

E a classe Control:

package teste;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;


 public class FXMLDocumentController implements Initializable {
 
@FXML
private Button btClick;
@FXML
private Label lbTexto;
 
@FXML
private void handleButtonAction(ActionEvent event) {
    System.out.println("You clicked me!");
    lbTexto.setText("Hello World!");
}
 
@Override
public void initialize(URL url, ResourceBundle rb) {
    
     
}    

    @FXML
    private void handleButtonAction(MouseEvent event) {
         
       System.out.println("You clicked me!");
       lbTexto.setText("Hello World!"); 
    }
     
}

5 Respostas

T

Provavelmente você esta passando o nome errado no FXMLLoader.load(getClass().getResource(“FXMLDocument.fxml”))

O arquivo de Layout esta dentro de um pacote? Se estiver, vai ter que passar o nome do pacote

Tenta essas opções:

Opção 1: Parent root = FXMLLoader.load(getClass().getResource("/FXMLDocument.fxml"));

Opção 2: Parent root = FXMLLoader.load(getClass().getResource("/nomeDoPacote/FXMLDocument.fxml"));

Opção 3: Parent root = FXMLLoader.load(getClass().getClassLoader().getResource(“FXMLDocument.fxml”));

B

Continua não dando certo. Não sei se tem alguma coisa que poderia influenciar também em termos de configuração, pois nunca havia mexido com JavaFx, mas eu fiz uma alteração na seguinte configuração do NetBeans, fui em “Ferramentas >>> Opções >>> Java >>> JavaFX” e apontei o Home Page do Scene Builder para a minha area de trabalho do windows, não sei se isso poderia estar influenciando ou impedindo que o projeto rode…

Está dando essa mensagem antes da Exception que eu mandei anteriormente.

`

ant -f C:\\Users\\ALLINONE\\Documents\\NetBeansProjects\\Teste -Djavac.includes=teste/Teste.java -     Dnb.internal.action.name=run.single -Drun.class=teste.Teste run-single

init:

Deleting: C:\Users\ALLINONE\Documents\NetBeansProjects\Teste\build\built-jar.properties

deps-jar:

Updating property file: C:\Users\ALLINONE\Documents\NetBeansProjects\Teste\build\built-jar.properties
 Compiling 1 source file to C:\Users\ALLINONE\Documents\NetBeansProjects\Teste\build\classes
 compile-single:
 run-single:

`

T

Tenta assim:

Arquivo -> Novo projeto -> Maven -> Aplicação JavaFX

B

Agora está exibindo outro erro…

BUILD FAILURE

Total time: 10.761s
Finished at: Mon Sep 12 11:53:54 BRT 2016
Final Memory: 18M/103M

Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli) on project Teste_JavaFX: Command execution failed. Process exited with an error: 1 (Exit value: 1) -> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Estou pesquisando aqui e parece ser algum erro de plugin… mas até o momento nada que faça rodar um projeto simples em JavaFx no meu Netbeans, to quase desinstalando e instalando ele denovo…

B
Solucao aceita

Problema resolvido, após desinstalar e instalar o Netbeans, estranhamente consegui criar um projeto e faze-lo funcionar.

Criado 10 de setembro de 2016
Ultima resposta 15 de set. de 2016
Respostas 5
Participantes 2