日期:2014-05-20  浏览次数:20682 次

编写j2me程序遇到错误,请大家帮帮忙啊?
代码如下:
import   javax.microedition.midlet.MIDlet;
import   javax.microedition.midlet.MIDletStateChangeException;
import   javax.microedition.lcdui.*;
import   java.io.IOException;
import   java.io.InputStream;

public   class   jarbook   extends   MIDlet   {

public   jarbook()   {
super();
//   TODO   自动生成构造函数存根
Form   form   =   new   Form( "Welcome   to   J2ME   's   world! ");
String   s   =   null;
s   =   loadStr( "/a.txt ");
form.append(s);
Display.getDisplay(this).setCurrent(form);
}

protected   void   destroyApp(boolean   arg0)   throws   MIDletStateChangeException   {
//   TODO   自动生成方法存根

}

protected   void   pauseApp()   {
//   TODO   自动生成方法存根

}

protected   void   startApp()   throws   MIDletStateChangeException   {
//   TODO   自动生成方法存根

}

protected   String   loadStr(String   url)
{
String   str   =   null;
//获得本地资源,路径为url
InputStream   is   =   this.getClass().getResourceAsStream(url);
try
{
byte[]   data   =   new   byte[is.available()];
//读取数据
is.read(data);
is.close();
is   =   null;
//转换为字符串
str   =   new   String(data);
}
catch   (IOException   e)  
{
e.printStackTrace();
return   " ";
}
if(str   !=   null)
{
return   str;
}
else
{
return   " ";
}
}

}


错误信息:
正在通过存储根   DefaultColorPhone   来运行  
java.lang.NullPointerException
at   jarbook.loadStr(+12)
at   jarbook. <init> (+22)
at   java.lang.Class.runCustomCode(+0)
at   com.sun.midp.midlet.MIDletState.createMIDlet(+19)
at   com.sun.midp.midlet.Scheduler.schedule(+52)
at   com.sun.midp.main.Main.runLocalClass(+28)
at   com.sun.midp.main.Main.main(+116)
Execution   completed.
701216   bytecodes   executed
6   thread   switches
740   classes   in   the   system   (including   system   classes)
3695   dynamic   objects   allocated   (101952   bytes)
2   garbage   collections   (85716   bytes   collected)

------解决方案--------------------
直接访问文件不是很好