package hello;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
//CommandListener,
public class HelloMIDlet extends MIDlet implements ItemStateListener{
private boolean midletPaused = false;
private Display display;
private Form form;
private Command exitCommand;
private Command okCommand;
private Command a;
private Command b;
private Command c;
private Command d;
private StringItem stringItem;
private ChoiceGroup group;
private TextField textfield;
private TextField infor;
private TextField name;
private TextField book;
private TextField in;
public Form getForm() {
Image[] imageArray=null;
try {
Image duke=Image.createImage("/Icon.png");
imageArray=new Image[]{ duke,duke,duke,duke};
}catch(java.io.IOException err){}
if(form==null){
exitCommand = new Command("Exit", Command.EXIT, 1);
okCommand=new Command("send",Command.ITEM,1);
a=new Command("",Command.ITEM,1);
b=new Command("",Command.ITEM,1);
c=new Command("",Command.ITEM,1);
d=new Command("",Command.ITEM,1);
infor=new TextField("infor","text",18,TextField.ANY);
form.append(infor);
name=new TextField("name","text",18,TextField.ANY);
form.append(name);
book=new TextField("book","text",18,TextField.ANY);
form.append(book);
in=new TextField("in","text",18,TextField.ANY);
form.append(in);
textfield = new TextField("confirm item:","auto confirm",20,TextField.ANY);
form = new Form("Welcome", new Item[] {getStringItem()});
String[] stringArray={"a","b","query","continue out book" };
group =new ChoiceGroup("select item press enter",ChoiceGroup.POPUP,stringArray,imageArray);
form.append(group);
form.append(textfield);
form.addCommand(okCommand);
form.addCommand(exitCommand);
form.addCommand;
form.addCommand;
form.addCommand;
form.addCommand;
// form.setCommandListener(this);
form.setItemStateListener(this);
}
return form;
}
public void startApp() {
if (midletPaused) {
resumeMIDlet ();
} else {
initialize ();
startMIDlet ();
}
midletPaused = false;
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void callServlet() throws IOException {
HttpConnection http = null;
InputStream iStrm = null;
String infor1=infor.getString().trim();
String book2=book.getString().trim();
String name3=name.getString().trim();
String in4=in.getString().trim();
String url = "http://127.0.0.1/cellphone/servlet/SearchReaderInfo2" + "?" + "infor1="
+ infor1+"book2="
+book2+"name3="
+name3+"in4="
+in4;
try {
http = (HttpConnection) Connector.open(url);
http.setRequestMethod(HttpConnection.GET);
if (http.getResponseCode() == HttpConnection.HTTP_OK) {
iStrm = http.openInputStream();
int length = (int) http.getLength();
if (length > 0) {
byte servletData[] = new byte[length];
iStrm.read(servletData);
form.append("confirm ok : \n"
+ new String(servletData));
} else
form.append("no data");
}
} catch (Exception e) {
form.append("network error");
System.out.println(e.toString());
} finally {
if (iStrm != null)
iStrm.close();
if (http != null)
http.close();
}
}
public void updateServlet() throws IOException {
HttpConnection http = null;
InputStream iStrm = null;
String infor1=infor.getString().trim();
String book2=book.getString().trim();
String name3=name.getString().trim();
String in4=in.getString().trim();
String url = "http://127.0.0.1/cellphone/servlet/SearchReaderInfo2" + "?" + "infor1="
+ infor1+"book2="
+book2+"name3="
+name3+"in4="
+in4;
try {
http = (HttpConnection) Connector.open(url);
http.setRequestMethod(HttpConnection.GET);
if (http.getResponseCode() == HttpConnection.HTTP_OK) {
iStrm = http.openInputStream();
int length = (int) http.getLength();
if (length > 0) {
byte servletData[] = new byte[length];
iStrm.read(servletData);
form.append("confirm ok : \n"
+ new String(servletData));
} else
form.append("no data");
}
} catch (Exception e) {
form.append("network error");
System.out.println(e.toString());
} finally {
if (iStrm != null)
iStrm.close();
if (http != null)
http.close();
}
}
public void commandAction(Command a,Command b,Command c, Displayable s) {
if (form==s){
if (a == exitCommand) {
exitMIDlet();
}
if (a == okCommand ) {
System.out.println("123");
}
if (b == okCommand ) {
System.out.println("123");
}
if (c == okCommand ) {
System.out.println("123");
}
try{
System.out.println("error");
}
catch(Exception e ){
System.out.println("231");
}
}
}
public StringItem getStringItem() {
if (stringItem == null) {
// write pre-init user code here
stringItem = new StringItem("ksu libary", "please login:");
// write post-init user code here
}
return stringItem;
}
public void resumeMIDlet() {
}
private void initialize() {
}
public void startMIDlet() {
// write pre-action user code here
switchDisplayable(null, getForm());
// write post-action user code here
// try {
// callServlet();
// } catch (Exception e) {
// System.out.println(e.toString());
// }
}
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
Display display = getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}
}
public Display getDisplay () {
return Display.getDisplay(this);
}
public void exitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}
public void itemStateChanged(Item item) {
if( item instanceof ChoiceGroup)
{
textfield.setString( group.getString(group.getSelectedIndex()));
}
}
}
編譯時會過 執行會發生錯誤
startApp threw an Exception
java.lang.NullPointerException
java.lang.NullPointerException
at hello.HelloMIDlet.getForm(HelloMIDlet.java:47)
at hello.HelloMIDlet.startMIDlet(HelloMIDlet.java:224)
at hello.HelloMIDlet.startApp(HelloMIDlet.java:78)
at javax.microedition.midlet.MIDletProxy.startApp(MIDletProxy.java:43)
at com.sun.midp.midlet.Scheduler.schedule(Scheduler.java:374)
at com.sun.midp.main.Main.runLocalClass(Main.java:466)
at com.sun.midp.main.Main.main(Main.java:120)
請各位大大幫我看一下