0
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ 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. \$\endgroup\$
    – speak
    Jun 23, 2012 at 3:02
  • \$\begingroup\$ 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. \$\endgroup\$ Jun 23, 2012 at 3:07
  • \$\begingroup\$ 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. \$\endgroup\$
    – speak
    Jun 23, 2012 at 3:12
  • \$\begingroup\$ @speakon I noticed something and edited my answer. \$\endgroup\$ Jun 23, 2012 at 3:20
  • \$\begingroup\$ @speakon You're welcome. :) \$\endgroup\$ Jun 23, 2012 at 3:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .