views:

36

answers:

2

Thanks for taking the time to read. =)

The problem I am having is mainly Object visibility. I'm creating objects and attempting to create references to them before they have been initialized or have any data in them. You can get an idea what I'm trying to do here with the code provided.

Certain Squares can see other square ect. Now my code does compile! The issue is I have getter-methods that return null from these objects. I tried placing all the initializations in the Square-class declaration however that is where I get "Illegal Forward reference".

So finally, at the end of the code block you notice how I attempt to access these objects and their references. All four return null, what Im going for is some indication a reference exists.

I do understand slightly what is going wrong here, perhaps my approach with the objects is incorrect? I think my amateur skills are mainly what hold me back on this one =)

class Puzzle extends JFrame
{

     Square square1;
     Square square2;
     Square square3;
     Square square4;
     Square square5;
     Square square6;
     Square square7;
     Square square8;
     Square square9;
     Square square10;
     Square square11;
     Square square12;
     Square square13;
     Square square14;
     Square square15;
     Square square16;

    public Puzzle()
    {

        PuzzleListener plist = new PuzzleListener();
        JPanel puzzle_board = new JPanel(new GridLayout(4,4,5,5));

        //randomize the text index's and the starting black square.

        //            Square(north, south, east,west,isblack,text)
        square1 = new Square(null,square2,square5,null,false,"1");
        square2 = new Square(null,square3,square6,square1,false,"2");
        square3 = new Square(null,square4,square7,square2,false,"3");
        square4 = new Square(null,null,square8,square3,false,"4");
        square5 = new Square(square1,square6,square9,null,false,"5");
        square6 = new Square(square2,square7,square10,square5,false,"6");
        square7 = new Square(square3,square8,square11,square6,false,"7");
        square8 = new Square(square4,null,square12,square7,false,"8");
        square9 = new Square(square5,square10,square13,null,false,"9");
        square10 = new Square(square6,square11,square14,square9,false,"10");
        square11 = new Square(square7,square12,square15,square10,false,"11");
        square12 = new Square(square8,null,square16,square11,false,"12");
        square13= new Square(square9,square14,null,null,false,"13");
        square14 = new Square(square10,square15,null,square10,false,"14");
        square15 = new Square(square11,square16,null,square14,false,"15");
        square16 = new Square(square12,null,null,square15,false,"16");

}



class Square extends JPanel
{

    //visible lable
    JLabel lblText;

    //internal object ID
    int id;

    //Define the visible neighbors
    Square north;
    Square south;
    Square east;
    Square west;

    //is the square black
    boolean isBlack;

    public Square(String text)
    {

        lblText = new JLabel(text);
        //setLayout(new GridLayout(5,5));
        lblText.setLocation(10,10000);
        add(lblText);
    }

    public Square(Square n,Square e,Square s,Square w,boolean black, String text)
    {

        north = n;
        east = e;
        south = s;
        west = w;

        isBlack = black;

        lblText = new JLabel(text);
        //setLayout(new GridLayout(5,5));
        lblText.setLocation(10,10000);
        add(lblText);

    }//end square

    Square getNorthNeighbor()
    {
        return north;
    }
    Square getSouthNeighbor()
    {
        return south;
    }
    Square getEastNeighbor()
    {
        return east;
    }
    Square getWestNeighbor()
    {
        return west;
    }
    boolean getIsBlack()
    {
        return isBlack;
    }


}


System.out.println("Panel 1 has been clicked and has the neighbors " + "North: " + square1.getNorthNeighbor() + "East: " + square1.getEastNeighbor() + "South: " + square1.getSouthNeighbor() + "West: " + square1.getWestNeighbor());
+1  A: 

Given that they need to know about each other, why not create all the squares first, and then have a setNeighbours(Square north, Square south, ...) method that you can call when you've actually created all the objects? It's not nice in terms of immutability (generally a nice property for a type to have) but fundamentally, you've got circular references.

To put it another way, imagine you had to create two Person instances, and indicate to each that they were brothers. You can't do:

Person billy;
Person bobby;
billy = new Person(bobby);
bobby = new Person(billy);

but you can do:

Person billy = new Person();
Person bobby = new Person();
billy.setBrother(bobby);
bobby.setBrother(billy);

It's nicer if you can avoid requiring circular references in the first place, but sometimes that's just the way of things.

Jon Skeet
Hey leave the easy ones for me! :)
willcodejavaforfood
Thanks Jon, I'm such a dork for not thinking of that. I hear ya on the circular references it's easier for my brain to wrap around the current problem I have.
Bryan Harrington
+1  A: 

Just use setters instead of trying to do it in the constructor as that simply wont be possible.

square1 = new Square();
square2 = new Square();
square3 = new Square();

square1.setNorth(square2);
square1.setSouth(square3);
...
willcodejavaforfood
Thanks, but Jon beat ya =)
Bryan Harrington