Results 1 to 6 of 6
  1. #1
    Jaeela is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default NullPointerException on Graphics.drawImage

    Well to try to get used to the concept of images, I am trying to make a program to run the game 21. I have tried to fix this error, but i can't figure it out. Thanks in advance.
    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at src.ImagePanel.paintComponent(ImagePanel.java:23)
    	at src.P1.<init>(P1.java:32)
    	at src.P1.main(P1.java:59)
    and here is my code for ImagePanel:
    Java Code:
    package src;
    
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;
    
    public class ImagePanel extends JPanel{
    
        public BufferedImage image;
    
        public ImagePanel(String a) throws IOException {                
               InputStream is = new BufferedInputStream(new FileInputStream(a));
        	   image = ImageIO.read(is);
        }
        public ImagePanel() {
    	
    	}
    
    	public void paintComponent(Graphics g) {
    		if (image != null) {
    		g.drawImage(image, 0, 0, null);
    		}
        }
    
    }
    and here is my code for P1:

    Java Code:
    package src;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.io.File;
    import java.io.IOException;
    
    import javax.swing.*;
    
    public class P1 extends JFrame {
       public static JPanel panel_1;
       public static JPanel panel_2;
       public static JPanel panel_3;
       public static JButton pickCard;
       public static JLabel score;
       public static JLabel cardsLeft;
       public static JButton button_1;
       public static Graphics g;
        public P1() throws IOException {
            mainguiLayout customLayout = new mainguiLayout();
    
            getContentPane().setFont(new Font("Helvetica", Font.PLAIN, 12));
            getContentPane().setLayout(customLayout);
    
            panel_1 = new ImagePanel("src/src/Game21Images/greenfelt.png");
            getContentPane().add(panel_1);
            //((ImagePanel) panel_1).paintComponent(g);
    
    
            panel_2 = new ImagePanel("src/src/Game21Images/back2.jpg");
            getContentPane().add(panel_2);
            ((ImagePanel) panel_2).paintComponent(g);
    
            panel_3 = new ImagePanel();
            getContentPane().add(panel_3);
    
            pickCard = new JButton("Pick Card");
            getContentPane().add(pickCard);
    
            score = new JLabel("Score: 0");
            getContentPane().add(score);
            cardsLeft = new JLabel("Cards Left: 52");
            getContentPane().add(cardsLeft);
    
            button_1 = new JButton("Instructions");
            getContentPane().add(button_1);
    
            setSize(getPreferredSize());
    
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        }
    
    
    	public static void main(String args[]) throws IOException {
            P1 window = new P1();
    
            window.setTitle("21");
            window.pack();
            window.show();
        }
    }
    
    class mainguiLayout implements LayoutManager {
    
        public mainguiLayout() {
        }
    
        public void addLayoutComponent(String name, Component comp) {
        }
    
        public void removeLayoutComponent(Component comp) {
        }
    
        public Dimension preferredLayoutSize(Container parent) {
            Dimension dim = new Dimension(0, 0);
    
            Insets insets = parent.getInsets();
            dim.width = 639 + insets.left + insets.right;
            dim.height = 358 + insets.top + insets.bottom;
    
            return dim;
        }
    
        public Dimension minimumLayoutSize(Container parent) {
            Dimension dim = new Dimension(0, 0);
            return dim;
        }
    
        public void layoutContainer(Container parent) {
            Insets insets = parent.getInsets();
    
            Component c;
            c = parent.getComponent(0);
            if (c.isVisible()) {c.setBounds(insets.left+0,insets.top+0,464,352);}
            c = parent.getComponent(1);
            if (c.isVisible()) {c.setBounds(insets.left+240,insets.top+48,208,248);}
            c = parent.getComponent(2);
            if (c.isVisible()) {c.setBounds(insets.left+16,insets.top+48,208,248);}
            c = parent.getComponent(3);
            if (c.isVisible()) {c.setBounds(insets.left+496,insets.top+16,104,24);}
            c = parent.getComponent(4);
            if (c.isVisible()) {c.setBounds(insets.left+496,insets.top+80,104,24);}
            c = parent.getComponent(5);
            if (c.isVisible()) {c.setBounds(insets.left+496,insets.top+144,104,24);}
            c = parent.getComponent(6);
            if (c.isVisible()) {c.setBounds(insets.left+496,insets.top+304,104,24);}
        }
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: NullPointerException on Graphics.drawImage

    Don't put null for the final argument, instead try 'this'

  3. #3
    Jaeela is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Re: NullPointerException on Graphics.drawImage

    Still the same error.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: NullPointerException on Graphics.drawImage

    Don't make the call at line 32:
    Java Code:
    ((ImagePanel) panel_2).paintComponent(g);
    The panel will paint itself automatically. Removing this line should work.

  5. #5
    Jaeela is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Re: NullPointerException on Graphics.drawImage

    Thanks so much!! working now:)

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

Similar Threads

  1. DrawImage with transparency ?
    By mrhid6 in forum AWT / Swing
    Replies: 2
    Last Post: 10-02-2011, 12:58 AM
  2. drawImage im 13 plz help
    By pseudohawtcakes in forum New To Java
    Replies: 3
    Last Post: 05-17-2011, 09:07 PM
  3. Drawing a graphics onto another Graphics ?
    By Ziden in forum Java Applets
    Replies: 0
    Last Post: 01-08-2011, 08:30 PM
  4. SWT Graphics Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 06-28-2008, 10:28 PM
  5. graphics
    By Joe2003 in forum Advanced Java
    Replies: 4
    Last Post: 01-18-2008, 08:44 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
  •