Lethalbeast
Lethalbeast

Reputation: 25

cant get array in class A to work in class B

I am working on a java project. When i first started i had a lot of code in one java class. this was starting to become really messy so i tried to spread the code into other classes. this is where i get got a problem with an array.

i made a class that creates the array, the array has one getter method so my main class can use it.

public class CreateBlockArray {

private static int[][][] blockarray;


public CreateBlockArray(){

//get coordinates 

    //fill array        
    int[][][] blockarray = new int[Xdelta+ 10][Ydelta + 10][Zdelta + 10];

    blockarray = fillarray(Xlow,Ylow, Zlow, Xhigh, Yhigh, Zhigh, blockarray   );
    blockarray = getvisibleBlocks(Xlow,Ylow, Zlow, Xhigh, Yhigh, Zhigh, blockarray);    
    blockarray = filloceanfloor(Xlow,Ylow, Zlow, Xhigh, Yhigh, Zhigh, blockarray);  
    //these three methods modify array and than return it

}


public int[][][] getBlockarray() {
    return blockarray;
}     

in the main class i use this

CreateBlockArray Array = new CreateBlockArray();
int[][][] blockarray = Array.getBlockarray();
//more code
if (blockarray[i][k][j] != 0) //it crashes here

this gives a null pointer exception. from similair questions on stackoverflow i understood that the array is not a primitive but a reference.

so i tried simply copying the array block by block with this code

blockarray2 = new int[(Xhigh-Xlow)+10][(Yhigh-Ylow)+10][(Zhigh-Zlow) +10];
    for(int h = Ylow;  h <= Yhigh;h++)
    {
    for(int i = Xlow;  i <= Xhigh;i++)
    {           
    for(int j = Zlow;  j <= Zhigh;j++)
    {                           

        blockarray2[i-Xlow][h-Ylow][j-Zlow] = blockarray[i-Xlow][h-Ylow][j-Zlow];

    }}}  

when i return this everything works fine and i get no errors. can i also return the first array without having to copy it?

Upvotes: 0

Views: 52

Answers (2)

RealSkeptic
RealSkeptic

Reputation: 34638

Here you declare your array:

private static int[][][] blockarray;

But in the constructor, you declare it again as a local variable:

int[][][] blockarray = new int[Xdelta+ 10][Ydelta + 10][Zdelta + 10];

So all your operations are done on the local array rather than the field.

Avoid declaring it again, just write

blockarray = new int[Xdelta+ 10][Ydelta + 10][Zdelta + 10];

inside the constructor, and then it will use the field.

Also, of course, if you want an array to be part of the state of a class, it must not be declared as static, or there will be just one array shared between all instances of that class. Perhaps you don't need a constructor at all, just a static initialization block.

Upvotes: 2

Jean-Fran&#231;ois Savard
Jean-Fran&#231;ois Savard

Reputation: 21004

The problem is in this constructor :

public CreateBlockArray(){

//get coordinates 

    //fill array        
    int[][][] blockarray = new int[Xdelta+ 10][Ydelta + 10][Zdelta + 10];

    blockarray = fillarray(Xlow,Ylow, Zlow, Xhigh, Yhigh, Zhigh, blockarray   );
    blockarray = getvisibleBlocks(Xlow,Ylow, Zlow, Xhigh, Yhigh, Zhigh, blockarray);    
    blockarray = filloceanfloor(Xlow,Ylow, Zlow, Xhigh, Yhigh, Zhigh, blockarray);  
    //these three methods modify array and than return it

}

You are not really modifying the blockarray of the class as you re-declare it in the method :

int[][][] blockarray = new int[Xdelta+ 10][Ydelta + 10][Zdelta + 10];

just change that line for

blockarray = new int[Xdelta+ 10][Ydelta + 10][Zdelta + 10];

and remove the static identifier in that line :

private static int[][][] blockarray;

Upvotes: 5

Related Questions