0

I'm working on a fairly basic game for myself. New to Java Swing but not to Cpp. Onto the problem at hand.

As I start to run the GUI app, I'm shot with a Null Pointer Exception and given four links where they happen(I use Eclipse as my IDE). But when I look at them I see no problem with the lines or anything around them.

So I came to see if any of you can spot what I can't find.

This class grabs the picture from the source folder and sets up the keys for giving movement.

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

public class SPRITES 
{
private int x_sped;
private int y_sped;
private int x;
private int y;
private Image sprite;

public SPRITES()
{
    ImageIcon pine = new ImageIcon(this.getClass().getResource("Untitled.png")); //exception here, line 17
    sprite = pine.getImage();
    x = 0;
    y = 0;
}

public void move()
{
    x += x_sped;
    y += y_sped;
}

public int getX()
{
    return x;
}

public int getY()
{
    return y;
}

public Image getImage()
{
     return sprite;
}

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

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

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

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

    if(key == KeyEvent.VK_S)
    {
        y_sped = 1;
    }
}

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

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

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

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

    if(key == KeyEvent.VK_S)
    {
        y_sped = 0;
    }
}
}

The second one is for getting the frame setting up with completely making the frame and finishing up the making of the movement.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Stage extends JPanel implements ActionListener
{
private static final long serialVersionUID = 1L;
private Timer start_stop;
private SPRITES player;
public Stage()
{
    addKeyListener(new TAdapter());
    setFocusable(true);
    setBackground(Color.black);
    setDoubleBuffered(true);

    player = new SPRITES();

    start_stop = new Timer(5, this);
    start_stop.start();

}

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

    Graphics2D G2D = (Graphics2D) character;
    G2D.drawImage(player.getImage(), player.getX(), player.getY(), this);

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

public void actionPerformed(ActionEvent arg0) 
{
    player.move();
    repaint();
}

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

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

This last one should tie it all together...granted I knew how to fix the exception.

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class Framing extends JFrame
{
public Framing()
{
    add(new Stage()); 

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(600, 600);
    setLocationRelativeTo(null);
    setTitle("FLY");
    setResizable(false);
    setVisible(true);
}
public static void main(String[] args)
{
        new Framing();
}
}

What is causing these exceptions to happen? How can I fix the problem and what should I work on to avoid this happening again?

0

1 Answer 1

1

I think you are misreading the stacktrace from eclipse. All the links except for the first(The one at the top) is the call stack, which show the calls made to the place where you got a nullpointer exception.

Which fit because the code:
new Framing()
Calls
add(new Stage())
which calls
player = new SPRITES();
Which calls
ImageIcon pine = new ImageIcon(this.getClass().getResource("Untitled.png")); 

And this is where your nullpointer exception is. My guess is that getResources("Untitled.png"); returns null which then causes the ImageIcon constructor to throw a nullpointer exception.

Sign up to request clarification or add additional context in comments.

5 Comments

Oh, thanks for the explanation of what is really going on, but how do I fix it so that getResource("Untitled.png") doesn't return a null? Does it have to do with a resource being improperly put/called?
@Episha it depends on the path to the .png, if you get a nullpointer then its not the right path.
@MTilsted I took another look at the folder and did in fact see that I wasn't pointing to the right area. Thanks for the clear up and quick explanations.
Are you sure you want to use getResource. It should as far as I remember only be used if your program is packed into a .jar file.
I haven't had any problems with using getResource, but I have only been packing my mini game into a .jar file. Is there another method of this I seem to be unaware of?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.