当前位置: 技术问答>java相关
我都快要急疯了!!我的j2me程序,老是出错。现附例题一则,请帮我看看:
来源: 互联网 发布时间:2015-02-07
本文导语: 我都快要急疯了!!我的j2me程序,用j2me wireless toolkit 可以built通过,但是run时,总是出错:null exception:java.lang.nullpointer Exception.我用的都是一些书上或网上的例题,但是都出这样的错误。 /* * HelloWorld.java * * June 27...
我都快要急疯了!!我的j2me程序,用j2me wireless toolkit 可以built通过,但是run时,总是出错:null exception:java.lang.nullpointer Exception.我用的都是一些书上或网上的例题,但是都出这样的错误。
/*
* HelloWorld.java
*
* June 27, 2000
*
* ? Copyright 2000 Motorola, Inc. All Rights Reserved.
* This notice does not imply publication.
*/
package com.mot.j2me.midlets.tutorials;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
/**
* A simple Hello World midlet
*
* @see MIDlet
*/
public class HelloWorld extends MIDlet {
/**
* Main application screen
*/
private Form mainScreen;
/**
* A reference to the Display
*/
private Display myDisplay;
/**
* Creates a screen for our midlet
*/
HelloWorld() {
myDisplay = Display.getDisplay(this);
mainScreen = new Form("Hello World");
/*
* Create a string item
*/
StringItem strItem = new StringItem
("Hello", "This is a J2ME MIDlet.");
mainScreen.append(strItem);
}
/**
* Start the MIDlet
*/
public void startApp() throws MIDletStateChangeException {
myDisplay.setCurrent(mainScreen);
}
/**
* Pause the MIDlet
*/
public void pauseApp() {
}
/**
* Called by the framework before the application is unloaded
*/
public void destroyApp(boolean unconditional) {
}
}
/*
* HelloWorld.java
*
* June 27, 2000
*
* ? Copyright 2000 Motorola, Inc. All Rights Reserved.
* This notice does not imply publication.
*/
package com.mot.j2me.midlets.tutorials;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
/**
* A simple Hello World midlet
*
* @see MIDlet
*/
public class HelloWorld extends MIDlet {
/**
* Main application screen
*/
private Form mainScreen;
/**
* A reference to the Display
*/
private Display myDisplay;
/**
* Creates a screen for our midlet
*/
HelloWorld() {
myDisplay = Display.getDisplay(this);
mainScreen = new Form("Hello World");
/*
* Create a string item
*/
StringItem strItem = new StringItem
("Hello", "This is a J2ME MIDlet.");
mainScreen.append(strItem);
}
/**
* Start the MIDlet
*/
public void startApp() throws MIDletStateChangeException {
myDisplay.setCurrent(mainScreen);
}
/**
* Pause the MIDlet
*/
public void pauseApp() {
}
/**
* Called by the framework before the application is unloaded
*/
public void destroyApp(boolean unconditional) {
}
}
|
用printStackTrace定位你出错的语句!
|
constructor HelloWorld() need add public key word,
or your environment of project setting is error,
you can see the demo setting and set it.
or your environment of project setting is error,
you can see the demo setting and set it.
|
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.lang.*;
public class HelloWorld extends MIDlet implements CommandListener
{
private Form mainScreen;
private Display myDisplay;
public HelloWorld()
{
myDisplay = Display.getDisplay(this);
mainScreen = new Form("Hello World");
StringItem strItem = new StringItem("Hello", "This is a J2ME MIDlet.");
mainScreen.append(strItem);
}
public void startApp()
{
myDisplay.setCurrent(mainScreen);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command c,Displayable d){}
}
import javax.microedition.midlet.*;
import java.lang.*;
public class HelloWorld extends MIDlet implements CommandListener
{
private Form mainScreen;
private Display myDisplay;
public HelloWorld()
{
myDisplay = Display.getDisplay(this);
mainScreen = new Form("Hello World");
StringItem strItem = new StringItem("Hello", "This is a J2ME MIDlet.");
mainScreen.append(strItem);
}
public void startApp()
{
myDisplay.setCurrent(mainScreen);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command c,Displayable d){}
}