Results 1 to 10 of 10
  1. #1
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default BuferedImage nullPointerException

    Hey, I'm trying to load in a sprite sheet and cut it up into sub images for my game. When I run it I get a null pointer exception, I've commented in where it breaks.
    The for loops only run once before I get the error. I'm probably being really dumb but I have no idea why this is giving me this exception. Sorry if I'm asking too much lol.

    Java Code:
    public class Resources {
    	private static final int spriteSheetHeight = 1750;
    	private static final int spriteSheetCellHeight = 125;
    	private static final int spriteSheetWidth = 1750;
    	private static final int spriteSheetCellWidth = 125;
    	private static final int rows = ((int)spriteSheetHeight/spriteSheetCellHeight);
    	private static final int cols = ((int)spriteSheetWidth/spriteSheetCellWidth);
    
    	private static BufferedImage spriteSheet;
    	private static BufferedImage [][] cells = new BufferedImage[rows][cols];
    
    	public static BufferedImage sprite_playerBottom;
    	public static BufferedImage sprite_playerTurret;
    
    
    	static void loadSprites(){
    		try {
    			spriteSheet = ImageIO.read(new File("Resources/spriteSheet.png"));
    		} catch (IOException e) {
    			System.out.println("Sprite Sheet could not be read!");
    			System.exit(1);
    		}
    		
    		for (int row = 0; row < rows - 1;row++){ // for each row
    			for (int col = 0; col<cols; col++){ // for each column
    				
    				cells[row][col] = spriteSheet.getSubimage(col*spriteSheetCellWidth, row*spriteSheetCellHeight, spriteSheetCellWidth, spriteSheetCellHeight); // I get a null pointer exception here
    
    			}
    		}
    
    		//Spritesheet constants declared here.
    		sprite_playerBottom = cells[0][0];
    		sprite_playerTurret = cells[1][0];
    
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,254
    Rep Power
    19

    Default Re: BuferedImage nullPointerException

    Add a line just before the line that's giving rise to the NPE"
    Java Code:
    System.out.println("spriteSheet is null? " + (spriteSheet == null));
    What do you get?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default Re: BuferedImage nullPointerException

    It says it is null. Why could this be, there is no IOException so surely it must be loading the file fine?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,254
    Rep Power
    19

    Default Re: BuferedImage nullPointerException

    Have you read the API for ImageIO#read(File)?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default Re: BuferedImage nullPointerException

    "If no registered ImageReader claims to be able to read the resulting stream, null is returned."
    Is this what is happening? If so I don't understand why and what I can do about it lol :/

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: BuferedImage nullPointerException

    Let's first try to simplify the problem. Try to get a most basic program to work without all of the other code, and once this is solved, add the code to your main program. For instance, see if you can get something like this to work:

    Java Code:
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class SimpleImageProg {
       private static final String IMAGE_PATH = "Resources/spriteSheet.png";
    
       public static void main(String[] args) {
          File imageFile = new File(IMAGE_PATH);
          try {
             BufferedImage image = ImageIO.read(imageFile );
             System.out.println("is image null? " + (image == null));
             ImageIcon icon = new ImageIcon(image);
             JLabel label = new JLabel(icon);
             JOptionPane.showMessageDialog(null, label);
          } catch (IOException e) {
             e.printStackTrace();
          }
       }
    }

  7. #7
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default Re: BuferedImage nullPointerException

    Its still not working :(
    gets a npe at ImageIcon icon = new ImageIcon(image);

    is image null? true
    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:204)
    at tests.SimpleImageProg.main(SimpleImageProg.java:15 )

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: BuferedImage nullPointerException

    OK, what if you add the following to the code above?

    Java Code:
       public static void main(String[] args) {
          System.out.println("User dir: " + System.getProperty("user.dir"));
          
          File imageFile = new File(IMAGE_PATH);
          
          System.out.println("File dir: " + imageFile.getAbsolutePath());
          
          try {
             BufferedImage image = ImageIO.read(imageFile );
    
              // ... etc ...
    What output do you see, and does it match your expectations? Is your image file in the expected location?
    Also, what if you try a different image file? Perhaps the one you're trying is somehow corrupted.

  9. #9
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default Re: BuferedImage nullPointerException

    It seems both of the images I used to test this were corrupted. I don't know how it happened, but thank you very much :)

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: BuferedImage nullPointerException

    Quote Originally Posted by Guy View Post
    It seems both of the images I used to test this were corrupted. I don't know how it happened, but thank you very much :)
    Aha! If there's a lesson to be learned from this, it's to try as many different trouble-shooting techniques as you can. Glad you've got things fixed!

Similar Threads

  1. I keep getting NullPointerException
    By coasterguy10 in forum New To Java
    Replies: 6
    Last Post: 09-19-2011, 05:53 PM
  2. NullPointerException
    By Diz in forum New To Java
    Replies: 10
    Last Post: 05-13-2011, 02:58 AM
  3. NullPointerException
    By Juuno in forum New To Java
    Replies: 1
    Last Post: 02-11-2010, 05:43 PM
  4. NullPointerException help?
    By fab5freddy in forum New To Java
    Replies: 2
    Last Post: 02-04-2010, 08:26 PM
  5. NullPointerException I NEED HELP
    By mayhewj7 in forum New To Java
    Replies: 2
    Last Post: 02-13-2009, 08:03 AM

Tags for this Thread

Posting Permissions

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