-2

This is my first question, I hope it's not too poorly made.

I'm definitely beginner level at java, probably lower. I'm taking most of my code from a tutorial in fact, hoping I'll learn what all the things do soon enough.

Anyway, so far, I have 3 .java files in my program, and it shows the exception to be at all 3 of them, plus one I never made.

Here's the full error:

Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
at Emilia.<init>(Emilia.java:17)
at Board.<init>(Board.java:26)
at TestGame.<init>(TestGame.java:7)
at TestGame.main(TestGame.java:18)

Here's all the code:

TestGame.java

import javax.swing.JFrame;

public class TestGame extends JFrame {
    public TestGame() {
        add(new Board());

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(null);
        setTitle("Project Obcasus");
        setResizable(false);
        setVisible(true);
    }

    public static void main(String[] args) {
        new TestGame();
    }
}

Board.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Board extends JPanel implements ActionListener {
    private Timer timer;
    private Emilia emilia;

    public Board() {
        addKeyListener(new TAdapter());
        setFocusable(true);
        setBackground(Color.BLACK);
        setDoubleBuffered(true);

        emilia = new Emilia();

        timer = new Timer(5, this);
        timer.start();
    }

    public void paint(Graphics g) {
        super.paint(g);

        Graphics2D g2d = (Graphics2D)g;
        g2d.drawImage(emilia.getImage(), emilia.getX(), emilia.getY(), this);

        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }

    public void actionPerformed(ActionEvent e) {
        emilia.move();
        repaint();  
    }

    private class TAdapter extends KeyAdapter {
        public void keyReleased(KeyEvent e) {
            emilia.keyReleased(e);
        }

        public void keyPressed(KeyEvent e) {
            emilia.keyPressed(e);
        }
    }

}

Emilia.java

import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;

public class Emilia {
    private String emilia = "emiliasprite.png";

    private int dx;
    private int dy;
    private int x;
    private int y;
    private Image image;

    public Emilia() {
        ImageIcon ii = new ImageIcon(this.getClass().getResource(emilia));
        image = ii.getImage();
        x = 40;
        y = 60;
    }

    public void move() {
        x += dx;
        y += dy;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public Image getImage() {
        return image;
    }

    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_A) {
            dx = -1;
        }

        if (key == KeyEvent.VK_D) {
            dx = 1;
        }

        if (key == KeyEvent.VK_W) {
            dy = -1;
        }
    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_A) {
            dx = 0;
        }

        if (key == KeyEvent.VK_D) {
            dx = 0;
        }

        if (key == KeyEvent.VK_W) {
            dy = 0;
        }
    }
}

ImageIcon.java - Line 205

this(location, location.toExternalForm());

Again, I'm beginner level so if you guys could explain it as you would to a newcomer to java (or any programming language for that matter)

Thanks for any help. - Niblexis


path of .png file:

C:\Users\Damon\workspace\TestGame\Resources\Sprites\Player 

The .png file is in the player folder. I tried to run the program via the run button in Eclipse. At least, I think it's the run button because it's what showed me the errors in the first place.

Jason S
  • 184,598
  • 164
  • 608
  • 970
Niblexis
  • 5
  • 1
  • use github gist to share several files. combining files together into one file via pastebin in that way is a stumbling block since we can't match line numbers easily. – Jason S Aug 29 '14 at 01:22
  • because users with low points are limited in what they can do on SO. Anyway SO doesn't show line numbers either. – Jason S Aug 29 '14 at 01:23
  • ...and there's not much point in posting the innards of built-in classes unless the blame lies there. – Jason S Aug 29 '14 at 01:25

1 Answers1

2

Looks like the problem is in this line:

ImageIcon ii = new ImageIcon(this.getClass().getResource(emilia));

which means most likely that you haven't placed your .png file in the right place for Java to find it.

Could you post the exact path of the .png file on disk?


More specifically: a null pointer in this line of ImageIcon.java:

this(location, location.toExternalForm());

would imply that the URL location is null (causing an exception in the method call .toExternalForm(). If you look at the docs for Class.getResource() you will see it says:

Returns: A URL object or null if no resource with this name is found

which implies that Java can't find the resource in question.

For us to help, you will need to describe your runtime environment (are you running your program from .class files or in a .jar? at the command-line or in a debugger in Eclipse / Netbeans?) so we can help you figure out why the resource isn't being found.


You're effectively calling Emilia.class.getResource("emiliasprite.png") with Emilia.java in the default (root) package, which means that you need to tell your IDE / build process to copy this file into the root of the classpath. (in the same directory that Emilia.class ends up) Otherwise, Java has no idea where to find it.

If you want to place the resource somewhere else, you need to change the path accordingly, as well as the mechanism that copies the resource from the source directory to the appropriate place on the classpath.

See this stackoverflow answer: Java in Eclipse: Where do I put files on the filesystem that I want to load using getResource? (e.g. images for an ImageIcon)

Community
  • 1
  • 1
Jason S
  • 184,598
  • 164
  • 608
  • 970
  • You should mention that all assets *(images, configurations, ...)* should be in a `./resources/` folder under the source directory and that folder should be exported to the classpath. – Mr. Polywhirl Aug 29 '14 at 01:34
  • NPE is generally caused by a reference to a NULL object. (Well, fine ALWAYS) it is not caused by a non existent file as that would cause the FileNotFoundException. (Subclass of Exception) – EDToaster Aug 29 '14 at 01:34
  • hmm, that's weird.... – Jason S Aug 29 '14 at 01:36
  • Correct me if I am wrong :) – EDToaster Aug 29 '14 at 01:38
  • Mr. P -- why `./resources/` in particular rather than in a directory with another name? Perhaps you should mention it in your own answer. – Jason S Aug 29 '14 at 01:39
  • 1
    JClassic: your explanation sounded reasonable, but you're wrong; `Class.getResource()` returns `null` if it can't find the resource. – Jason S Aug 29 '14 at 01:46
  • Don't know if this is the right place to put the path. But, here it is: C:\Users\Damon\workspace\TestGame\Resources\Sprites\Player The .png file is in the player folder. To answer your second question, I tried to run the program via the run button in Eclipse. At least, I think it's the run button because it's what showed me the errors in the first place. – Niblexis Aug 29 '14 at 18:59
  • I've edited the question to add this info. – Jason S Aug 29 '14 at 20:03