0

I am trying to load an image using

static Image terrain = null;

Followed by

public static void main(String[] args) {
    Arcanus arc = new Arcanus();  
    try {
        terrain = ImageIO.read(getClass().getResource("Arcanus Terrain Tileset.png"));            
        Font customFont = Font.createFont(Font.TRUETYPE_FONT, new File("Golden-Sun.ttf")).deriveFont(12f);
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("Golden-Sun.ttf")));
        arc.setFont(customFont);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (FontFormatException e) {
        e.printStackTrace();
    }
}

But I get the following error

Uncompilable source code - non-static method getClass() cannot be referenced from a static context

I have no idea why its doing this any help would be apreciated

Mark9135
  • 101
  • 11

1 Answers1

2

You are probably calling

terrain = ImageIO.read(getClass().getResource("Arcanus Terrain Tileset.png"));

inside a static method. You cannot call getClass() inside a static method since getClass() refers to an instance of a class. In that context you should call:

terrain = ImageIO.read(MyClass.class.getResource("Arcanus Terrain Tileset.png")); 
Mathieu Fortin
  • 1,048
  • 7
  • 17