java.lang.NullPointerException bei javafx

Bsuted

Mitglied
Hallo!
Ich bin gerade dabei mit javafx ein Programm zu schreiben und habe dabei folgendes Problem;
Sobald ich das Programm starten will, kommt die Fehlermeldung

java.lang.NullPointerException
at application.Main.start(Main.java:22).



Hier der Code:
(Main.java)
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 {


@Override
public void start(Stage primaryStage) {
try {
// Read file fxml and draw interface.
FXMLLoader loader = new FXMLLoader(Main.class.getResource("MainWindow.fxml"));

Parent root = FXMLLoader.load(getClass()
.getResource("MainWindow.fxml"));

MainWindowController mainWindowController = loader.getController();
mainWindowController.setMain(this); Hier ist Z.22

primaryStage.setTitle("GMZ-Software");
primaryStage.setScene(new Scene(root));
primaryStage.show();

} catch(Exception e) {
e.printStackTrace();
}
}

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

}

(MainWindowController.java)

package application;

public class MainWindowController {

public Main main;

public void setMain(Main main) {
this.main = main;

}
}


Ich kann den Fehler leider nicht finden und würde mich über Hilfe sehr freuen!
Grüße Busted
 

phlinther1

Mitglied
Ich rate mal: loader.getController() gibt null zurück, weil du nicht loader.load() aufgerufen hast. Statt FxmlLoader.load() (statische Methode), verwende loader.load() direkt auf das loader object
 

Bsuted

Mitglied
Hi!
Ich habe den Code jetzt wie folgt geändert:

package application;

import java.io.IOException;

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

public class Main extends Application {

private Stage primaryStage;

@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
mainWindow();
}

public void mainWindow() {
try {
FXMLLoader loader = new FXMLLoader(Main.class.getResource("MainWindow.fxml"));
AnchorPane pane = (AnchorPane) loader.load();

primaryStage.setMinHeight(1080);
primaryStage.setMinWidth(1920);

MainWindowController mainWindowController = loader.getController();
mainWindowController.setMain(this);

Scene scene = new Scene(pane);

primaryStage.setScene(scene);
primaryStage.show();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


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

}
Leider funktioniert es immer noch nicht und der Fehler wird immer noch bei mainWindowController.setMain(this); angezeigt.
 

Bsuted

Mitglied
Vielen Dank! Das hatte ich vergessen. Allerdings konfrontiert das Programm mich jetzt schon mit dem nächsten Problem:
javafx.fxml.LoadException:
bei dieser Stelle AnchorPane pane = (AnchorPane) loader.load();

Falls es hilft hier nochmal der FXML code

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

<?import javafx.scene.Cursor?>
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.LineChart?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane fx:controller="MainWindowController" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1080.0" prefWidth="1920.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
    <AnchorPane layoutX="-2.0" maxHeight="-1.0" maxWidth="-1.0" prefHeight="1083.0" prefWidth="1931.0" style="-fx-background-color: #4a4a4a;">
         <children>
            <Label fx:id="field" alignment="CENTER_RIGHT" contentDisplay="RIGHT" layoutX="32.0" layoutY="805.0" prefHeight="52.0" prefWidth="296.0" style="-fx-background-color: grey;" text="0.00">
               <font>
                  <Font size="34.0" />
               </font>
               <cursor>
                  <Cursor fx:constant="DEFAULT" />
               </cursor>
            </Label>
            <ChoiceBox layoutX="239.0" layoutY="864.0" prefHeight="25.0" prefWidth="86.0" />
            <LineChart alternativeColumnFillVisible="true" focusTraversable="true" layoutX="717.0" layoutY="60.0" prefHeight="602.0" prefWidth="1200.0" style="-fx-background-color: white;">
              <xAxis>
                <CategoryAxis side="BOTTOM" />
              </xAxis>
              <yAxis>
                <NumberAxis side="LEFT" />
              </yAxis>
            </LineChart>
         </children>
      </AnchorPane>
    <MenuBar prefHeight="25.0" prefWidth="1927.0">
      <menus>
        <Menu mnemonicParsing="false" text="Menü">
          <items>
            <MenuItem mnemonicParsing="false" text="Aufzeichnung starten" />
            <MenuItem mnemonicParsing="false" text="Aufzeichung stoppen" />
            <Menu mnemonicParsing="false" text="Aufzeichung zurücksetzen" />
            <SeparatorMenuItem mnemonicParsing="false" />
            <MenuItem mnemonicParsing="false" text="Close" />
            <MenuItem mnemonicParsing="false" text="Save" />
            <MenuItem mnemonicParsing="false" text="Save As…" />
            <MenuItem mnemonicParsing="false" text="Revert" />
          </items>
        </Menu>
        <Menu mnemonicParsing="false" text="Graph">
          <items>
            <MenuItem mnemonicParsing="false" text="Lineare Ausgleichslinie (M x X + B)" />
            <MenuItem mnemonicParsing="false" text="Ausgleichslinie (" />
            <SeparatorMenuItem mnemonicParsing="false" />
            <MenuItem mnemonicParsing="false" text="Cut" />
            <MenuItem mnemonicParsing="false" text="Copy" />
            <MenuItem mnemonicParsing="false" text="Paste" />
            <MenuItem mnemonicParsing="false" text="Delete" />
            <SeparatorMenuItem mnemonicParsing="false" />
            <MenuItem mnemonicParsing="false" text="Select All" />
            <MenuItem mnemonicParsing="false" text="Unselect All" />
          </items>
        </Menu>
        <Menu mnemonicParsing="false" text="Hilfe">
          <items>
            <MenuItem mnemonicParsing="false" text="..." />
          </items>
        </Menu>
      </menus>
    </MenuBar>
   </children>
</AnchorPane>
 
Zuletzt bearbeitet von einem Moderator:

phlinther1

Mitglied
Kannst du den ganzen Stacktrace kopieren und posten? Eventuell passt deine Projektstruktur nicht und die FXML-Datei kann nicht geladen werden, weil sie nicht gefunden wird.
 

Bsuted

Mitglied
Code:
javafx.fxml.LoadException:
/E:/Programmieren/C-Hanken/Alpha/bin/application/MainWindow.fxml:16

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at application.Main.mainWindow(Main.java:25)
    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)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: MainWindowController
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920)
    ... 18 more

Zeile 19 steht mainWindow();
 
Zuletzt bearbeitet von einem Moderator:

mrBrown

Super-Moderator
Mitarbeiter
Im der FXML-Datei, aktuell hast du dort nur den Klassennamen angegeben <AnchorPane fx:controller="MainWindowController", dort muss aber der Klassenname inklusive packages hin.
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
F Fehlermeldung java.lang.NullPointerException Java Basics - Anfänger-Themen 4
D java.lang.NullPointerException Java Basics - Anfänger-Themen 19
X java.lang.NullPointerException fehler ? Java Basics - Anfänger-Themen 1
R Exception in thread "main" java.lang.NullPointerException Java Basics - Anfänger-Themen 10
H java.lang.NullPointerException Java Basics - Anfänger-Themen 4
G java.lang.NullPointerException Java Basics - Anfänger-Themen 3
E Compiler-Fehler java.lang.NullPointerException Java Basics - Anfänger-Themen 2
J java.lang.NullPointerException in meiner JavaFXControllerKlasse Java Basics - Anfänger-Themen 26
D Communications link failure | java.lang.NullPointerException Java Basics - Anfänger-Themen 3
F java.lang.NullPointerException, kann aber nicht sein! Java Basics - Anfänger-Themen 4
M Compiler-Fehler Java suckt - java.lang.NullPointerException Java Basics - Anfänger-Themen 12
L Compiler-Fehler Exception in thread "main" java.lang.NullPointerException Java Basics - Anfänger-Themen 2
S java.lang.NullPointerException Java Basics - Anfänger-Themen 4
G Compiler-Fehler java.lang.NullPointerException Java Basics - Anfänger-Themen 4
A Compiler-Fehler Exception in thread "main" java.lang.NullPointerException Java Basics - Anfänger-Themen 7
P Interpreter-Fehler java.lang.NullPointerException Java Basics - Anfänger-Themen 3
T Problem mit Eclipse? Exception in thread "main" java.lang.NullPointerException Java Basics - Anfänger-Themen 4
S Fehler: "java.lang.NullPointerException" Java Basics - Anfänger-Themen 6
I Erste Schritte Ausführfehler: java.lang.NullPointerException Java Basics - Anfänger-Themen 3
1 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException Java Basics - Anfänger-Themen 5
S Exception in thread "main" java.lang.NullPointerException Java Basics - Anfänger-Themen 11
V [Greenfoot] java.lang.NullPointerException Java Basics - Anfänger-Themen 12
S Compiler-Fehler java.lang.NullPointerException Java Basics - Anfänger-Themen 10
J java.lang.NullPointerException Java Basics - Anfänger-Themen 18
K Interpreter-Fehler java.lang.NullPointerException Java Basics - Anfänger-Themen 6
K OOP java.lang.NullPointerException Java Basics - Anfänger-Themen 2
B Exception in thread "main" java.lang.NullPointerException Fehler Hilfe! Java Basics - Anfänger-Themen 4
S Fehlermeldung: java.lang.NullPointerException Java Basics - Anfänger-Themen 10
A Exception in thread "main" java.lang.NullPointerException Java Basics - Anfänger-Themen 16
K java.lang.NullPointerException in Array bei Arraylistübergabe Java Basics - Anfänger-Themen 7
G java.lang.NullPointerException Was tun? Java Basics - Anfänger-Themen 7
J java.lang.NullPointerException . Java Basics - Anfänger-Themen 5
G java.lang.NullPointerException Java Basics - Anfänger-Themen 2
S java.lang.NullPointerException - Bitte helft mir! Java Basics - Anfänger-Themen 5
S java.lang.NullPointerException Fehler bei Umrechnung Java Basics - Anfänger-Themen 3
P java.lang.NullPointerException Java Basics - Anfänger-Themen 19
M java.lang.NullPointerException at . Java Basics - Anfänger-Themen 4
S java.lang.nullpointerexception bei arrayübergabe Java Basics - Anfänger-Themen 30
S java.lang.NullPointerException bei Bildern Java Basics - Anfänger-Themen 13
P java.lang.NullPointerException was ist das? Java Basics - Anfänger-Themen 7
G java.lang.NullPointerException beim objekt Java Basics - Anfänger-Themen 3
F TableModelListener: java.lang.ArrayIndexOutOfBoundsException: 132 Java Basics - Anfänger-Themen 3
F Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11 at main.main(main.java:11) Java Basics - Anfänger-Themen 2
O Exception in thread "main" java.lang.ArithmeticException: / by zero Java Basics - Anfänger-Themen 4
JaVaN0oB java.lang.ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 18
R Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 5
S Compiler-Fehler Exception in thread "main" java.lang.Error: Unresolved compilation problem: Java Basics - Anfänger-Themen 6
H JUnit in Eclipse: java.lang.NoClassDefFoundError: Java Basics - Anfänger-Themen 9
I Compiler-Fehler Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 Java Basics - Anfänger-Themen 3
B java.lang.ArithmeticException: / by zero Java Basics - Anfänger-Themen 3
P java.lang.ClassCastException Bedeutung und Lösung Java Basics - Anfänger-Themen 3
J Erste Schritte java.lang.NoClassDefFoundError Java Basics - Anfänger-Themen 4
I java.lang.ArrayIndexOutOfBoundsException at lösung.main Java Basics - Anfänger-Themen 3
T Compiler-Fehler java.lang.ArithmeticException: / by zero Java Basics - Anfänger-Themen 2
R java.lang.ArrayIndexOutOfBoundsException: 0 Rechner Error Java Basics - Anfänger-Themen 4
D Erste Schritte Java.lang.NullPointer.Exception Java Basics - Anfänger-Themen 8
C Compiler-Fehler Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 Java Basics - Anfänger-Themen 3
N jodaTime java.lang.IllegalArgumentException: Invalid format Java Basics - Anfänger-Themen 3
F Erste Schritte java.lang.StringIndexOutOfBoundsException Java Basics - Anfänger-Themen 3
R java.lang.StringIndexOutOfBoundsException Java Basics - Anfänger-Themen 1
V java.lang.ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 2
H java.lang.NoClassDefFoundError Run as>> Run on Server Java Basics - Anfänger-Themen 2
L Fehler: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 4
S Java memory fehler: Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap spa Java Basics - Anfänger-Themen 5
M java.lang.Enum.valueOf(Unknown Source) Java Basics - Anfänger-Themen 2
L Compiler-Fehler Problem beim Programmieren eines Kalenders (java.lang.ArrayIndexOutOfBoundsException) Java Basics - Anfänger-Themen 2
A Code läuft nicht, Fehlermeldung Exception in thread "main" java.lang.Error: Unresolved compilation " Java Basics - Anfänger-Themen 11
P Exception in thread "main" java.lang.NoClassDefFoundError: Java Basics - Anfänger-Themen 1
C Hilfe!!! java.lang.ClassCastException Java Basics - Anfänger-Themen 1
O java.lang.IndexOutOfBoundsException JTable autoSort Java Basics - Anfänger-Themen 5
W Methoden Rückgabedatentyp java.util.Map<java.lang.String,? extends ...> Java Basics - Anfänger-Themen 4
F Exception in thread main java.lang.StackOverflowError Java Basics - Anfänger-Themen 3
K Caused by: java.lang.NoClassDefFoundError: org/mindrot/jbcrypt/BCrypt Java Basics - Anfänger-Themen 0
OnDemand java.lang.reflect.InvocationTargetException Java Basics - Anfänger-Themen 9
J Klassen java.lang.String - lexikographisches Sortieren Java Basics - Anfänger-Themen 5
T Erste Schritte import java.lang.System.out Java Basics - Anfänger-Themen 4
H Classpath java.lang.NoClassDefFoundError externe Libary Java Basics - Anfänger-Themen 4
M Exception in thread "main" java.lang.NoClassDefFoundError: MeineKlasse Java Basics - Anfänger-Themen 12
X Applet Fehler: java.lang.reflect.InvocationTargetException Java Basics - Anfänger-Themen 45
B java.lang.ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 4
K Date cannot be cast to java.lang.Integer Java Basics - Anfänger-Themen 4
H java.lang.IndexOutOfBoundsException bei verschachtelter ArrayList Java Basics - Anfänger-Themen 9
V java.lang.NoSuchMethodError Java Basics - Anfänger-Themen 2
J java.lang.math asin() Java Basics - Anfänger-Themen 18
R Compiler-Fehler java.lang.ArrayIndexOutOfBoundsException, warum? Java Basics - Anfänger-Themen 6
K Error: java.lang.NoSuchMethodException Java Basics - Anfänger-Themen 2
M Compiler-Fehler Fehler Meldung java.lang.NumberFormatException: empty String Java Basics - Anfänger-Themen 2
G java.lang.ClassNotFoundException Java Basics - Anfänger-Themen 4
S String index out of range: 8 at java.lang.String.substring(Unknown Source) Java Basics - Anfänger-Themen 13
X Interpreter-Fehler "java.lang.NullPionterException: null" bei BlueJ Java Basics - Anfänger-Themen 10
P java.lang.ClassCastException Java Basics - Anfänger-Themen 2
D java.lang.NoSuchMethodError: main Java Basics - Anfänger-Themen 11
S expected java.lang.string but found char Java Basics - Anfänger-Themen 5
S Umgebungsvariable Exception in thread "main" java.lang.UnsatisfiedLinkError: no J3D in java.librar y.path Java Basics - Anfänger-Themen 15
S Compiler-Fehler java.lang.SecurityException: Invalid signature file digest for Manifest main attributes Java Basics - Anfänger-Themen 5
M Klassen Exception in thread "main" java.lang.NoClassDefFoundError: Java Basics - Anfänger-Themen 2
A Compiler-Fehler unreported exception java.lang.Exception; must be caught or declared to be thrown Java Basics - Anfänger-Themen 7
K Fehlermeldung: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 Java Basics - Anfänger-Themen 4
M Datentypen java.util.Arrays$ArrayList cannot be cast to [Ljava.lang.String; Java Basics - Anfänger-Themen 11
F Ich kenn mich nicht mehr aus - 'java.lang.NoSuchMethodError: main' in Applet? Java Basics - Anfänger-Themen 2

Ähnliche Java Themen

Neue Themen


Oben