JTextField Array issue - java

So I'm working with a two-dimensional array of JTextFields for a Sudoku program.
public JTextField[][] userInputArray = new JTextField[9][9];
Now. I'm getting a continual null pointer exception, and can't discern how to fix it. It comes from firing this method:
public void showTextFields()
{
int rowCounter = 0;
int columnCounter = 0;
for (rowCounter = 0; rowCounter <= 8; rowCounter += 1)
{
for (columnCounter = 0; columnCounter <= 8; columnCounter += 1)
{
pane.setLayout(new GridLayout(9, 9));
//pane.add(userInputArray[rowCounter][columnCounter]);
//userInputArray[rowCounter][columnCounter].setColumns(1);
//userInputArray[rowCounter][columnCounter].setVisible(true);
}
}
}
Everything commented out will throw the nullpointerexception.
Optimally, my goal is to display the JTextFields on screen, assigning them on the grid.
NullPointerException occurs at pane.add(...)

While you have created the 2D array to house your fields, you need to instantiate the JTextField components in your array.
for (int i =0; i < userInputArray.length; i++) {
for (int j =0; j < userInputArray[0].length; j++) {
userInputArray[i][j] = new JTextField();
}
}

When you declare array it is initialized with default values. Default value for Object is null so you need to instantiate the objects first before using them
userInputArray[rowCounter][columnCounter] = new JTextField();
So now your code should look like below
for (columnCounter = 0; columnCounter <= 8; columnCounter += 1)
{
pane.setLayout(new GridLayout(9, 9));
userInputArray[rowCounter][columnCounter] = new JTextField();
pane.add(userInputArray[rowCounter][columnCounter]);
userInputArray[rowCounter][columnCounter].setColumns(1);
userInputArray[rowCounter][columnCounter].setVisible(true);
}

Related

JavaFx - Removing Node Stored in Array

I have an array of buttons declared as a class variable that is added to a gridPane. When I try to remove the buttons of the array from the gridPane the event handler is being recognized but the node is not being removed.
Any ideas of how to accomplish this?
for(int i = 0; i < Rows; i++) {
for(int j = 0; j < Cols; j++) {
bArray = new Button[Rows][Cols];
bArray[i][j] = new Button();
bArray[i][j].setMinSize(20,20);
bArray[i][j].setMaxSize(25,25);
gridBoard.setVgap(1); //vertical gap in pixels
gridBoard.add(bArray[i][j], j, i);
bArray[i][j].setOnMouseClicked(e->checkNeighbors());
}
}
// Local Method
public static void checkNeighbors() {
// This print out statement is met, but the removal does not occur
System.out.println("Action is called");
gridBoard.getChildren().remove(bArray[0][1]);
gridBoard.getChildren().remove(bArray[0][2]);
gridBoard.getChildren().remove(bArray[0][3]);
}
You are overriding bArray in each iteration and the only valid references that are left at the end of the for loops are from [Rows][0] to [Rows][Cols].
As an example if Rows = 4 and Cols = 4, that would be that at the end the only valid references are [3][0], [3][1], [3][2] and [3][3], and you are trying to remove [0][1], [0][2], [0][3].
You should move the initialization of bArray before the loops start.
bArray = new Button[rows][cols];
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
bArray[i][j] = new Button();
// ......
}

Label 2D-Array | JavaFX

I want to Fill a 2D-Array with Javafx Labels in Which I can change the Text when I click it.
This is my actual Code but it's returning a NullPointer Exception.
Blockquote
`public static Label[][] initWelt() {
Label[][] welt = new Label[DIM1][DIM2];
for (int x = 1; x < welt.length - 1; x++) {
for (int y = 1; y < welt.length - 1; y++) {
if (Math.random() > 0.4) {
welt[x][y].setText("X");
}
else{
welt[x][y].setText(" ");
}
}
}
return welt;
}`
it's returning a NullPointer Exception.
The only thing the below code does is initialise a two-dimensional array, it doesn't populate the two-dimensional array, hence the NullPointerException occurs.
Label[][] welt = new Label[DIM1][DIM2];
Basically you can't call this:
welt[x][y].setText("X");
without populating the two-dimensional array with object references.
to overcome the problem first populate the two dimensional array, something like below:
Label[][] welt = new Label[DIM1][DIM2];
for(int i = 0; i < DIM1; i++){
for(int j = 0; j < DIM2; j++){
welt[i][j] = new Label();
}
}
then you can proceed with your current task at hand.
so now your code becomes like this:
public static Label[][] initWelt() {
Label[][] welt = new Label[DIM1][DIM2];
for(int i = 0; i < DIM1; i++){ //populate the array
for(int j = 0; j < DIM2; j++){
welt[i][j] = new Label();
}
}
for (int x = 0; x < DIM1; x++) {
for (int y = 0; y < DIM2; y++) {
if (Math.random() > 0.4) {
welt[x][y].setText("X");
}
else{
welt[x][y].setText(" ");
}
}
}
return welt;
}
Note - personally I think it would be better to refactor the current method and insert the code that populates the two-dimensional array in a different method.

Clear Two Dimensional Array

How can I clear a 6x6 "table", so that anything in it is cleared?
(I made the clearbutton already with ActionListener...etc)
//other code above that creates window, below is the code that creates the table I need to clear
square = new JTextField[s][s];
for (int r=0; r!=s; r++) {
symbols[r] = new JTextField();
symbols[r].setBounds(35+r*35, 40, 30, 25);
win.add(symbols[r], 0);
for (int c=0; c!=s; c++) {
square[r][c] = new JTextField();
square[r][c].setBounds(15+c*35, 110+r*30, 30, 25);
win.add(square[r][c], 0);
}
}
win.repaint();
}
Loop over the array and and set each element to null. You can use the java.utils.Arrays utility class to make things cleaner/neater.
for( int i = 0; i < square.length; i++ )
Arrays.fill( square[i], null );
Here is one line solution:
Arrays.stream(square).forEach(x -> Arrays.fill(x, null));
Something like...
for (int index = 0; index < square.length; index++) {
square[index] = null;
}
square = null;
Will do more then the trick (in fact the last line would normally be enough)...
If you're really paranoid...
for (int index = 0; index < square.length; index++) {
for (int inner = 0; inner < square[index].length; inner++) {
square[index][inner] = null;
}
square[index] = null;
}
square = null;

Using the For statement to create buttons and add them to a panel?

I've tried a couple of ways to do this...Basically I'm trying to create a tic tac toe board for an assignment and maybe I'm missing something obvious, but I get a "not a statement" error when I try to create the buttons. Here's the code I've got:
int rows = 3;
int cols = 3;
JPanel ticTacToeBoard = new JPanel();
ticTacToeBoard.setLayout(new GridLayout(3, 3));
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
JButton gameButton[i] = new JButton[];
ticTacToeBoard.add(gameButton[i]);
}
}
Thanks...
You need to declare your array somewhere:
JButton[] gameButton = new JButton[size];
Then in your loop:
gameButton[i] = new JButton();
For example:
JButton[] gameButton = new JButton[rows * cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
gameButton[i] = new JButton();
ticTacToeBoard.add(gameButton[i]);
}
}
You can also have a look at the Java tutorial on arrays.
Note: Is there a reason why you don't use a List instead of an array? If would make your life easier.
The following is incorrect
JButton gameButton[i] = new JButton[];
There is no need for the []. Just do
JButton gameButton = new JButton();
ticTacToeBoard.add(gameButton);
If you want to store the buttons in an array as well, you should have code like
JButton[] buttonArray = new JButton[10];//or whatever length
...
JButton gameButton = new JButton();
buttonArray[i] = gameButton;

Eclipse - Java Array ID Values are Equal. Cannot change values independently

I have two arrays that I create like this:
public int GameBoard[][] = new int[30][14];
public int DirectionMap[][] = new int[30][14];
I then initialize the arrays like this:
for (int i = 0; i < GameBoard.length; i++)
{
for (int j = 0; j < GameBoard[i].length; j++)
{
GameBoard[i][j] = 0;
}
}
... //Same for DirectionMap
When I run the function:
DirectionMap = AStar(GameBoard);
To render the pathfinding map that my units will follow, DirectionMap is correctly set to the values generated based on my GameBoard. However GameBoard is set to the result as well. When I run the application in Debug Mode within Eclipse, I can see that the ID's of the two arrays are the same. For some reason they seem to be pointing to the memory space. My AStar function does not modify the GameBoard array at all. The only reference to it is int retVal[][] = GameBoard;
My function prototype is public int[][] AStar(int[][] Board); and it returns the int[][] retVal.
I have no idea why I cannot change the values of DirectionMap without GameBoard following. I have never had any issues like this before.
Any ideas are really appreciated. Thanks for your help.
public int[][] AStar(int[][] Board)
{
int retVal[][] = Board;
//Initialize All Needed Lists
int width = retVal.length;
int height = retVal[0].length;
int goalX = 0;
int goalY = 0;
//List<Node> fieldInfo = new ArrayList<Node>();
Node fieldArray[][] = new Node[width][height];
for (int i = 0; i < fieldArray.length; i++)
{
for (int j = 0; j < fieldArray[i].length; j++)
{
fieldArray[i][j] = new Node(i, j);
if (retVal[i][j] == 2)
{
fieldArray[i][j].setOpen(1);
fieldArray[i][j].setDirection(10); //Set as target
goalX = i;
goalY = j;
}
if (retVal[i][j] == 1)
{
fieldArray[i][j].setOpen(0);
fieldArray[i][j].setDirection(9); //Set as wall
}
}
}
//Add AStar Algorithm Here
for (int i = 0; i < fieldArray.length; i++)
{
for (int j = 0; j < fieldArray[i].length; j++)
{
if (fieldArray[i][j].getDirection() == 0)
{
//Occurs when node was never reached
int dX = i - goalX;
int dY = j - goalY;
if (dX < 0)
dX = -dX;
if (dY < 0)
dY = -dY;
if (dY > dX)
fieldArray[i][j].setDirection(1);
else
{
if (i > goalX)
fieldArray[i][j].setDirection(7);
if (i < goalX)
fieldArray[i][j].setDirection(3);
}
}
}
}
for (int i = 0; i < fieldArray.length; i++)
{
for (int j = 0; j < fieldArray[i].length; j++)
{
retVal[i][j] = fieldArray[i][j].getDirection();
}
}
return retVal;
}
Remember when you are passing an object to methods you are actually passing a copy of reference. So when you initialise retVal[][] = Board; you actually point Board using another reference retVal. And you are returning the same reference to DirectionMap. Hence same id's for Board and DirectionMap. Consider array copy instead.
I can see that the ID's of the two arrays are the same. For some
reason they seem to be pointing to the memory space
The int[] array in Java has 0's as default values.
int[] x = new int[2];
System.out.println(x[0]); // prints 0
System.out.println(x[1]); // prints 0
The only reference to it is int retVal[][] = GameBoard;
Java arrays are objects. If at any point you are setting array = array, you are setting one reference to point to the same object as the other.

Categories