1

在我的 MIDLet 中有一个名为 anImg 的 java 类 ImageFetcher 的实例。同样在我的 MIDLet 中,我有一个简单地说是 fetch 的命令,一个 CommandListener 当检测到 fetch 被点击时运行下面的函数。这个函数应该简单地从类 ImageFetcher 的 anImg 实例运行 public getImage(),它返回一个图像,然后将该图像附加/设置到显示器上的表单上。(您可能会认出诺基亚 JavaME Wiki 中的 getImage() 函数!!!)

这不是显示任何图像,而是写入 netbeans 中的输出终端: Msg: Java.lang.NullPointerException

但是,如果我将 public getImage() 更改为 public static getImage() 并将 anImg.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.");
    }
}
4

2 回答 2

1

你能发布你的听众吗?我猜测 NPE 来自于侦听器使用 ImageFetcher 实例的事实,即null. 取消引用时NullPointerException会抛出 a 。

当您更改为静态方法时不会发生这种情况,因为不涉及任何实例。

于 2010-03-19T17:44:45.357 回答
1

如果你NullPointerExceptionanImg.getImage(),那么它只是意味着它anImgnull。做一个System.out.println(anImg);,你会看到它打印出来null

要修复它,您需要以anImg某种方式实例化。例如

ImageFetcher anImg = new ImageFetcher();

只有这样你才能访问它并调用它的方法。

于 2010-03-19T18:05:33.320 回答