2D Array of classes are null - java

Im working on a program that uses class Map that stores a 2D Array of Tile objects to form a grid. In the map class:
public class Map {
private int x = 80;
private int y = 40;
//store the entire map - a 50x50 2d array
private tile[][] grid = new tile[x][y];
public void Map() {
initialize();
grid[0][0].tile('.');
}
public void initialize() {
for (int i = 0; i < y; i++) {
for (int j = 0; j < x; j++) {
grid[j][i] = new tile();
}
}
}
public void display() {
for (int i = 0; i < y-1; i++) {
for (int j = 0; j < x-1; j++) {
System.out.println("Pass [" + j + "][" + i + "]");
System.out.print(grid[j][i].c());
}
System.out.println();
}
}
}
In the tile class:
public class tile {
private String title = "null";
private int id = 0;
private char c = ' ';
private boolean isVis = false;
public tile() {
id = 1;
format(id);
}
private void format(int n) {
c = 'A';
title = "foo";
isVis = false
}
public char c() { return c; }
}
When running the program, I get an output of:
Pass [0][0]
Exception in thread "main" java.lang.NullPointerException
at Map.display(Map.java:22)
at rogue.main(rogue.java:7)
highlighting the line:
System.out.print(grid[j][i].c());
What I think is happening is that the class inst being initialised once created. When I just print
System.out.print(grid[j][i]);
it returns a page full of "nullnullnull" What could I do to ensure the objects are initialised properly?

public void Map()
is not a constructor (it's a regular method), so it's not invoked when you create a Map instance, and your initialize method is never called.
Change it to
public Map()

Related

Is there a way to declare a 2 dimensional array of List in a type safe manner in Java? [duplicate]

This question already has answers here:
Type Cannot create a generic array of List<FooClass>
(3 answers)
"Cannot create generic array of .." - how to create an Array of Map<String, Object>?
(6 answers)
Closed 1 year ago.
Here is the code I have that works but with a type safety warning when I size and instantiate the array:
import java.util.ArrayList;
import java.util.List;
public class Test {
private static final int MAX_ROWS = 2;
private static final int MAX_COLS = 5;
private List<String> _stringSets[][];
public Test() {
_stringSets = new List[MAX_ROWS][MAX_COLS];
for(int row = 0; row < MAX_ROWS; row++) {
for(int col = 0; col < MAX_COLS; col++) {
_stringSets[row][col] = new ArrayList<String>();
}
}
}
}
I tried
_stringSets = new List<String>[MAX_ROWS][MAX_COLS];
but this won't work...
Lists are unidimensional and would be declared something like this:
List<String> myList = new ArrayList<>()
When you seem to be looking for a 2-dimensional arrays, which are declared something like this:
String[][] myArray = new String[n1][n2]
So what you want is probably something along those lines:
public class Test {
private static final int MAX_ROWS = 2;
private static final int MAX_COLS = 5;
private String[][] stringSets;
public Test() {
stringSets = new String[MAX_ROWS][MAX_COLS];
for(int row = 0; row < MAX_ROWS; row++) {
for(int col = 0; col < MAX_COLS; col++) {
stringSets[row][col] = row+"-"+col;
}
}
}
}
Generally not recommended to mix Arrays and Lists in this way. May want to consider using other data structures to achieve a similar effect instead. For example, you could create a Map that uses Position coordinates to achieve similar results.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class MapList {
private static Map<Position, List<Integer>> mapList;
public static void main(String[] args) {
final int MAX_X = 5;
final int MAX_Y = 6;
mapList = new HashMap<>();
for(int i = 0; i < MAX_X; i++) {
for(int j = 0; j < MAX_Y; j++) {
mapList.put(new Position(i, j), new ArrayList<>());
for(int k = 0; k < 5; k++) {
mapList.get(new Position(i, j)).add(k);
}
}
}
for(Position p : mapList.keySet())
System.out.println(p.toString() + mapList.get(p));
}
private static class Position {
private Integer x;
private Integer y;
public Position(Integer x, Integer y) {
this.x = x;
this.y = y;
}
#Override
public boolean equals(Object o) {
if(this == o)
return true;
if(o == null)
return false;
if(getClass() != o.getClass())
return false;
Position other = (Position) o;
return x == other.x && y == other.y;
}
#Override
public int hashCode() {
return Objects.hash(x, y);
}
#Override
public String toString() {
return "(x:" + x + ", y:" + y + ")";
}
}
}

Checking to see if two 2D boolean arrays are equal at a given interval: Java

I have two 2d boolean arrays, the smaller array (shape) is going over the larger array (world).
I am having trouble to find a method to find out when the smaller array can "fit" into the larger one.
When I run the code it either just goes through the larger array, never stopping, or stops after one step (incorrectly).
public void solve() {
ArrayList<Boolean> worldList=new ArrayList<>();
ArrayList<Boolean> shapeList=new ArrayList<>();
for (int i = 0; i < world.length; i++) {
for (int k = 0; k < world[i].length; k++) {
worldList.add(world[i][k]);
display(i, k, Orientation.ROTATE_NONE);
for (int j = 0; j < shape.length; j++) {
for (int l = 0; l < shape[j].length; l++) {
shapeList.add(shape[j][l]);
if(shapeList.equals(worldList)) {
return;
}
}
}
}
}
}
A good place to start with a problem like this is brute force for the simplest case. So, for each index in the world list, just check to see if every following index of world and shapes match.
Notice we only iterate to world.size()-shapes.size(), because naturally if shapes is longer than the portion of world we haven't checked, it won't fit.
import java.util.ArrayList;
public class Test {
ArrayList<Boolean> world = new ArrayList<>();
ArrayList<Boolean> shapes = new ArrayList<>();
public static void main(String[] args) {
new Work();
}
public Test() {
world.add(true);
world.add(false);
world.add(false);
world.add(true);
shapes.add(false);
shapes.add(true);
// Arraylists initialized to these values:
// world: T F F T
// shapes: F T
System.out.println(getFitIndex());
}
/**
* Get the index of the fit, -1 if it won't fit.
* #return
*/
public int getFitIndex() {
for (int w = 0; w <= world.size()-shapes.size(); w++) {
boolean fits = true;
for (int s = 0; s < shapes.size(); s++) {
System.out.println("Compare shapes[" + s + "] and world["+ (w+s) + "]: " +
shapes.get(s).equals(world.get(w+s)));
if (!shapes.get(s).equals(world.get(w+s))) fits = false;
}
System.out.println();
if (fits) return w;
}
return -1;
}
}
When we run this code, we get a value of 2 printed to the console, since shapes does indeed fit inside world, starting at world[2].
You can find the row and column of fitting like this
public void fit() {
int h = world.length - shape.length;
int w = world[0].length - shape[0].length;
for (int i = 0; i <= h; i++) {
for (int k = 0; k <= w; k++) {
boolean found = true;
for (int j = 0; j < shape.length && found; j++) {
for (int l = 0; l < shape[j].length && found; l++) {
if (shape[j][l] != world[i + j][k + l])
found = false;
}
}
if (found) {
//Your shape list fit the world list at starting index (i, k)
//You can for example save the i, k variable in instance variable
//Or return then as an object for further use
return;
}
}
}

Printing a 2 dimensional constructor in Java

I'm need to print the values of the 2d array in the constructor from the main method. Everytime I call the mainSc array, I get a value of null for each value in the 2d array. Why is that and how do I fix the array to call values from the constructor?
public class Main {
public static void main(String[] args) {
String[][] mainSc = new String[5][5];
System.out.println(Arrays.deepToString(mainSc));
}
}
import java.util.Arrays;
public class Schedule {
private int numDays;
private int numClasses;
private String[][] Cal;
public Schedule(String[][] array) {
this.numDays = 5;
this.numClasses = 4;
this.Cal = array;
}
public String[][] Array() {
for (int r = 0; r < numDays; r++){
for (int j = 0; j <= numClasses; j++){
this.Cal[0][0] = "Monday";
this.Cal[1][0] = "Tuesday";
this.Cal[2][0] = "Wednesday";
this.Cal[3][0] = "Thursday";
this.Cal[4][0] = "Friday";
}
}
return this.Cal;
}
public void printSchedule() {
for (int r = 0; r <= numDays; r++){
for (int j = 0; j <= numClasses; j++){
System.out.println(this.Cal[r][j]);
}
}
}
}
Declaring an array reference variable does not create an array. The next step in the process is to use the new keyword to create an array and assign its address to the variable. You should also include the dimensions for the array.
private String[][] cal = new String[5][5];

Why does this code in java print a reference instead the result of invoking the method?

I am supposed to write a method that accepts 3 2-D arrays of This method should determine whether one of the matrices is the result of matrix addition of the other two.
public class Matrix {
public static void main(String[]args){
int [][] a = {{5,2,3},{4,1,6},{0,7,2}};
int [][] b = {{1,2,3},{4,5,6},{0,1,2}};
int [][] t = {{6,4,6},{8,6,12},{0,8,4}};
System.out.println(add(a,b));
System.out.println(check(a,b,t));
}
public static int [][] add(int[][]a,int[][]b){
int i=0;
int j=0;
int[][] r = new int [3][3];
while (i<a.length){
r[i][j] = a[i][j] + b[i][j];
i++;
j++;
}
return r;
}
public static boolean check(int[][]a,int[][]b,int[][]t){
int i = 0;
int j = 0;
while(i<t.length){
if(t==add(a,b))
return true;
}
return false;
}
}
add returns an array. Arrays in Java are objects, but they do not override the toString() method. When printing, you'd print their default toString() call, which is implemented by Object as return getClass().getName() + "#" + Integer.toHexString(hashCode());.
Luckily, Java provides a utility in the form of java.util.Arrays.deepToString(Ojbect[]) to generate a more readable string output:
System.out.println(Arrays.deepToString(add(a,b)));
EDIT:
Your add method is also wrong. Your code iterates i and j together, so it only sums the elements along the matrix's diagonal instead of adding all of them. You should use a nested loop instead:
public static int [][] add(int[][]a, int[][]b) {
int[][] r = new int [3][3];
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
r[i][j] = a[i][j] + b[i][j];
}
}
return r;
}
Your check method, by the way, is also wrong - it attempts to compare the array itself instead of is elements:
public static boolean check(int[][]a, int[][]b, int[][]t) {
int[][] r = add(a, b);
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (r[i][j] != t[i][j]) {
return false;
}
}
}
return true;
}

Calculating in an array

I want to calculate an x and a y value in my coördinaat class. I've got another class called arrayreader which just reads a textfile and returns it.
public class Coordinaten
{
public static final double SCHERMBREEDTE = 1200;
public static final double SCHERMHOOGTE = 1000;
private double tMax, tMin, sMax, sMin;
private Double[] ecgWaardes, xWaardes, yWaardes, coordinaatX;
public Coordinaten()
{
ArrayReader ecg1 = new ArrayReader();
ecg1.leesBestand();
ecg1.getLijnen();
ecgWaardes = ecg1.getLijnen();
}
public double bepaalTMax()
/**
* Deze method geeft de tijd in miliseconden van de ECG weer.
*/
{
tMax = (ecgWaardes.length * 2);
return tMax;
}
public double bepaalSMax()
{
for (int i = 0; i < ecgWaardes.length; i++)
{
sMax = ecgWaardes[0];
if (ecgWaardes[i] > sMax)
{
sMax = ecgWaardes[i];
}
}
return sMax;
}
public double bepaalSMin()
{
for (int i = 0; i < ecgWaardes.length; i++)
{
sMin = ecgWaardes[0];
if (ecgWaardes[i] < sMin)
{
sMin = ecgWaardes[i];
}
}
return sMin;
}
public Double[] berekenX()
{
for (int i = 0; i < ecgWaardes.length; i++)
{
coordinaatX = new Double[i];
xWaardes [i] = (double)((((i+1) *2) - 0) * (SCHERMBREEDTE-1) / (tMax - 0));
}
return xWaardes;
}
public Double[] berekenY()
{
for (int i = 0; i < ecgWaardes.length; i++)
{
yWaardes = new Double[i];
yWaardes [i] = (double)(((ecgWaardes[i] - sMax) * (SCHERMHOOGTE-1)) / (sMin - sMax));
}
return yWaardes;
}
It just keeps giving me null pointer exceptions and i really don't know why?
Anyone who can help?
When you initialise yWaardes, xWaardes, ... you put the initialization in your for loop! This causes your array to be reinitialised and (and resized) every loop in the for! You should only initialise your arrays once! This can be done outside the for loop and by making it ecgWaardes.length:
public Double[] berekenY()
{
yWaardes = new Double[ecgWaardes.length];
for (int i = 0; i < ecgWaardes.length; i++)
{
yWaardes [i] = (double)(((ecgWaardes[i] - sMax) * (SCHERMHOOGTE-1)) / (sMin - sMax));
}
return yWaardes;
}
EDIT: Note that the above only is valid for the initialisation for yWaardes, simular mistakes can be found for the other arrays. So make sure all your arrays are properly initialised!

Categories