nullpointerexception when declaring new ImageIcon

2019-07-24 10:59发布

问题:

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)

回答1:

Your badgePath is null.

The constructor uses the badgePath as argument for the ImageIcon constructor, but it didn't initialize it first. Use a constructor like this:

public badgeIMG(String path)
{
    ImageIcon ii = new ImageIcon(this.getClass().getClassLoader().getResource(path));
    badgeIcon = ii.getImage();
    badgePath = path;
}

Note: Very important: Java naming conventions are that classes start with an uppercase char. So change the class name and file name to: BadgeImg or BadgeIMG.



回答2:

this.getClass().getClassLoader().getResource(badgePath))

Your badgePath is null. Please initialize it with image path.

When you trying to have an image , you should give a path. Right now it is null. Giving a path in deceleration or assigning a value to it before using it resolves your issue.

  String badgePath="some/valid/path";