我有这样一个java程序希望大家帮我看看错误在哪里?
packageexample6;importjava.awt.*;importjavax.swing.*;publicclassdemo1{JFramejf=null;M...
package example6;
import java.awt.*;
import javax.swing.*;
public class demo1 {
JFrame jf=null;
MyPanel mypanel=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
demo1 de=new demo1();
}
public demo1(){
mypanel=new MyPanel();
jf=new JFrame();
jf.add(mypanel);
jf.setSize(300, 300);
jf.setLocation(300, 300);
jf.setVisible(true);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setResizable(true);
}
}
//定义一个
class MyPanel extends Panel{
public void paint(Graphics g){
//这句话不能少
super.paint(g);//调用父类函数完成初始化任务
//在面板上画出图片
Image im=Toolkit.getDefaultToolkit().
getImage(Panel.class.getResource
("C:/Users/Administrator/Desktop/nihaob.jpg"));
g.drawImage(im, 50, 50, 250,200,this);
}
}
系统提示的显示的错误信息是:
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 java.awt.*;
import javax.swing.*;
public class demo1 {
JFrame jf=null;
MyPanel mypanel=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
demo1 de=new demo1();
}
public demo1(){
mypanel=new MyPanel();
jf=new JFrame();
jf.add(mypanel);
jf.setSize(300, 300);
jf.setLocation(300, 300);
jf.setVisible(true);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setResizable(true);
}
}
//定义一个
class MyPanel extends Panel{
public void paint(Graphics g){
//这句话不能少
super.paint(g);//调用父类函数完成初始化任务
//在面板上画出图片
Image im=Toolkit.getDefaultToolkit().
getImage(Panel.class.getResource
("C:/Users/Administrator/Desktop/nihaob.jpg"));
g.drawImage(im, 50, 50, 250,200,this);
}
}
系统提示的显示的错误信息是:
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)
我的图片是放在桌面上的 展开
3个回答
展开全部
哦,空指针异常,你把赋值赋错了。自己看一下。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询