The full problem:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:181)
at rtype.Craft.<init>(Craft.java:19)
at rtype.Board.<init>(Board.java:27)
at rtype.RType.<init>(RType.java:9)
at rtype.RType.main(RType.java:20)
My Code:
Craft.Java package rtype; import java.awt.Image; import java.awt.event.KeyEvent; import javax.swing.ImageIcon; public class Craft { private String craft = "/users/gregorystedman/images/craft.png"; private int dx; private int dy; private int x; private int y; private Image image; public Craft() { ImageIcon ii = new ImageIcon(this.getClass().getResource(craft)); 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_LEFT) { dx = -1; } if (key == KeyEvent.VK_RIGHT) { dx = 1; } if (key == KeyEvent.VK_UP) { dy = -1; } if (key == KeyEvent.VK_DOWN) { dy = 1; } } public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_LEFT) { dx = 0; } if (key == KeyEvent.VK_RIGHT) { dx = 0; } if (key == KeyEvent.VK_UP) { dy = 0; } if (key == KeyEvent.VK_DOWN) { dy = 0; } } }
Board.java package rtype; 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 Board extends JPanel implements ActionListener { private Timer timer; private Craft craft; public Board() { addKeyListener(new TAdapter()); setFocusable(true); setBackground(Color.BLACK); setDoubleBuffered(true); craft = new Craft(); timer = new Timer(5, this); timer.start(); } public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D)g; g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(), this); Toolkit.getDefaultToolkit().sync(); g.dispose(); } public void actionPerformed(ActionEvent e) { craft.move(); repaint(); } private class TAdapter extends KeyAdapter { public void keyReleased(KeyEvent e) { craft.keyReleased(e); } public void keyPressed(KeyEvent e) { craft.keyPressed(e); } } }
RType.java package rtype; import javax.swing.JFrame; public class RType extends JFrame { public RType() { add(new Board()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 300); setLocationRelativeTo(null); setTitle("R - Type"); setResizable(false); setVisible(true); } public static void main(String[] args) { new RType(); } }