Results 1 to 2 of 2
  1. #1
    Lingerz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default 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".

    Java 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:

    Java 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.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

Similar Threads

  1. Public static method error
    By leapinlizard in forum New To Java
    Replies: 5
    Last Post: 04-30-2009, 12:10 AM
  2. Replies: 3
    Last Post: 04-11-2009, 10:14 PM
  3. Replies: 1
    Last Post: 02-10-2009, 11:03 AM
  4. Non-Static method in static context error
    By wizmang in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 09:51 AM
  5. Replies: 1
    Last Post: 08-07-2007, 06:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •