I am trying to load multiple images on the same window, so to prevent a lot of copy and paste, I made an image class called badgeIMG that looks like this:
package BattleSim;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class badgeIMG extends JPanel{
Image badgeIcon;
String badgePath;
int x = 0;
int y = 0;
public badgeIMG() {
ImageIcon ii = new ImageIcon(this.getClass().getClassLoader().getResource(badgePath));
badgeIcon = ii.getImage();
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(badgeIcon, x, y, null);
}
}
Then in another class, called badgeSelectionWindow, I have this piece of code:
badgeIMG allOrNothingBadge = new badgeIMG();
badgeIMG closeCall = new badgeIMG();
allOrNothingBadge.badgePath = "/Badges/allornothing.gif";
allOrNothingBadge.x = 128;
allOrNothingBadge.y = 144;
closeCall.badgePath = "/Badges/closecall.gif";
closeCall.x = 256;
closeCall.y = 144;
add(allOrNothingBadge);
add(closeCall);
The problem is, I get a NullPointerException when declaring a badgePath from the above code, but when I put replace badgePath with one of the real file paths, it does not give me error, but I want to be able to plug in a String with the file path and have it display multiple images. Any ideas?
Here is the error:
Exception in thread "main" java.lang.NullPointerException
at sun.misc.MetaIndex.mayContain(Unknown Source)
at sun.misc.URLClassPath$JarLoader.getResource(Unknown Source)
at sun.misc.URLClassPath.getResource(Unknown Source)
at sun.misc.URLClassPath.getResource(Unknown Source)
at java.lang.ClassLoader.getBootstrapResource(Unknown Source)
at java.lang.ClassLoader.getResource(Unknown Source)
at java.lang.ClassLoader.getResource(Unknown Source)
at BattleSim.badgeIMG.<init>(badgeIMG.java:17)
at BattleSim.badgeSelectionWindow.<init>(badgeSelectionWindow.java:11)
at BattleSim.badgeSelectionWindow.main(badgeSelectionWindow.java:36)