0

Heyho,

i have a very strange load exception after starting my javafx project. The loadexception just appears when i provoke another error, otherwise everything seems to work fine. So the nullpointerexception which i also do not understand and might correlate with the load exception is the reason why i even see this loadexception. I wrote a getController() Method in my Main class and in start() i wanted to get the controller by loader.getController(), which seems to return null. In my @FXML initialize() i instantiate the model graph, which calls my getController() Method in the constructor and when graph trie to use this constructor i certainly get a nullpointerexception.. Does anybody know the problem?

Here is the Stack trace

`javafx.fxml.LoadException: 
/C:/Users/bertr/eclipse-workspace/Hashiwokakero/bin/application/view/Layout.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2571)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at application.Main.start(Main.java:30)
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(Unknown Source)
Caused by: 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 sun.reflect.misc.Trampoline.invoke(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
    ... 12 more
Caused by: java.lang.NullPointerException
    at application.model.Graph.<init>(Graph.java:88)
    at application.view.Controller.initialize(Controller.java:68)
    ... 22 more
`

And the start()- Method

public class Main extends Application {
    public static Controller controller;

    @Override
    public void start(Stage primaryStage) {
        try {

            //anLeitungLaden();
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("view/Layout.fxml"));
            Pane pane = (Pane) loader.load();
            controller = loader.getController();
            Scene scene = new Scene(pane);

            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setTitle("Hashiwokakero");
            primaryStage.setScene(scene);
            //primaryStage.getIcons().add(new Image("resources/logo2.png"));
            primaryStage.show();


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

The beginning of Controller class

package application.view;

import java.io.File;
import java.util.Arrays;

import application.model.Vertex;
import application.Main;
import application.model.Edge;
import application.model.Graph;
import application.model.HashiGenerator;
import application.model.HashiGenerator2;
import javafx.animation.Animation;
import javafx.animation.AnimationTimer;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.SequentialTransition;
import javafx.animation.Timeline;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.geometry.Bounds;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.effect.Glow;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.scene.shape.Shape;
import javafx.util.Duration;


public class Controller {


    @FXML public Pane root;
    @FXML public Pane pane;
    @FXML public CheckBox checkboxMusic;
    @FXML public CheckBox checkboxSoundeffect;
    @FXML public Button newLevelButton;
    public MediaPlayer mediaPlayer;
    private MediaPlayer soundeffectPlayer;
    private boolean soundEffect;
    public int numberOfEmptyContainer;
    public Graph level;
    public DragStartHandler startHandler;


    @FXML private void initialize() {


        String musicFile = "src/resources/star-wars-cantina-song.mp3";  

        Media sound = new Media(new File(musicFile).toURI().toString());
        mediaPlayer = new MediaPlayer(sound);

        mediaPlayer.play();
        numberOfEmptyContainer=0;
        checkboxMusic.setSelected(true);
        checkboxSoundeffect.setSelected(true);
        soundEffect = true;
        newLevelButton.setVisible(false);
        newLevelButton.setDisable(true);
        startHandler = new DragStartHandler();

       level = new Graph(pane);

and the Layout.fxml

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

<?import javafx.scene.effect.*?>
<?import javafx.scene.paint.*?>
<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.text.*?>

<Pane id="root" fx:id="root" prefHeight="839.0" prefWidth="1281.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"  fx:controller="application.view.Controller">
   <children>
      <Pane id="pane" fx:id="pane" layoutX="21.0" layoutY="64.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="749.0" prefWidth="1209.0" style="-fx-border-radius: 100; -fx-border-width: 100;">
         <children>
            <Button id="newLevelButton" fx:id="newLevelButton" layoutX="363.0" layoutY="342.0" mnemonicParsing="false" prefHeight="66.0" prefWidth="484.0" style="-fx-border-color: #ffd308; -fx-border-radius: 30;" text="Weiter zu Level 2" textFill="#ffd308">
               <font>
                  <Font name="System Italic" size="40.0" />
               </font></Button>
         </children></Pane>
      <CheckBox id="checkboxSoundeffect" fx:id="checkboxSoundeffect" layoutX="1032.0" layoutY="23.0" mnemonicParsing="false" onAction="#handleCheckboxSoundEffect" text="Soundeffekte" textFill="WHITE" />
      <CheckBox id="checkboxMusic" fx:id="checkboxMusic" layoutX="1185.0" layoutY="23.0" mnemonicParsing="false" onAction="#handleCheckboxMusic" text="Musik" textFill="WHITE" />
   </children>
</Pane>

Edit: This is my class Graph

package application.model;

import java.io.File;
import java.util.Arrays;

import application.Main;
import application.view.Controller;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.Node;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

    public class Graph {


        public int numberOfContainer = 8;
        public int[][] adjazenzMatrix;
        public int[] nodelist;
        public boolean[] nodelistvisited;

        private Controller controller;
        private Pane pane = new Pane();



            public Edge ContainerLine ;
            public Vertex startContainer;


        // 3 Zeilen/ 4 Spalten
        // TODO: Positionen überlegen
        public Vertex createContainer(double x, double y, double radius, int anzahl) {
            return new Vertex(x, y, radius,anzahl);
        }

    public Graph(Pane pane){
        controller= Main.getController();
        pane = pane;
        nodelist = new int[numberOfContainer];
        adjazenzMatrix = new int[numberOfContainer][numberOfContainer];
        for (int i = 0; i < numberOfContainer; ++i ) {
             Arrays.fill(adjazenzMatrix[i],0);
             nodelist[i] = i;
         }
        nodelistvisited = new boolean[numberOfContainer] ;
            Arrays.fill(nodelistvisited,false);

    Vertex[][] containerGrid = new Vertex[][]{
            {createContainer(pane.getPrefWidth()-(7*pane.getPrefWidth())/8, (double)pane.getPrefHeight()-(5*pane.getPrefHeight())/6,30,4), null ,createContainer(pane.getPrefWidth()-(7*pane.getPrefWidth())/8, pane.getPrefHeight()-(1*pane.getPrefHeight())/6,30,3)},
            {createContainer(pane.getPrefWidth()-(5*pane.getPrefWidth())/8, (double)pane.getPrefHeight()-(5*pane.getPrefHeight())/6,30,5), createContainer(pane.getPrefWidth()-(5*pane.getPrefWidth())/8, (double)pane.getPrefHeight()-(3*pane.getPrefHeight())/6,30,3),null},
            {null, createContainer(pane.getPrefWidth()-(3*pane.getPrefWidth())/8, (double)pane.getPrefHeight()-(3*pane.getPrefHeight())/6,30,2),createContainer(pane.getPrefWidth()-(3*pane.getPrefWidth())/8, (double)pane.getPrefHeight()-(1*pane.getPrefHeight())/6,30,1)},
            {createContainer(pane.getPrefWidth()-(1*pane.getPrefWidth())/8, (double)pane.getPrefHeight()-(5*pane.getPrefHeight())/6,30,1), createContainer(pane.getPrefWidth()-(1*pane.getPrefWidth())/8, (double)pane.getPrefHeight()-(3*pane.getPrefHeight())/6,30,1),null}

        };
      //Zeilen X Richtung
     for (int i = 0; i < 4; ++i) {
         //Spalten Y Richtung
         for ( int j = 0; j<3; ++j) {
             if(containerGrid[i][j] != null) {
             Vertex c = containerGrid[i][j];
             c.setOnDragDetected(controller.startHandler);
             c.setOnMouseDragReleased(controller.dragReleaseHandler);
             c.setOnMouseDragEntered(controller.dragEnteredHandler);
             c.setUserData(Boolean.TRUE);
             c.getChildren().addAll(c.circle, c.label,c.label2);
             pane.getChildren().add(c);

             }
         }
     }
    } // KostruktorLevel


    public void deepSearch(int[][] matrix,int[] nodelist ,boolean[] nodelistvisited  ,int current,int suche){
            nodelistvisited[current]=true; // Knoten als besucht kennzeichnen
            for (int i=0;i<nodelist.length;i++)
            {
                if(matrix[current][i] == 1&& !nodelistvisited[i]) deepSearch(matrix,nodelist,nodelistvisited,i,suche);
            }

        }   



    }    
meggi
  • 25
  • 12
  • 1
    The exception happens: `at application.model.Graph.(Graph.java:88)` - what is this line? Please include the code for `Graph`. – Itai Dec 26 '17 at 09:36
  • You did not show the part that causes the exception as sillyfly noted but if you're trying to retrieve the `Controller` instance from the static field in `Main`: `FXMLLoader` calls the controller's `initialize` method before returning from the `load()` method. This means the field is not initialized at that time. But why not add the controller as constructor parameter of the `Graph` class and avoid the problem overall also removing the necessity of using a static field to pass data (which is bad practice): `level = new Graph(pane, this);` – fabian Dec 26 '17 at 11:10
  • Thank you, Fabian! i didnt think about that way to pass the controller ^^ That works fine, and solved the null pointer exception. – meggi Dec 26 '17 at 11:22
  • But the invocationtargetexception is still there, do you know why? It works and the exception instantly disappears, but why does it even occur? – meggi Dec 26 '17 at 11:25

0 Answers0