Results 1 to 5 of 5
  1. #1
    LousyPanda is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default getSubimage throws a NullPointerException

    I've been trying to get an animation running from a sprite sheet, which currently loads and shows in the JFrame correctly, but as soon as I try to crop the sprite sheet, I get two null exceptions. The code as follows.
    Java Code:
    package newgame;
    
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.URL;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    
    public class Main extends JFrame {
        
        public BufferedImage spriteSheet;
        
        public Main(){
            setSize(800, 600);
            setVisible(true);
            setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            init();
        }
        public void initSpriteSheet() throws IOException {
            URL url = this.getClass().getResource("test.png");
            spriteSheet = ImageIO.read(url);
        }
        
        private void init(){
            try {
                initSpriteSheet();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        Image dbImage;
        Graphics dbg;
        
        @Override
        public void paint(Graphics g){
            dbImage = createImage(getWidth(), getHeight());
            dbg = dbImage.getGraphics();
            paintComponent(dbg);
            g.drawImage(dbImage, 0, 0, null);
        }
        
        public void paintComponent(Graphics g){
            g.drawImage(spriteSheet.getSubimage(0, 0, 20, 20), 100, 100, null);
            repaint();
        }
        
        public static void main(String[] args) {
            Main main = new Main();
        }
    }
    And the errors I get when trying to run:

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at newgame.Main.paintComponent(Main.java:89)
    	at newgame.Main.paint(Main.java:80)
    	at javax.swing.RepaintManager$3.run(RepaintManager.java:807)
    	at javax.swing.RepaintManager$3.run(RepaintManager.java:784)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:784)
    	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:757)
    	at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:706)
    	at javax.swing.RepaintManager.access$1000(RepaintManager.java:62)
    	at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1647)
    	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    	at java.awt.EventQueue.access$200(EventQueue.java:103)
    	at java.awt.EventQueue$3.run(EventQueue.java:694)
    	at java.awt.EventQueue$3.run(EventQueue.java:692)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at newgame.Main.paintComponent(Main.java:89)
    	at newgame.Main.paint(Main.java:80)
    	at javax.swing.RepaintManager$3.run(RepaintManager.java:807)
    	at javax.swing.RepaintManager$3.run(RepaintManager.java:784)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:784)
    	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:757)
    	at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:706)
    	at javax.swing.RepaintManager.access$1000(RepaintManager.java:62)
    	at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1647)
    	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    	at java.awt.EventQueue.access$200(EventQueue.java:103)
    	at java.awt.EventQueue$3.run(EventQueue.java:694)
    	at java.awt.EventQueue$3.run(EventQueue.java:692)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
    Can anyone please help with this as I can't get this working for most of the day. Thanks.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: getSubimage throws a NullPointerException

    A couple of suggestions. First, please include all your pertinent code to ensure the line numbers reflected in the exception match up with your posted code.
    Second, you should not be painting to a JFrame. I suggest you subclass JPanel and add that to JFrame. In your subclass, override paintComponent (not to be confused with your paint component). And don't call repaint within paint or anything paint calls.

    Now, did you check to see if spriteSheet is null?
    If it is not, then check to see if spriteSheet.getSubimage(0, 0, 20, 20) is null.

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

  3. #3
    LousyPanda is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: getSubimage throws a NullPointerException

    Quote Originally Posted by jim829 View Post
    A couple of suggestions. First, please include all your pertinent code to ensure the line numbers reflected in the exception match up with your posted code.
    Second, you should not be painting to a JFrame. I suggest you subclass JPanel and add that to JFrame. In your subclass, override paintComponent (not to be confused with your paint component). And don't call repaint within paint or anything paint calls.

    Now, did you check to see if spriteSheet is null?
    If it is not, then check to see if spriteSheet.getSubimage(0, 0, 20, 20) is null.

    Regards,
    Jim
    Sorry for the line number mismatch, I had to remove the commented code, because it took ridiculous amount of space. Both exceptions occur at the line 50, where the draw command is.

    Neither of them is null, that's why I'm confused. The error also doesn't explain what exactly is null. When only painting the spriteSheet, these errors don't occur and everything runs fine. Any ideas of what could be wrong? Thank you.

    EDIT: I managed to do what you mentioned about the JPanel and that removed the exceptions, but now I can't see neither the spriteSheet nor the cropped part when I try to draw them. Most likely I don't understand how to paint the screen. This is the code that I'm currently using.

    Java Code:
    package newgame;
    
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.URL;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Main extends JPanel {
        
        public BufferedImage spriteSheet;
        JFrame f;
        JPanel p;
        
        public Main(){
            f = new JFrame();
            f.setSize(800, 600);
            f.setVisible(true);
            f.setResizable(false);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setLocationRelativeTo(null);
            p = new JPanel();
            f.getContentPane().add(p);
            init();
        }
        public void initSpriteSheet() throws IOException {
            URL url = this.getClass().getResource("test.png");
            spriteSheet = ImageIO.read(url);
        }
        
        private void init(){
            try {
                initSpriteSheet();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        Image dbImage;
        Graphics dbg;
        
        @Override
        public void paint(Graphics g){
            dbImage = createImage(getWidth(), getHeight());
            dbg = dbImage.getGraphics();
            paintComponent(dbg);
            g.drawImage(dbImage, 0, 0, null);
        }
        
        public void paintComponent(Graphics g){
            g.drawImage(spriteSheet, 100, 100, null);
            //repaint();
        }
        
        public static void main(String[] args) {
            Main main = new Main();
        }
    }
    I didn't understand the part about paintComponent(), does it now paint the screen or does paint() still do it? Tried to move the drawing commands across the functions, but the sprite doesn't show up. Could you please elaborate.
    Last edited by LousyPanda; 08-23-2013 at 02:58 AM.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: getSubimage throws a NullPointerException

    With JPanel and other classes that derive from JComponent you should override paintComponent and not paint. Also, the paint methods tend to be called before you get a chance to read it. So your paintComponent method should check for null values before they are used. Where is createImage and its arguments You defined? You may want to check out Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing) and Lesson: Overview of the Java 2D API Concepts (The Java™ Tutorials > 2D Graphics). In the latter section, working with images should be helpful.

    Regards,
    Jim
    Last edited by jim829; 08-23-2013 at 04:18 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    LousyPanda is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: getSubimage throws a NullPointerException

    Quote Originally Posted by jim829 View Post
    With JPanel and other classes that derive from JComponent you should override paintComponent and not paint. Also, the paint methods tend to be called before you get a chance to read it. So your paintComponent method should check for null values before they are used. Where is createImage and its arguments You defined? You may want to check out Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing) and Lesson: Overview of the Java 2D API Concepts (The Java™ Tutorials > 2D Graphics). In the latter section, working with images should be helpful.

    Regards,
    Jim
    Thank you, I finally got it working with the JPanel. The tutorials were very helpful!

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2013, 07:30 PM
  2. throws
    By java4amanda in forum New To Java
    Replies: 2
    Last Post: 03-29-2012, 10:08 AM
  3. Replies: 1
    Last Post: 11-25-2009, 07:48 AM
  4. Recursive Class throws NullPointerException
    By freeBatjko in forum New To Java
    Replies: 5
    Last Post: 11-03-2009, 10:18 AM
  5. throws
    By jdgallag in forum New To Java
    Replies: 14
    Last Post: 02-11-2009, 02:07 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
  •