I'm getting the error "Exception in thread "main" java.lang.NullPointerException" when I try to read an array value. Here is the code that I think causes the error.
class Board
{
private static char[][] board;
public Board(int r, int c)
{
setRow(r);
setColumn(c);
char board[][] = new char[row][column];
}
public void getBoard()
{
for (int c = 1;c <= getColumn()-1 ;c++)
{
System.out.print("\t"+c);
}
System.out.print("\n");
for (int r = 1; r <= getRow()-1; r++)
{
System.out.print(r);
for (int c = 1; c <= getColumn(); c++)
{
System.out.print("\t" + board[r][c]); //I think board[r][c] is causing it.
}
System.out.println("");
}
return;
}
}
I can upload the whole file if need be.
Any help would be appreciated, this kept me up last night.
Replace
char board[][] = new char[row][column];
with
board = new char[row][column];
In the first statement, you're assigning a value to a local variable, not to the one of your instance.
You are hiding member variable in the constructor
char board[][] = new char[row][column];
it should be
board= new char[row][column];
Related
I am attempting to generate a gameboard as a 2D array of chars, the top layer of the array is displaying the char versions of ints(no problems there), however the code is throwing an error message, as displayed below.
java -classpath .:/run_dir/junit-4.12.jar:target/dependency/*
Main Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
at Main.genVisual(Main.java:16)
at Main.main(Main.java:3)
Here is my code:
class Main {
public static void main(String[] args) {
char[][] hehe = genVisual(7, 2);
}
public static char[][] genVisual(int length, int height) {
char[][] visual = new char[height][length];
char character = ' ';
for (int n = 0; n < height; n++) {
for (int i = 0; n < length; n++) {
if (n == 0) {
int w = i + 1;
character = (char) w;
visual[n][i] = character;
} else {
visual[n][i] = 'x';
}
}
}
return visual;
}
}
I have tried changing the values that control the list size in the for loop and have browsed this website for answers, however I do not see any issues with the program.
Here's my code:
public static String[][] displayPlay()
{
String[][] board = new String[10][10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
board[i][j] = ".";
}
}
return board[][];
}
(This is meant to create a 10x10 array of "."s)
The error message in the title points to the end of the line return board[][]. Why is this?
You want to just return board
Change return board[][]; to return board;
You must return board, not board [][].
I'm currently trying to figure out how to redifine my toString method so that it will display the matrix. Here's the code..
import java.util.Random;
public class TextLab09st
{
public static void main(String args[])
{
System.out.println("TextLab09\n\n");
Matrix m1 = new Matrix(3,4,1234);
Matrix m2 = new Matrix(3,4,1234);
Matrix m3 = new Matrix(3,4,4321);
System.out.println("Matrix m1\n");
System.out.println(m1+"\n\n");
System.out.println("Matrix m2\n");
System.out.println(m2+"\n\n");
System.out.println("Matrix m3\n");
System.out.println(m3+"\n\n");
if (m1.equals(m2))
System.out.println("m1 is equal to m2\n");
else
System.out.println("m1 is not equal to m2\n");
if (m1.equals(m3))
System.out.println("m1 is equal to m3\n");
else
System.out.println("m1 is not equal to m3\n");
}
}
class Matrix
{
private int rows;
private int cols;
private int mat[][];
public Matrix(int rows, int cols, int seed)
{
this.rows = rows;
this.cols = cols;
mat = new int[rows][cols];
Random rnd = new Random(seed);
for (int r = 0; r < rows; r ++)
for (int c = 0; c < cols; c++)
{
int randomInt = rnd.nextInt(90) + 10;
mat[r][c] = randomInt;
}
}
public String toString()
{
return ("[" + mat + "]");
}
public boolean equals(Matrix that)
{
return this.rows == (that.rows)&&this.cols == that.cols;
}
}
I know how to display it and how to redifine the equals method, I feel like it's just late and I'm missing something silly. Sorry for any inconvience!
EDIT: Sorry, I forgot to specify that it had to be shown as a 2 dimensional row x column display.
EDIT2: Now I'm having trouble redefining the equals method, as it is required for my assignment. I rewrote it to this:
public boolean equals(Matrix that)
{
return this.mat == that.mat;
}
and it still outputs:
m1 is not equal to m2
m1 is not equal to m3
Is there any easy way to fix this?
You'd have to make a nested loop for each cell in your matrix.
public String toString()
{
String str = "";
for (int i = 0 ; i<this.rows ; i ++ ){
for (int j = 0 ; j < this.cols ; j++){
str += mat[i][j]+"\t";
}
str += "\n";
}
return str;
}
As for the "equal" method, I'm not quite sure what is the criteria.
Does your application consider two matrixes to be equal if they both have the same number of rows and cols?
P.S.
Overriding the equal method is a very good practice, but it is a better practice to override the "hashCode" method as well.
Might not be relevant to your app, but it is important.
Hope that would help :)
Use Arrays.deepToString:
public String toString()
{
return Arrays.deepToString(mat);
}
So basically I am trying to make a 9x9 grid for a minesweeper game. I need the grid to be filled with question marks to represent a minefield that has not been selected yet. Ex: [?][?][?][?][?] Basically my question is how would I get my program to output an array of question marks like that?
import java.util.Scanner;
import java.util.Arrays;
public class H4_Minesweeper {
public static void main(String[] args) {
//Game Description and rules
System.out.println("Minesweeper is a very straightforward game, the rules are simple.");
System.out.println("Uncover a mine (x), and the game ends. Uncover an empty square (o), and you keep playing.");
System.out.println("A question mark (?) will represent tiles you have not uncovered yet.");
System.out.println("Uncover a number, and it tells you how many mines lay hidden in the eight surrounding squares.");
System.out.println("Use this information to carefully choose which squares to click.");
System.out.println("\n\n\n");
Scanner userin;
String[][] board = new String [9][9];
for (int r = 0; r<board.length;r++){
for (int c = 0; c <board.length;c++){
}
}
}
}
First, you must initialize the array by setting all its elements to "?":
String[][] board = new String [9][9];
for (int r = 0; r<board.length;r++){
for (int c = 0; c <board.length;c++){
board[r][c] = "?";
}
}
Then you can print it:
for (int r = 0; r<board.length;r++){
for (int c = 0; c <board.length;c++){
System.out.print (board[r][c] + " ");
}
System.out.println();
}
This should do it.
for (int r = 0; r<board.length;r++){
for (int c = 0; c <board.length;c++){
board[r][c] = "?"; // Initialize the cell
System.out.print("[" +board[r][c] + "]"); // Display the content of cell board
}
System.out.println(); // go to next line
}
Fill your 2d array with the String "?" for each of the grid spaces, and then, go row by row printing out the values of each array index
Filling array:
String[][] board = new String[9][9];
for(int y=0;y<9;y++){
for(int x=0;x<9;x++){
board[x][y] = "?";
}
}
Displaying the rows:
for (int r = 0; r<9;r++){
String line = "";
for (int c = 0; c <9;c++){
line+="["+board[c][r]+"]";
}
System.out.println(line);
}
I encounter a NullPointerException on clickCell[r][c] = false; and on new LifeGUI(new LifeModel(x, y, s); and can't fix it. Please explain why this problem occurs and how I may fix it.
Code:
public LifeModel(int rows, int cols, int cellSize) {
row = rows;
col = cols;
cSize = cellSize;
for (int r = 0; r < row; r++) {
for ( int c = 0; c < col; c++) {
clickCell[r][c] = false;
}
}
}
public static void main(int x, int y, int s) {
new LifeGUI(new LifeModel(x, y, s));
}
You haven't shown where the clickcell array was declared, but likely you have declared it but not initialized it. You may have
boolean[][] clickcell;
but need:
boolean[][] clickcell = new boolean[rows][cols];
where rows and cols represent the size of the array you need.
You have to create the array object
boolean [][] clickCell = new boolean[rows][cols];
Add this command before the for loop.
More info here
If clickCell is declared somewhere else, the command should be:
clickCell = new boolean[rows][cols];
Or as GriffeyDog suggests, add the new boolean[rows][cols] at the place where you declare the array, depending on the logic of your program.