I'm making a JPanel for a game. I want the background to be an imported image, so I set up a ClassLoader to import the image and converted that to a URL. However, that URL appears to be null, as when I try to put the panel into a JFrame, no image is drawn and a NullPointerException is thrown. Now I have an if statement to avoid that in the future, but I am mystified as to why the URL would be null in the first place. Any help would be appreciated!
import java.awt.*;
import java.net.URL;
public class StartPanel extends JPanel
{
private ClassLoader loader=StartPanel.class.getClassLoader();
private Toolkit tools=Toolkit.getDefaultToolkit();
private URL startBackground=loader.getResource("Sword.PNG");
public StartPanel()
{
}
@ Override public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(startBackground!=null)
{
Image startBackgroundi=tools.createImage(startBackground);
g.drawImage(startBackgroundi, 0, 0, 20, 30, this);
}
}
}