在我的MIDLet我叫anImg的Java類ImageFetcher的一個實例。同樣在我的MIDLet中,我有一個簡單地說是獲取的命令,一個CommandListener在檢測到抓取被點擊時運行下面的函數。該函數應該簡單地從類ImageFetcher的anImg實例運行public getImage(),該實例返回一個圖像,然後將該圖像附加/設置到顯示器上的窗體上。 (您可能識別來自諾基亞的JavaME維基的getImage()函數!!!)顯示java.lang.NullPointerException(除非方法是靜態使用!)
代替任何圖像的顯示此被寫入到輸出端子在NetBeans: 信息:顯示java.lang.NullPointerException
但是,如果我將public getImage()更改爲public static getImage()並將ImageImage.getImage()替換爲ImageFetcher.getImage(),則圖像將成功顯示!
謝謝您的答覆對這個問題:) 我期待着這個嚴酷的考驗後去我的頭髮回來了!
FetchImageApp.java
...
...
public class FetchImageApp()
extends MIDlet implements CommandListener {
private ImageFetcher anImg; //this is my ImageFetcher instance, it is assigned within the constructor
public FetchImageApp(){
anImg = new ImageFetcher(); //NO IT WASN'T, I knew it was something simple... I feel a fool... but I know we all do it!
}
...
private doThis(){
try {
Image im;
if ((im = anImg.getImage()) != null) {
ImageItem ii = new ImageItem(null, im, ImageItem.LAYOUT_DEFAULT, null);
// If there is already an image, set (replace) it
if (form.size() != 0) {
form.set(0, ii);
} else // Append the image to the empty form
{
form.append(ii);
}
} else {
form.append("Unsuccessful download.");
}
// Display the form with the image
display.setCurrent(form);
} catch (Exception e) {
System.err.println("Msg: " + e.toString());
}
}
...
...
...
ImageFetcher.java
...
...
...
/*--------------------------------------------------
* Open connection and download png into a byte array.
*-------------------------------------------------*/
public Image getImage() throws IOException {
String url = "http://kenai.com/attachments/wiki_images/chessgame/java-duke-logo.png";
ContentConnection connection = (ContentConnection) Connector.open(url);
// * There is a bug in MIDP 1.0.3 in which read() sometimes returns
// an invalid length. To work around this, I have changed the
// stream to DataInputStream and called readFully() instead of read()
// InputStream iStrm = connection.openInputStream();
DataInputStream iStrm = connection.openDataInputStream();
ByteArrayOutputStream bStrm = null;
Image im = null;
try {
// ContentConnection includes a length method
byte imageData[];
int length = (int) connection.getLength();
if (length != -1) {
imageData = new byte[length];
// Read the png into an array
// iStrm.read(imageData);
iStrm.readFully(imageData);
} else // Length not available...
{
bStrm = new ByteArrayOutputStream();
int ch;
while ((ch = iStrm.read()) != -1) {
bStrm.write(ch);
}
imageData = bStrm.toByteArray();
bStrm.close();
}
// Create the image from the byte array
im = Image.createImage(imageData, 0, imageData.length);
} finally {
// Clean up
if (iStrm != null) {
iStrm.close();
}
if (connection != null) {
connection.close();
}
if (bStrm != null) {
bStrm.close();
}
}
return (im == null ? null : im);
}
...
...
...
這裏是聽者代碼爲每個請求:)
public void commandAction(Command c, Displayable d) {
if (c == doThisCommand) {
if (c.getLabel().equals("Start")) {
System.out.println("Started...");
begin();
//doThisCommand = new Command("Stop", Command.OK, 2); //ERROR:: After the command is changed to exit the program throws and unhandled excaption.
} else {
System.out.println("Stopped...");
doThisCommand = new Command("Start", Command.OK, 2);
}
} else if (c == exitCommand) {
notifyDestroyed();
} else {
throw new UnsupportedOperationException("Not supported yet.");
}
}
非常感謝,我知道這很簡單,我意識到當我添加額外的代碼時,我不知道發生了什麼,不會發起這個對象。我感到愚蠢......但我們都這樣做不是。請確認我不是唯一一個:) 真是太浪費了! – 2010-03-19 18:09:10
@Emdiesse上計算器這是習慣,以紀念該解決你的問題,因爲接受(使用計票下面打勾) – Bozho 2010-03-19 22:54:52
乾杯:)我以爲會有類似的東西的地方,但我無法找到它的答案。 – 2010-03-21 14:43:30