-
Non-static method error
Hello there.
When I compile the below piece of code, whether the method is static or not, I get "non-static method cannot be referenced from a static context".
Code:
public static void createSprite(int spriteID, int x, int y) {
BufferedImage img = null;
try {
img = ImageIO.read(new File("Sprites/" + spriteID + ".png"));
Graphics.drawImage(img, x, y, null);
}
catch (IOException e) {
}
}
The error:
Code:
Client.java:197: non-static method drawImage(java.awt.Image,int,int,java.awt.ima
ge.ImageObserver) cannot be referenced from a static context
Graphics.drawImage(img, x, y, null);
If someone could point out to me what's wrong, or give me a hint on why it does that, I'd be very grateful.
Thanks in advance.
-
The method you are trying to call is not static but you are calling it using ClassName.methodName, Graphics.drawImage. That is not allowed. Better read the Java basics on how to call methods via references to instances before you try to do any graphics in Java.