Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Exception in thread "main" java.lang.NullPointerException

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exception in thread "main" java.lang.NullPointerException

    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();
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,604
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    There is a problem with a null value on line 19. What is null on that line?
    Separate the call to ImageIcon into separate statements and print out the results returned by the geResource() method to see its value.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: December 7th, 2011, 01:03 AM
  2. Replies: 1
    Last Post: October 31st, 2011, 06:19 AM
  3. Exception in thread "main" java.lang.NullPointerException
    By manzili in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 9th, 2011, 12:02 PM
  4. Replies: 7
    Last Post: May 30th, 2011, 09:11 AM
  5. Please help! Exception in thread "main" java.lang.NullPointerException
    By Arutha2321 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 18th, 2009, 01:25 AM