JavaFX, nie mozna uruchomić kontrolera.

0

Przepraszam, w głowie mi się już kręci od tego pisania kodu.

To jest moja klasa Main:

package application;

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

public class Main extends Application
{

public static void main(String[] args)
{
    launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception
{
    Parent parent = (Parent) FXMLLoader.load(getClass().getResource("fxmvc2\\MainPane2.fxm"));
    Scene scene = new Scene(parent);
    Stage stage = new Stage();
    stage.setScene(scene);
    stage.setTitle("Login Page");
    stage.show();

}

}

Poniżej klasa kontrolera:

package controller;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;

public class LoginController implements Initializable
{

@FXML
private AnchorPane loginAnchorPane;

@FXML
private Label userNameLabel;

@FXML
private TextField userNameTextField;

@FXML
private Button loginButton;

@Override
public void initialize(URL location, ResourceBundle resources)
{
    System.out.println(userNameLabel.getText());
    userNameTextField.setText("Wpisz swoje imię");

}

}

Teraz plik fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<anchorpane fx:id="loginAnchorPane" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" maxheight="-Infinity" maxwidth="-Infinity" minheight="-Infinity" minwidth="-Infinity" prefheight="629.0" prefwidth="969.0"> <children> <label fx:id="userNameLabel" text="Nazwa użytkownika" layoutx="457.0" layouty="235.0" /> <textfield fx:id="userNameTextField" layoutx="435.0" layouty="302.0" /> <button fx:id="loginButton" text="Zaloguj" layoutx="472.0" layouty="369.0" mnemonicparsing="false" /> </children> </anchorpane> 

Teraz, przy odpaleniu programu, mam taki błąd:

Exception in Application start method
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 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(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
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$launchApplication$154(LauncherImpl.java:182)
	at java.lang.Thread.run(Unknown Source)
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 application.Main.start(Main.java:19)
	at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
	at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
	at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
	at java.security.AccessController.doPrivileged(Native Method)
	at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
	... 1 more
Exception running application application.Main

Może dla kogoś z Was to będzie oczywiste, ale mi się już plącze przed oczami i nie dam rady dojść, co jet do czego.

Pozdrawiam.

0
kviatek napisał(a):

Przepraszam, w głowie mi się już kręci od tego pisania kodu.

To jest moja klasa Main:

package application;

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

public class Main extends Application
{

public static void main(String[] args)
{
    launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception
{
    Parent parent = (Parent) FXMLLoader.load(getClass().getResource("fxmvc2\\MainPane2.fxm"));
    Scene scene = new Scene(parent);
    Stage stage = new Stage();
    stage.setScene(scene);
    stage.setTitle("Login Page");
    stage.show();

}

}

Poniżej klasa kontrolera:

package controller;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;

public class LoginController implements Initializable
{

@FXML
private AnchorPane loginAnchorPane;

@FXML
private Label userNameLabel;

@FXML
private TextField userNameTextField;

@FXML
private Button loginButton;

@Override
public void initialize(URL location, ResourceBundle resources)
{
    System.out.println(userNameLabel.getText());
    userNameTextField.setText("Wpisz swoje imię");

}

}

Teraz plik fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<anchorpane fx:id="loginAnchorPane" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" maxheight="-Infinity" maxwidth="-Infinity" minheight="-Infinity" minwidth="-Infinity" prefheight="629.0" prefwidth="969.0"> <children> <label fx:id="userNameLabel" text="Nazwa użytkownika" layoutx="457.0" layouty="235.0" /> <textfield fx:id="userNameTextField" layoutx="435.0" layouty="302.0" /> <button fx:id="loginButton" text="Zaloguj" layoutx="472.0" layouty="369.0" mnemonicparsing="false" /> </children> </anchorpane> 

Teraz, przy odpaleniu programu, mam taki błąd:

Exception in Application start method
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 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(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
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$launchApplication$154(LauncherImpl.java:182)
	at java.lang.Thread.run(Unknown Source)
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 application.Main.start(Main.java:19)
	at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
	at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
	at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
	at java.security.AccessController.doPrivileged(Native Method)
	at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
	... 1 more
Exception running application application.Main

Może dla kogoś z Was to będzie oczywiste, ale mi się już plącze przed oczami i nie dam rady dojść, co jet do czego.

Pozdrawiam.

Ktoś może pomóc, czy nie wiecie, co z tym zrobić?

0
Parent parent = (Parent) FXMLLoader.load(getClass().getResource("fxmvc2\\MainPane2.fxm"));

Masz ścieżkę pokopaną. Rozszerzenie nie powinno być .fxml?

0

A jakie rozszerzenie ma nazwa pliku FXML'owego, bo ładujesz *.fxm (MainPane2.fxm) ?

1

Przede wszystkim masz literówki w pliku fxml, zamiast <anchorpane> powinno być <AnchorPane>, literówki są też w nazwach właściwości np. masz maxheight zamiast maxHeight itp. Plik fxml się nie ładuje z powodu literówek więc ci wyskakuje NullPointerException przy wywoływaniu metody FXMLLoader.load(). Włącz sobie jakieś ide i przejrzyj te pliki albo użyj JavaFX Scene Buildera, albo zbuduj sobie to okienko w Javie bez fxml.

0

Dzięki, sprawdzę i dam znać, czy działa.

1 użytkowników online, w tym zalogowanych: 0, gości: 1