Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am getting this strange error and I cannot for the life of me figure out why:

    Cannot instantiate the type Image

CODE:

import java.awt.Image;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class MainMenuState extends BasicGameState {

    int stateID = -1;

    Image background = null;
    Image startGameOption = null;
    Image exitOption = null;

    float startGameScale = 1;
    float exitScale = 1;
    MainMenuState( int stateID ) 
    {
       this.stateID = stateID;
    }


    public int getID() {
        return stateID;
    }

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
        try {

        background = new Image("data/menu.jpg");

        Image menuOptions = new Image("data/menuoptions.png");

        startGameOption = menuOptions.getSubImage(0, 0, 377, 71);

        exitOption = menuOptions.getSubImage(0, 71, 377, 71);

        }catch (SlickException e) {
            System.err.print(e);
        }
    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {

    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {

    }

}

Why do I get this error? I've googled endlessly and nobody else has it, this worked fine in my other game. Any ideas?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

You're importing this right at the top of your code:

import java.awt.Image;

So you're actually trying to instantiate the Java AWT Image class, which is abstract, so no wonder you're getting that error. I'm guessing you used your IDE's suggestion to import a namespace.

Slick2D's Image class is located in the org.newdawn.slick namespace, which you never import. The Slick2D Image class simply doesn't exist as far as your code is concerned - but the java.awt Image class does.

import org.newdawn.slick; and you'll be fine.

share|improve this answer
    
Why? I've made two programs so far that have all used Image background = null; and then background = New Image(filepath); in the init() method. But in this program it doesn't? I haven't changed anything. –  speak Jun 23 '12 at 3:02
    
I don't know. I don't use Slick2D. But that's why you're getting your error - you're instantiating it and you can't. This was a very literal answer. Maybe you're using a different Image type to what you think, or you're constructing an Image elsewhere via, say, the Image() or Image(Image) constructors which are protected. It would help if you knew exactly which line was throwing this error. –  Jonathan Hobbs Jun 23 '12 at 3:07
    
Line 32: background = new Image("data/menu.png");. Cannot instantiate the type Image. I have a separate eclipse open and I typed out the exact same code (declared, then initialised in init() ) and it works perfectly. Try it in my Eclipse 2 and I get this error. I am lost. –  speak Jun 23 '12 at 3:12
    
@speakon I noticed something and edited my answer. –  Jonathan Hobbs Jun 23 '12 at 3:20
    
I cannot thank you enough. –  speak Jun 23 '12 at 3:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.