0

So, as of right now, I'm currently in a Java Programming class that uses either Netbeans or Eclipse as their main methods of choice. I already chose Eclipse as my choice for coding, and most of the assignments have gone well up until now. As of late, I have tried to code a tictactoe board with the ability to randomize the locations of blank spaces, Xs and Os, and while I have successfully written the code, there is one issue I have come across. Below is the code that I have made for this assignment, after that, I'll give out the main issue that I have been facing.

import java.util.Random;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class TicTacToeBoard extends Application {
    
    // Variables for Setup
    Image X, O; // Shows X and O
    ImageView cells[]; // Preparation for a 3 by 3 Grid
    Random randomizer; // Makes the generation of cells far more random
    int cellLength, cellHeight = 130; // Dimensions for the cells
    
    // Set the Stage, a stage is a world of entertainment.
    @Override
    public void start(Stage primaryStage) { 
        // Image Setup
        // Note: Your pathway will be different compared to my own.
        X = new Image("file:C:\\Users\\...\\x.gif");
        O = new Image("file:C:\\...\\0.gif"); // The ... is just a placeholder for my file path
        cells = new ImageView[9]; // 3 by 3 Setup
        randomizer = new Random(); // Make the Setup Random
        
        // Commence cell construction
        for(int t = 0; t < cells.length; t++) {
            // The height and width
            cells[t].setFitWidth(cellLength);
            cells[t].setFitHeight(cellHeight);
            
            // Randomize the position. X is 1, O is 2, blank is 0.
             int r = randomizer.nextInt(3);
             if(r == 1) {
                 cells[t].setImage(X);
             }
             else if (r == 2) {
                 cells[t].setImage(O);
             }
        }
        
        GridPane pane = new GridPane();
        // Set column and row variables
        int column = 0, row = 0;
        
        for(int c = 0; c < cells.length; c++) {
            pane.add(cells[c], column, row);
            // Increment the Column
            column++;
            // Max value for Column
            if(column == 3) {
                column = 0;
                // Increment the Row
                row++;
            }
        }
        
        // Commence the Scenery
        Scene scene = new Scene(pane);
        primaryStage.setTitle("Tic-Tac-Toe Board");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    // Launch the Program
    public static void main(String[] args) {
        launch(args);
    }
}

Now, from a regular glance, this code seems fine as is, it should be able to at least. However, I needed to download an external JRE to get some of the functions working as this assignment is focused around JavaFX, which I haven't used before. I did find at least one extra library to go with my program that these beginning errors disappear; currently, I'm using JavaSE-16(the default library for my version of eclipse) and jre-9.0.4, and most errors aren't there thanks to this program. However, one issue that I managed to find was: The project was not built due to "Failed to init ct.sym for C:\Program Files\Java\jre-9.0.4\lib\jrt-fs.jar". Fix the problem, then try refreshing this project and building it since it may be inconsistent If anyone can find some kind of fix for an issue like this for me, I would greatly appreciate the help. Thank you for reading through this post, and I hope we can work through this.

Edit: Okay, so I did manage to get pathing fixed, but then I encountered a unique error that displayed the following: j

ava.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 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: Cannot invoke "javafx.scene.image.ImageView.setFitWidth(double)" because "this.cells[t]" is null at ticTacToeBoard.start(ticTacToeBoard.java:35) 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 ticTacToeBoard 

Fortunately, I did manage to find a way to fix this, and I simply added cells[t] = new ImageView(); so... shoutout to Jim for giving me that little extra nudge in the right direction.

  • 1
    I don’t think this is a build path issue, it appears to be an environment or configuration issue. [Likely duplicate:](https://stackoverflow.com/questions/63446925/the-project-was-not-built-due-to-failed-to-init-ct-sym-for-c-program-files-jav), especially look at Phil’s comment and recommendation on one of the answers: create a new project from scratch. – jewelsea Dec 11 '21 at 05:20
  • I also advise using only recent stable release software, e.g. uninstall your old IDE and JDK and JavaFX versions, then just install the most recent IDE version and JDK 17 + JavaFX 17.0.1. Follow setup instructions in the getting started documentation at [openjfx.io](https://openjfx.io/openjfx-docs/). It may seem a pain to do this, but will probably save you time in the future as you know you will be working with a stable, modern environment you can easily get support for. – jewelsea Dec 11 '21 at 05:24
  • “just make the new project jre9.0.4 from the start” -> No. The linked getting started documentation for JavaFX says: “it is required to have a recent version of JDK 16, or at least JDK 11.”, so jre 9 is not a supported runtime environment for a recent openjfx based distribution. – jewelsea Dec 11 '21 at 05:32
  • 1
    The ct.sym file is only present in JDK downloads, it looks like you are just using a JRE - switch to a full JDK Java. – greg-449 Dec 11 '21 at 08:13
  • Also don’t hardcode file based resource lookups, see [how to lookup resources](https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other). – jewelsea Dec 11 '21 at 08:19
  • Why did you install Eclipse IDE 2021-09 when 2021-12 is the current version? In the Java IDE packages shipped as ZIP a Java JDK 17 is included and when using the Eclipse Installer you can choose it. – howlger Dec 11 '21 at 08:33
  • 3
    Don't put stack traces in comments, edit the question, and put them as text in the question formatted as code (which you have already done in your follow up question, so delete the stack comments from this question and replace them with a link to the follow-up question, that is more useful for other readers how find this question). – jewelsea Dec 11 '21 at 22:35
  • Well, it's been a while everyone, but I'd like to say thank you for the contributions. I've been able to complete my project with relative ease, and I'm sorry if I wasted your time. – Metiri Magoro Mar 25 '22 at 03:36

0 Answers0