0

This code worked prior to the Java update, thus I'm assuming the problem lies there. I'm referring to the java update what was release on Jan 18th (Java 8 Update 321).

I've tried giving the full path of the .fxml file in .getResource(".."), but that also didn't help.

I've also tried finding the previous version of Java (prior to Java 8 update 321) and trying to install it instead, but I couldn't find it anywhere.

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

public class Conway extends Application {
    
    @Override
    public void start(Stage stage) throws Exception{
        
        Parent root = FXMLLoader.load(getClass().getResource("Conway.fxml"));
        Scene scene = new Scene(root);
        stage.setTitle("John Conway");
        stage.setScene(scene);
        stage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }

}

this part is probably of no importance, but i'll add it just in case:

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

public class ConwayController {

    @FXML
    private Canvas canvas;
    
    private boolean start = true;
    private static int[][] currentColor = new int[10][10];
    private static int[][] nextColor = new int[10][10];
    
    /* adds up living neighbors in the array, and returns livingNeighbors */
    public static int checkValidNeighbors(int i, int j) {
        int livingNeighbors = 0;
        
        /* gets data from neighbors */
        for(int k = i - 1; k < i+2; k++) {
            if(k >= 0 && k <=9) {
                for(int l = j - 1; l < j + 2; l++) {
                    if(l >= 0 && l <= 9) {
                        /* not to count himself as a living neighbor */
                        if((currentColor[k][l] == 1) && !((k == i) && (l == j)))
                            livingNeighbors += 1;       
                    }
                }
            }
        }
        
        return livingNeighbors;
    }
                                                        
    /* decides by the state of the next color by it's current color and the neighbors color */
    public static void updateNextColor() {
        int livingNeighbors = 0;
        // calculate next colors 
        for(int i = 0; i < 10; i++) {
            livingNeighbors = 0;
            for(int j = 0; j < 10; j++) {
                livingNeighbors = checkValidNeighbors(i, j);
                
                // if dead and surrounded by 3 neighbors -> live next generation
                if((currentColor[i][j] == 0) && (livingNeighbors == 3))
                    nextColor[i][j] = 1;
                
                // if alive 
                if(currentColor[i][j] == 1) {
                    // if too little or too many neighbors -> death next generation 
                    if((livingNeighbors <= 1) || (livingNeighbors >= 4))
                        nextColor[i][j] = 0;
                    // if 2 or 3 neighbors -> live next generation 
                    if(livingNeighbors == 3 || livingNeighbors == 2)
                        nextColor[i][j] = 1;
                }
            }
        }    
        
        /* puts the next colors into correntColor for the next coloring */
        for(int i = 0; i < 10; i++) {
            for(int j = 0; j < 10; j++) {
                currentColor[i][j] = nextColor[i][j];
            }
        }
        
    }
    
    @FXML
    void NextGenerationButtonPressed(ActionEvent event) {
   
        GraphicsContext gc = canvas.getGraphicsContext2D();
        
        gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
        
        int livingNeighbors = 0, k = 0, l = 0;
        
        /* if it's the first button push, initialize currentColor to random values */ 
        if(start == true) {
            start = false;
            /* initializes currentColor to random 1s and 0s */
            for(int i = 0; i < 10; i++) {
                int j = 0;
                for(j = 0; j < 10; j++) 
                    currentColor[i][j] = (int)Math.round(Math.random());
            }
            /* draws the rectangles according to currentColor data */
            for(int i = 0; i < canvas.getWidth(); i += canvas.getWidth()/10, k++) {
                l = 0;
                for(int j = 0; j < canvas.getHeight(); j += canvas.getWidth()/10, l++) {
                                        
                    if(currentColor[k][l] == 1) {
                        gc.setFill(Color.BLACK);    
                    } else {
                        gc.setFill(Color.WHITE);
                    }
                    
                    gc.fillRect(i, j, canvas.getWidth()/10, canvas.getWidth()/10);
                    gc.strokeRect(i, j, canvas.getWidth()/10, canvas.getWidth()/10);
                }
            }
            
            updateNextColor();      
        }
        /* if the currentColor is already initialized */ 
        else {
            
            for(int i = 0; i < canvas.getWidth(); i += canvas.getWidth()/10, k++) {
                l = 0;
                for(int j = 0; j < canvas.getHeight(); j += canvas.getWidth()/10, l++) {
                                        
                    if(currentColor[k][l] == 1) {
                        gc.setFill(Color.BLACK);    
                    } else {
                        gc.setFill(Color.WHITE);
                    }
                    
                    gc.fillRect(i, j, canvas.getWidth()/10, canvas.getWidth()/10);
                    gc.strokeRect(i, j, canvas.getWidth()/10, canvas.getWidth()/10);
                }
            }
            
            updateNextColor();      
        }
    }

}

The error:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3324)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3287)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3255)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3227)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3203)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3196)
    at Conway.start(Conway.java:12)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    ... 1 more
Exception running application Conway
  • Java 8 and JavaFX 8 are both 9 major versions old, I would recommend upgrading to a more modern versions of each e.g. 17. – jewelsea Jan 24 '22 at 08:37
  • 1
    Your issue is the fxml resource lookup failed. Follow the troubleshooting steps at [How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?](https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other) if you don’t solve it, edit the question to include the results of your troubleshooting. Also see the [eden resource guide](https://edencoding.com/where-to-put-resource-files-in-javafx/). – jewelsea Jan 24 '22 at 08:38
  • Oracle provide [historical versions of Java SE](https://www.oracle.com/java/technologies/downloads/archive/) for testing, the Java SE 8 versions will include a JavaFX 8 implementation. – jewelsea Jan 24 '22 at 09:00
  • I doubt that your problem is related to the Java version. Maybe this old question on SO helps: https://stackoverflow.com/questions/55396944/how-to-repair-caused-by-java-lang-nullpointerexception-location-is-required-j – mipa Jan 24 '22 at 11:36

0 Answers0