所以我导入了一张图片作为背景,出于某种原因它给了我:
Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(Unknown Source)
at sun.awt.image.URLImageSource.getDecoder(Unknown Source)
at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
at sun.awt.image.ImageFetcher.run(Unknown Source)
有人可以帮助我吗?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class PixelLegendsMain extends JFrame implements ActionListener{
public void actionPerformed(ActionEvent e){
}
public static void main(String[ ] args)throws Exception{
PixelLegendsMain plMain = new PixelLegendsMain();
arenaBuild arena = new arenaBuild();
plMain.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
plMain.add(arena);
plMain.setSize(600,460);;
plMain.setVisible(true);
plMain.setResizable(false);
plMain.setLocation(200, 200);
}
}
这是主要课程,这是:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
import java.awt.Graphics;
import java.net.URL;
import java.io.*;
import javax.swing.Timer;
public class arenaBuild extends JPanel{
String picPath = "pictures/";
String[] fileName = {picPath+"stageBridge.png", picPath+"turret.png"};
ClassLoader cl = arenaBuild.class.getClassLoader();
URL imgURL[] = new URL[2];
Toolkit tk = Toolkit.getDefaultToolkit();
Image imgBG;
public arenaBuild()throws Exception{
for (int x=0;x<2;x++){
imgURL[x]= cl.getResource(picPath+fileName[x]);
}
imgBG = tk.createImage(imgURL[0]);
}
public void paintComponent(Graphics g){
g.drawImage(imgBG,0,0,600,460,0,0,600,460, this);
}
}
Thjis 是我调用图像的地方。我是新手,所以如果有人能解释为什么会发生这种情况并帮助我修复它,我将不胜感激:D