Search code examples
javaarrayseclipse2d-games

Arrays Disappearing?


The title seems a little odd I know but that is the only way that I can describe my problem.

The Project is creating a simple puzzle game were you have to move tiles to locations to form a 4(tile) x 4(tile) image.

It has a couple of classes but the problematic ones are the Tile Grid classes. My thought process was: I'll create two classes for Tiles one for Left and one for Right

(I chose 2 because I was having a problem which just one whereby after shuffling the arrays I the main window would show duplicate images in the tile positions even though the array itself had no duplicates but a I reasoned that this was because I was making 2 TileGrid classes thereby making 2 Icon arrays both of size 16 both containing all of the images and rather than getting the Icons from only one of these arrays I was getting them from both. Also 2 separate classes reduces the amount of Anonymous Classes being made.)

Those classes will have two arrays of class Tile one that is used for operations and one that is a master to be used for reset.

Then i loop through those arrays setting the Tile icon to the Icon in one of the corresponding arrays in the MazeImage class

The MazeImage class takes an image, splits it into 16 pieces and adds those pieces to an image array and an unshuffled array. the image array is the operable array the unshuffled array is for the solution mechanic

once the image is split into 16 and stored the MazeImage class runs the setShuffledMaster() and setShuffledProxy() setShuffledMaster() copies images[] into master[] then shuffles the master array then splits the master array in 2 via System.copyarray and stores parts 1-8 into masterL[] and 8-16 into masterR[]

setShuffledProxy() then divides the master[] array into two and stores half into the imagesL[] and imagesR[] these are the arrays that will be used for operations

then I have a bunch of getMethods() that return the arrays however it seems that these get methods aren't working or are working but the half/half arrays are being reset to null

the exact error is a null pointer exception and occurs at : masterL[i].setIcon(img.getMasterL(i)); in the TGridL class, but also the same line in the TGridR class

This is probably one of the eventual problems that I may need to fix to get everything working but its just killing me because I can't see were/why this is happening any help would be truly sanity saving.

On a side note if anyone has any ideas on how to improve aspects of this please let me know. Right now this is akin to a rough draft so things aren't as pretty as they could be.

Here is the code:

for(int i = 0; i<masterL.length; i++)
        {
            masterL[i] = new Tile(){
                public Dimension getPreferredSize() {
                      return new Dimension(80, 80);
                    //Sets Dimensions 
                   };
            };


            masterL[i].setIcon(img.getMasterL(i));
            masterL[i].setIdentifier("Tile_" + (i + 1));
            //---------------------------------------------
        }

TGridR is exactly the same simply replaces all 'L' with 'R' so i won't post it but if you need it please let me know.

public void setShuffledMaster()   //Master Tiles Left
    {
        List<Image> temp;  
        //Temp list for shuffle

        Image[] shuffled = new Image[16];  
        //array for shuffled images

        shuffled = images; 
        //Set equal to initial right after crop from editImage();

        temp = Arrays.asList(shuffled); 
        //Set temp list equal to init order

        Collections.shuffle(temp); 
        //Shuffle

        shuffled = (Image[]) temp.toArray();
        master = shuffled;
        //Change List to Array to be able to loop over it

        System.arraycopy(shuffled, 0, masterL, 0, masterL.length);
        System.arraycopy(shuffled, masterL.length, masterR, 0, masterR.length);
        //Split array into two parts




 public ImageIcon getMasterL(int i) //Get Master Left
 {
    img = new ImageIcon(masterL[i]);
    return img;
 } 

So the Gist is that getMasterL is not actually working because apparently the masterL array is empty however when I print out the contents of all the arrays that MazeImage class creates none of those are null

If you guys need anything else or need clarification please let me know.

Thanks a bunch!


Solution

  • You're not initializing your img object of type MazeImage. Probably do that in the constructor...

    img = new MazeImage(); // Or whatever constructor you need here.
    

    Also, if your left and right classes are so similar, you could have simply just used a single base Tile class and subclassed it to create two subtly different varieties.