Question

The code:

public class EmptyTile extends TileEntity{ //error on this brace
    try{
        defaultTexture=TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("stone.png")); //defaultTexture is created in the class that this class extends
    }catch (IOException e) {
        e.printStackTrace();
    } //also an error on this brace
    public EmptyTile(int x, int y, int height, int width, Texture texture) {
        super(x, y, height, width, texture);
    }
}

I've also tried moving the try/catch statement to the EmptyTile constructor, but it needs to initialize the default texture before the super constructor gets called, which apparently isn't allowed.

I've also tried making the defaultTexture variable both static and regular in the parent class of this class.

Was it helpful?

Solution

You can not put a try/catch at the class level, only inside a constructor, a method or an initializer block. That's what's causing the error reported. Try moving the code inside the constructor, assuming that defaultTexture is an attribute:

public class EmptyTile extends TileEntity {

    public EmptyTile(int x, int y, int height, int width, Texture texture) {
        super(x, y, height, width, texture);
        try {
            defaultTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("stone.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

But if defaultTexture is an static attribute, then use a static initializer block:

public class EmptyTile extends TileEntity {

    static {
        try {
            defaultTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("stone.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public EmptyTile(int x, int y, int height, int width, Texture texture) {
        super(x, y, height, width, texture);
    }

}

OTHER TIPS

public class EmptyTile extends TileEntity{
    public EmptyTile(int x, int y, int height, int width, Texture texture) {
        super(x, y, height, width, texture);
        try{
            defaultTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("stone.png")); //defaultTexture is created in the class that this class extends
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Note that if TileEntity uses defaultTexture in the constructor, you will have to modify the constructor to allow it to be passed in.

If you want to do it outside the constructor you can put it in an instance initializer block:

public class EmptyTile extends TileEntity {

    // empty brace is instance initializer
    {
        try {
            defaultTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("stone.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public EmptyTile(int x, int y, int height, int width, Texture texture) {
        super(x, y, height, width, texture);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top