1

私の MIDLet には、anImg と呼ばれる Java クラス ImageFetcher のインスタンスがあります。また、私の MIDLet 内には、単純に fetch と言うコマンドがあります。フェッチがクリックされたことを検出すると CommandListener が以下の関数を実行します。この関数は、クラス ImageFetcher の anImg インスタンスから public getImage() を実行するだけで、画像を返し、この画像をディスプレイ上のフォームに追加/設定する必要があります。(Nokia 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

NullPointerExceptionに乗る場合anImg.getImage()、それは単にそれが であることを意味しanImgますnullSystem.out.println(anImg);を実行すると、印刷されることがわかりますnull

anImgそれを修正するには、何らかの方法でインスタンス化する必要があります。例えば

ImageFetcher anImg = new ImageFetcher();

そうして初めて、それにアクセスしてメソッドを呼び出すことができます。

于 2010-03-19T18:05:33.320 に答える
1

リスナーを投稿できますか?NPE は、リスナーが ImageFetcher インスタンスを使用しているという事実から来ていると推測していますnull。逆参照すると、 aNullPointerExceptionがスローされます。

インスタンスが関与していないため、静的メソッドに変更すると、これは発生しません。

于 2010-03-19T17:44:45.357 に答える