-2

I originally started with Chillax, after Encountering so many problems so near to deadline, I went back to the IDE I am more familiar with, NetBeans, and I changed my approach to a more basic "Asteroid"-type game:

In NetBeans I get:

Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
at gayme.Craft.<init>(Craft.java:27)
at gayme.Board.<init>(Board.java:54)
at gayme.Gayme.<init>(Gayme.java:9)
at gayme.Gayme.main(Gayme.java:19)
Java Result: 1

sources: (Craft 26 - 34)

    public Craft() {
    ImageIcon ii = new ImageIcon(this.getClass().getResource("craft.png"));
    image = ii.getImage();
    width = image.getWidth(null);
    height = image.getHeight(null);
    missiles = new ArrayList();
    visible = true;
    x = 40;
    y = 60;}

(Board 54)

    craft = new Craft();

(Gayme 9)

    add(new Board());

(Gayme 19)

   new Gayme();

I have issues that I really need resolved and my sleep deprived brain is coming up with a loss on each. Feel free to help out on whichever game you'd rather. Thanks so much guys!

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
MakJagz3
  • 29
  • 1
  • 3

1 Answers1

5

There are 3 methods:

A few things to remember about working with Jar files and resources located within:

  • JVM is case sensitive thus file and package names are case sensitive. i.e Main class is located within mypackage we cannot now extract it with a path like: myPackAge

  • Any period '.' located within the package name should be replaced by '/'

  • If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'. Resource names begin with / when executing class and the resources are in different packages.

Lets put that to the test using my preferred method of getResource(..) which will return the URL of our resource:

I create a project with 2 packages: org.test and my.resources:

enter image description here

As you can see my image is in my.resources while the Main class which hold main(..) is in org.test.

Main.java:

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class Main {

    public static final String RES_PATH = "/my/resources";//as you can see we add / to the begining of the name and replace all periods with /
    public static final String FILENAME = "Test.jpg";//the case sensitive file name

    /*
     * This is our method which will use getResource to extarct a BufferedImage
     */
    public BufferedImage extractImageWithResource(String name) throws Exception {

        BufferedImage img = ImageIO.read(this.getClass().getResource(name));

        if (img == null) {
            throw new Exception("Input==null");
        } else {
            return img;
        }
    }

    public static void main(String[] args) {
        try {
            BufferedImage img = new Main().extractImageWithResource(RES_PATH + "/" + FILENAME);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
}

If you play around with the names of the RES_PATH or FILENAME without making appropriate changes to actual files you will get an exception (just shows yous how careful we must be with paths)

UPDATE:

For your specific issue you have:

ImageIcon ii = new ImageIcon(this.getClass().getResource("craft.png"));

it should be:

ImageIcon ii = new ImageIcon(this.getClass().getResource("/resources/craft.png"));

The Alien and other classes also need to be changed.

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • @user1890890 tested your netbeans project found the problem: see update – David Kroukamp Dec 10 '12 at 08:17
  • Thanks for your help. I changed the image imports, now it compiles but looks like [this](http://i.imgur.com/5wBcK.png) before immediately going to the "Game Over" Screen... sorry to bug you even further, but any ideas? – MakJagz3 Dec 10 '12 at 08:29
  • Your images are not being scaled, so if the image is 300x300 it will show at 300x300, see my answer here for scaling images: http://stackoverflow.com/questions/12660122/image-resizing-and-displaying-in-a-jpanel-or-a-jlabel-without-loss-of-quality/12660146#12660146 – David Kroukamp Dec 10 '12 at 08:35
  • @user1890890 we all make mistakes especially under pressure glad to be of help – David Kroukamp Dec 10 '12 at 08:37
  • 1
    thanks again, [here](http://ge.tt/4BnWbGT/v/0?c) is my game zip, fully functional thanks to your help! – MakJagz3 Dec 10 '12 at 13:53
  • @MakJagz3 : LOL :-), I loved `MY GAME`, just wanted to change a few thingies in it, but slowly slowly will try to do that :-) – nIcE cOw Jul 12 '13 at 15:39