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.


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

Results 1 to 2 of 2

Thread: Exception Posting Image.

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Thanked 0 Times in 0 Posts

    Default Exception Posting Image.

    Hi I am new to all of this and am just trying to simple add an Image but I get this error:

    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:181)
    at bardejov.board.<init>(board.java:26)
    at bardejov.Bardejov.<init>(Bardejov.java:17)
    at bardejov.Bardejov.main(Bardejov.java:31)

    this is my code:

    package bardejov;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
    public class board extends JPanel
        Image bardejov;
        public board() 
            ImageIcon ii = new ImageIcon(this.getClass().getResource("zombiemenu.jpg"));
            bardejov = ii.getImage();
        public void paint(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.drawImage(bardejov, 10, 10, null); 


    package bardejov;
    import javax.swing.JFrame;
    public class Bardejov extends JFrame
         public Bardejov() 
            add(new board());
            setSize(280, 240);
         * @param args the command line arguments
        public static void main(String[] args)
            new Bardejov(); 

    Any Help would be appreciated.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,618 Times in 2,568 Posts

    Default Re: Exception Posting Image.

    What code is at line 26: bardejov.board.<init>(board.java:26)
    What folder is the image file in?
    What folder is the .class file in?
    Your program is in a package. The getClass().getResource() uses that as part of the path to find the image.

    How are you executing your program? In a console window with the java command?

    If you are using an IDE, then you'll need to ask someone how to configure your IDE.

  3. The Following User Says Thank You to Norm For This Useful Post:

    patKevin (July 25th, 2011)

Similar Threads

  1. send image file from java to php broken pipe exception occured
    By ashi123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 21st, 2011, 02:53 PM
  2. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM
  3. Replies: 2
    Last Post: February 14th, 2011, 05:36 PM

Tags for this Thread