小弟写了一个applet,是想实现对一幅图进行猜价游戏,界面很简单,图片我也是放在确定正确的地方了,但是getCodeBase()老是出现异常,小弟加了一些出错判断的语句,具体异常和代码如下:
代码:
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.awt.event.*;
public class Lucky extends Applet implements ActionListener {
Panel pNorth = new Panel();
PicPanel pCenter = new PicPanel();
Panel pBottom = new Panel();
TextField txfield = new TextField(10);
Label label = new Label("请开始游戏,按确定开始:");
private int nPrice = 1100;
private Image m_img;
private boolean t = false;
public Lucky() {
super();
this.setLayout(new BorderLayout());
pNorth.add(label);
pNorth.add(txfield);
add(pNorth, BorderLayout.NORTH);
add(pCenter, BorderLayout.CENTER);
Button btnStart = new Button("开始游戏");
Button btnOK = new Button("确定");
Button btnCancel = new Button("取消");
btnOK.setActionCommand("ok");
btnStart.setActionCommand("start");
btnCancel.setActionCommand("cancel");
btnOK.addActionListener(this);
btnStart.addActionListener(this);
btnCancel.addActionListener(this);
pBottom.add(btnStart);
pBottom.add(btnOK);
pBottom.add(btnCancel);
add(pBottom, BorderLayout.SOUTH);
setBackground(Color.white);
String filename = "Image.gif";
try {
System.out.println(getCodeBase());
m_img = getImage(getCodeBase(), "Image.gif");
} catch (Exception e) {
System.out.println("No Picture!");
e.printStackTrace();
}
MediaTracker mt = new MediaTracker(this);
mt.addImage(m_img, 0);
try {
mt.waitForID(0);
t = mt.checkAll();
} catch (Exception e) {
e.printStackTrace();
System.out.println();
System.out.println(t);
System.out.println("Loading Error!");
}
repaint();
}
public void init() {
}
public void paint(Graphics g) {
System.out.println("Begin Drawing!");
g.drawImage(m_img, 125, 0, 50, 160, this);
}
public void actionPerformed(ActionEvent evt) {
if (evt.getActionCommand().equals("start")) {
pCenter.initImg();
label.setText("请输入商品估价");
pCenter.repaint();
} else if (evt.getActionCommand().equals("ok")) {
int guessprice = 0;
try {
guessprice = Integer.parseInt(txfield.getText().trim());
String guess = comparePrice(guessprice);
new MsgDlg(guess);
} catch (Exception e) {
e.printStackTrace();
}
} else if (evt.getActionCommand().equals("cancel")) {
txfield.setText("");
}
}
public String comparePrice(int guessPrice) {
if (guessPrice == nPrice) {
return "答对了";
} else if (guessPrice > nPrice) {
return "估价过高!";
} else {
return "估价过低!";
}
}
}
class PicPanel extends Panel {
private Image m_img;
public void initImg() {
}
}
class MsgDlg extends Frame implements ActionListener {
Label label = new Label();
public MsgDlg(String strMsg) {
super();
setTitle("猜的结果");
Panel p = new Panel();
add(p);
p.add(label);
label.setText(strMsg);
setSize(150, 100);
setLocation(300, 200);
Button btnOK = new Button("确定");
btnOK.addActionListener(this);
p.add(btnOK);
show();
}
public void actionPerformed(ActionEvent evt) {
this.dispose();
}
}
异常如下:
java.lang.NullPointerException
at java.applet.Applet.getCodeBase(Applet.java:136)
at lucky.Lucky.<init>(Lucky.java:55)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:274)
at java.lang.Class.newInstance0(Class.java:308)
No Picture!//这句是自己加的出错提示
at java.lang.Class.newInstance(Class.java:261)
at com.borland.jbuilder.runtime.applet.AppletTestbed.a(Unknown Source)
at com.borland.jbuilder.runtime.applet.AppletTestbed.startApplet(Unknown Source)
at com.borland.jbuilder.runtime.applet.AppletTestbed.main(Unknown Source)
java.lang.NullPointerException
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2718)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2679)
at lucky.Lucky.paint(Lucky.java:83)
at sun.awt.RepaintArea.paint(RepaintArea.java:177)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:260)
at java.awt.Component.dispatchEventImpl(Component.java:3678)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
Begin Drawing!//这句也是自己加的判断
8 个解决方案
#1
将构造方法中的代码移到init()方法中(super()除外)
#2
你写的代码好乱啊,用什么工具写的啊
因多写一点的注释!!!!记得喔
因多写一点的注释!!!!记得喔
#3
Applet的生命周期是从 inti() 方法 初始化开始 然后再自动调用start() 方法,
所以你应该把你的构造方法 放在 inti() 里面覆盖它
所以你应该把你的构造方法 放在 inti() 里面覆盖它
#4
NullPointerException初始化的问题了
#5
空指针异常,你的55,83行看看
#6
可能文件名大小写的问题image.gif
#7
谢谢yaray和learlee,问题已经解决,分也给了,再次谢谢
#8
但是我想问问为什么把代码放在构造函数中就会出错呢?
Applet启动的顺序不是:构造函数-》init()-》paint()-》start()么?
放在构造函数中为什么就会有Nopoint的异常?
Applet启动的顺序不是:构造函数-》init()-》paint()-》start()么?
放在构造函数中为什么就会有Nopoint的异常?
#1
将构造方法中的代码移到init()方法中(super()除外)
#2
你写的代码好乱啊,用什么工具写的啊
因多写一点的注释!!!!记得喔
因多写一点的注释!!!!记得喔
#3
Applet的生命周期是从 inti() 方法 初始化开始 然后再自动调用start() 方法,
所以你应该把你的构造方法 放在 inti() 里面覆盖它
所以你应该把你的构造方法 放在 inti() 里面覆盖它
#4
NullPointerException初始化的问题了
#5
空指针异常,你的55,83行看看
#6
可能文件名大小写的问题image.gif
#7
谢谢yaray和learlee,问题已经解决,分也给了,再次谢谢
#8
但是我想问问为什么把代码放在构造函数中就会出错呢?
Applet启动的顺序不是:构造函数-》init()-》paint()-》start()么?
放在构造函数中为什么就会有Nopoint的异常?
Applet启动的顺序不是:构造函数-》init()-》paint()-》start()么?
放在构造函数中为什么就会有Nopoint的异常?