Results 1 to 3 of 3
  1. #1
    manudude03 is offline Member
    Join Date
    Oct 2014
    Rep Power

    Default BufferedImage.getSubImage returns a NullPointerException

    Java Code:
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;
    public class Character {
    	private int x;
    	private int y;
    	private int z;
    	private Animation sprite;
    	private String name;
    	private int HPcurrent;
    	private int HPmax;
    	private int MPcurrent;
    	private int MPmax;
    	private int strength;
    	private int defense;
    	private int magic;
    	private int agility;
    	private int accuracy;
    	private int magicDefense;
    	private int evasion;
    	private int luck;
    	private int map;
    	private boolean mainChar;
    	public Character(String name, String file, int HPmax, int MPmax, int strength, int defense, int magic, int magicDefense, int agility, int accuracy, int evasion, int luck, int map, int x, int y, int z, boolean mainChar)
    		try {
    			this.setSprite(new Animation(file));
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    	public int getX() {
    		return x;
    	public void setX(int x) {
    		this.x = x;
    	public int getY() {
    		return y;
    	public void setY(int y) {
    		this.y = y;
    	public int getZ() {
    		return z;
    	public void setZ(int z) {
    		this.z = z;
    	public int getHPcurrent() {
    		return HPcurrent;
    	public void setHPcurrent(int hPcurrent) {
    		HPcurrent = hPcurrent;
    	public int getHPmax() {
    		return HPmax;
    	public void setHPmax(int hPmax) {
    		HPmax = hPmax;
    	public int getMPcurrent() {
    		return MPcurrent;
    	public void setMPcurrent(int mPcurrent) {
    		MPcurrent = mPcurrent;
    	public int getMPmax() {
    		return MPmax;
    	public void setMPmax(int mPmax) {
    		MPmax = mPmax;
    	public int getStrength() {
    		return strength;
    	public void setStrength(int strength) {
    		this.strength = strength;
    	public int getDefense() {
    		return defense;
    	public void setDefense(int defense) {
    		this.defense = defense;
    	public int getMagic() {
    		return magic;
    	public void setMagic(int magic) {
    		this.magic = magic;
    	public int getAgility() {
    		return agility;
    	public void setAgility(int agility) {
    		this.agility = agility;
    	public int getAccuracy() {
    		return accuracy;
    	public void setAccuracy(int accuracy) {
    		this.accuracy = accuracy;
    	public int getMagicDefense() {
    		return magicDefense;
    	public void setMagicDefense(int magicDefense) {
    		this.magicDefense = magicDefense;
    	public int getEvasion() {
    		return evasion;
    	public void setEvasion(int evasion) {
    		this.evasion = evasion;
    	public int getLuck() {
    		return luck;
    	public void setLuck(int luck) {
    		this.luck = luck;
    	public String getName() {
    		return name;
    	public void setName(String name) { = name;
    	public Animation getSprite() {
    		return sprite;
    	public void setSprite(Animation sprite) {
    		this.sprite = sprite;
    	public int getMap() {
    		return map;
    	public void setMap(int map) { = map;
    	public Image getSpriteImage()
    		return this.sprite.getImage();
    	private class Animation
    		private Image[] images;
    		private int id;
    		public Animation(String file) throws IOException
    			BufferedImage spritesheet = File(file));
    			int height=spritesheet.getHeight();
    			int width=spritesheet.getWidth();
    			for (int i=0; i<4; i++)
    				for (int j=0; j<4; j++)
    					System.out.println("spritesheet is null? " + (spritesheet == null));
    					// program crashes here
    					this.images[i*4+j]=spritesheet.getSubimage(width*j/4, height*i/4, width/4, height/4);
    		public Image[] getImages() {
    			return images;
    		public void setImages(Image[] images) {
    			this.images = images;
    		public int getId() {
    			return id;
    		public void setId(int id) { = id;
    		public Image getImage(int index)
    			return images[index];
    		public Image getImage()
    			return images[id];
    Above is my Character class for a game I'm making, but I am somewhat puzzled by what is going wrong here. When I run the code, the console prints:

    spritesheet is null? false
    Exception in thread "main" java.lang.NullPointerException
    at Character$Animation.<init>(
    at Character.<init>(
    at Game.init(
    at Main.main(
    How can I get a NPE if the spritesheet isn't null?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: BufferedImage.getSubImage returns a NullPointerException

    You didn't initialize the images arrray so images is null.

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Rep Power

    Default Re: BufferedImage.getSubImage returns a NullPointerException

    This is a hidden case of blaming it on the tools.

    The method doesn't 'return' a NullPointerException, your code causes a NullPointerException.

    The exception you posted tells you exactly that. The top-level item on the stack trace is this:

    Java Code:
    at Character$Animation.<init>(
    See? That's your code, there is no mention of BufferedImage.getSubImage() in there. As such, the exception did not happen there.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 13
    Last Post: 07-10-2014, 03:28 PM
  2. Changing Canvas Returns With NullPointerException
    By GrimWinter in forum AWT / Swing
    Replies: 3
    Last Post: 06-22-2014, 05:19 AM
  3. getSubimage throws a NullPointerException
    By LousyPanda in forum New To Java
    Replies: 4
    Last Post: 08-23-2013, 12:06 PM
  4. BufferedImage not going to GC
    By vspin in forum New To Java
    Replies: 11
    Last Post: 07-03-2012, 09:42 AM
  5. BufferedImage through FTP
    By dudejonne in forum New To Java
    Replies: 7
    Last Post: 11-05-2009, 05:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts