Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Pictures not showing up (Java)

  1. Jun 29, 2011 #1
    The error: "Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:167)
    at Buttons.<init>(Windows.java:12)
    at Buttons.main(Windows.java:38)
    Java Result: 1"

    The Windows.java code:

    import javax.swing.*;

    class Buttons extends JFrame
    JPanel pnl = new JPanel();

    ClassLoader ldr = this.getClass().getClassLoader();

    java.net.URL tickURL = ldr.getResource("tick.png");
    java.net.URL crossURL = ldr.getResource("cross.png");

    ImageIcon tick = new ImageIcon( tickURL ); //LINE 12
    ImageIcon cross = new ImageIcon( crossURL );

    //ImageIcon tick = new ImageIcon( "tick.png" );
    //ImageIcon cross = new ImageIcon( "cross.png" );

    JButton btn = new JButton( "Click Me" );
    JButton tickBtn = new JButton( tick );
    JButton crossBtn = new JButton( "STOP", cross );

    public Buttons()
    super("Swing Window");
    setSize( 500,200 );
    setDefaultCloseOperation( EXIT_ON_CLOSE );

    pnl.add( btn );
    pnl.add( tickBtn );
    pnl.add( crossBtn );

    setVisible( true );

    public static void main ( String[] args )
    Buttons gui = new Buttons();//LINE 38

    and line 167 of ImageIcon.java looks like this :

    public ImageIcon (URL location) {
    this(location, location.toExternalForm()); //line 167


    according to the book this should work
  2. jcsd
  3. Jun 29, 2011 #2

    I like Serena

    User Avatar
    Homework Helper

    Hi UltimateSomni! :smile:

    It means that the program can't find the .png images.

    Put tick.png and cross.png into the directory where you run your program and you should be fine.
  4. Jun 30, 2011 #3


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    You should use the commented out ImageIcon constructors that you created instead of the the 5 lines of code where you're trying to pass java.net.URL instances into ImageIcon.
    Code (Text):
    ImageIcon tick = new ImageIcon( "tick.png" );
    ImageIcon cross = new ImageIcon( "cross.png" );
    If you're not sure where the program is trying to find your images, use this after your super() call:
    Code (Text):
    System.out.println("tick: " + tick);
    System.out.println("cross : " + cross);
    That will print out the complete path where the program is looking for the images. If your images aren't in the directory where the program is looking, Java will just create an empty image which is why you won't see anything.

    Finally, you should put the add(pnl); call after you add all of the buttons to the pnl object. It doesn't break in this case but it could cause problems.
    Last edited: Jun 30, 2011
  5. Jun 30, 2011 #4
    Code (Text):
    //ImageIcon tick = new ImageIcon( "tick.png" );
    //ImageIcon cross = new ImageIcon( "cross.png" );
    erasing that "//" maybe gonna solve your problem .
  6. Jun 30, 2011 #5


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    If he does that without commenting out the other pair of ImageIcon instances, he will have more problems.
  7. Jun 30, 2011 #6
    This works
  8. Jun 30, 2011 #7
    wow, thanks for your correction . so the only problem is because the image (.png) isnt in the class directory, isnt it ?
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook