JAVA出现异常,Exception in thread "main" java.lang.NullPointerException
package打地鼠;importjava.awt.EventQueue;importjava.awt.event.MouseAdapter;importjava.awt...
package 打地鼠;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;
import javax.swing.*;
public class Shrewmouse extends JFrame implements Runnable{
private static final long serialVersionUID = 8752899717443325480L;
private JLabel[] mouses;
private ImageIcon imgMouse;
ImageIcon img=new ImageIcon();
public static void main(String[] args) {
Shrewmouse frame=new Shrewmouse();
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try{
frame.setVisible(true);
new Thread(frame).start();
}catch (Exception e){
e.printStackTrace();
}
}
});
}
public Shrewmouse()
{
super();
setResizable(false);
getContentPane().setLayout(null);
setTitle("a simple game for shijing");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon img = new ImageIcon(getClass().getResource("background.jpg"));
// 初始化地鼠图片对象
imgMouse = new ImageIcon(getClass().getResource("mouse.png"));
mouses=new JLabel[6];
for(int i=0;i<6;i++){
mouses[i]=new JLabel();
mouses[i].setSize(imgMouse.getIconWidth(),imgMouse.getIconHeight());
mouses[i].addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
Object source=e.getSource();
if(source instanceof JLabel){
JLabel mouse=(JLabel) source;
mouse.setIcon(null);
}
}
});
字数超了后边就不写了。问题就在图片地址跟主函数那里new frame那里
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at 打地鼠.Shrewmouse.<init>(Shrewmouse.java:40)
at 打地鼠.Shrewmouse.main(Shrewmouse.java:17) 展开
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;
import javax.swing.*;
public class Shrewmouse extends JFrame implements Runnable{
private static final long serialVersionUID = 8752899717443325480L;
private JLabel[] mouses;
private ImageIcon imgMouse;
ImageIcon img=new ImageIcon();
public static void main(String[] args) {
Shrewmouse frame=new Shrewmouse();
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try{
frame.setVisible(true);
new Thread(frame).start();
}catch (Exception e){
e.printStackTrace();
}
}
});
}
public Shrewmouse()
{
super();
setResizable(false);
getContentPane().setLayout(null);
setTitle("a simple game for shijing");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon img = new ImageIcon(getClass().getResource("background.jpg"));
// 初始化地鼠图片对象
imgMouse = new ImageIcon(getClass().getResource("mouse.png"));
mouses=new JLabel[6];
for(int i=0;i<6;i++){
mouses[i]=new JLabel();
mouses[i].setSize(imgMouse.getIconWidth(),imgMouse.getIconHeight());
mouses[i].addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
Object source=e.getSource();
if(source instanceof JLabel){
JLabel mouse=(JLabel) source;
mouse.setIcon(null);
}
}
});
字数超了后边就不写了。问题就在图片地址跟主函数那里new frame那里
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at 打地鼠.Shrewmouse.<init>(Shrewmouse.java:40)
at 打地鼠.Shrewmouse.main(Shrewmouse.java:17) 展开
1个回答
展开全部
Shrewmouse初始化出错,
图片初始化错了,可以这样:
Image image = new ImageIcon("graphics/Window2.png").getImage();
图片初始化错了,可以这样:
Image image = new ImageIcon("graphics/Window2.png").getImage();
更多追问追答
追问
我图片跟这个类文件放在同一个文件夹下了,直接写new ImageIcon("mouse.png").getImage();就行了吧。另外Shrewmouse初始化错误应该怎么修改呢。
追答
图片应在src文件夹的同级,可以在src同级创建一个文件夹,我上面的代码就是创建了一个graphics文件夹,
注意是【src文件夹】的同级
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询