Why Are My Images Not Loading in Java Swing?

In summary: If he does that without commenting out the other pair of ImageIcon instances, he will have more problems.
  • #1
UltimateSomni
62
0
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 );
add(pnl);

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
 
Technology news on Phys.org
  • #2
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.
 
  • #3
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:
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:
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:
  • #4
Code:
//ImageIcon tick = new ImageIcon( "tick.png" );
//ImageIcon cross = new ImageIcon( "cross.png" );

erasing that "//" maybe going to solve your problem .
 
  • #5
kevinfrankly said:
Code:
//ImageIcon tick = new ImageIcon( "tick.png" );
//ImageIcon cross = new ImageIcon( "cross.png" );

erasing that "//" maybe going to solve your problem .
If he does that without commenting out the other pair of ImageIcon instances, he will have more problems.
 
  • #6
I like Serena said:
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.
This works
 
  • #7
Borg said:
If he does that without commenting out the other pair of ImageIcon instances, he will have more problems.

wow, thanks for your correction . so the only problem is because the image (.png) isn't in the class directory, isn't it ?
 

Related to Why Are My Images Not Loading in Java Swing?

What is Java and how does it relate to pictures not showing up?

Java is a programming language commonly used to create dynamic and interactive content on websites. It is often used to display images and other media on web pages. If pictures are not showing up on a website, it could be due to a Java-related issue.

Why are pictures not showing up on my website?

There could be several reasons for pictures not showing up on a website. It could be due to incorrect coding, a server error, slow internet connection, or a problem with the image file itself. It is important to troubleshoot and identify the specific issue in order to fix it.

How can I fix pictures not showing up on my website?

The solution to fixing pictures not showing up on a website depends on the root cause of the issue. If it is a coding error, it must be corrected. If it is a server error, the server must be fixed or the images must be hosted on a different server. If it is a slow internet connection, the images may take longer to load. If the image file itself is corrupted, it will need to be replaced with a working file.

Can a browser affect pictures not showing up on a website?

Yes, the browser being used can affect whether pictures are showing up on a website or not. Some browsers may have compatibility issues with certain types of images or may have security settings that block certain images. It is recommended to test the website on different browsers to ensure compatibility.

Is there a way to prevent pictures from not showing up on my website?

While it is impossible to completely prevent pictures from not showing up on a website, there are some steps that can be taken to minimize the chances. This includes using proper coding techniques, hosting images on a reliable server, optimizing image file sizes, and regularly testing the website on different browsers.

Similar threads

  • Programming and Computer Science
Replies
14
Views
4K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
14
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
4K
Back
Top