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.