用netbeans 4.0
在执行startApp()后抛错,错误如下:
startApp threw an Exception
java.lang.NullPointerException
java.lang.NullPointerException
at ImageItemDemo.startApp(+6)
at javax.microedition.midlet.MIDletProxy.startApp(+7)
at com.sun.midp.midlet.Scheduler.schedule(+270)
at com.sun.midp.main.Main.runLocalClass(+28)
at com.sun.midp.main.Main.main(+116)
我觉得可能是图片载入出错了,可是也没有执行catch,图片在/res 和 /src 里面都有,应该不是路径问题,一下午也没找到,代码基本是按书上写的,怎么就报错了呐?
源代码如下:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* @ImageItem的例子
* @author mark
* @version
*/
public class ImageItemDemo extends MIDlet implements CommandListener{
private Command exitCommand=new Command("Exit",Command.EXIT,1);
private Form mainform;
private ImageItem img1,img2;
public void ImageItemDemo(){
mainform=new Form("ImageItem Test");
}
public void startApp() {
mainform.append("显示各种ImageItem");
try{
img1=new ImageItem("默认图像控件",
Image.createImage("/Image.png"),
Item.LAYOUT_DEFAULT,
"第一张图片");
img2=new ImageItem("默认图像控件",
Image.createImage("/Image.png"),
Item.LAYOUT_2|Item.LAYOUT_EXPAND|Item.LAYOUT_NEWLINE_BEFORE,
"第二张图片");
}catch(Exception err){
System.out.println("can not load....");
}
mainform.append(img1);
mainform.append(img2);
mainform.addCommand(exitCommand);
mainform.setCommandListener(this);
Display.getDisplay(this).setCurrent(mainform);