Java.lang.NullPointerException when using retreived Image (Unless method is used statically!)

Posted by Emdiesse on Stack Overflow See other posts from Stack Overflow or by Emdiesse
Published on 2010-03-19T17:38:02Z Indexed on 2010/03/19 17:41 UTC
Read the original article Hit count: 178

Hi there,

This has been doing my head in all day and I have finally decided to resort to asking for help!

In my MIDLet I have an instance of the java class ImageFetcher called anImg. Also within my MIDLet I have a command that simply say's fetch, a CommandListener that when detects fetch was clicked runs the function below. This function should simply run public getImage() from the anImg instance of class ImageFetcher which returns an image and then appends/sets this Image onto the form on the display. (You may recognise the getImage() function from the Nokia JavaME Wiki!!!)

Instead of any image being displayed this is written to the output terminal in netbeans: Msg: Java.lang.NullPointerException

HOWEVER, If I change public getImage() to public static getImage() and replace anImg.getImage() with ImageFetcher.getImage() the image is successfully displayed!!!

Thank you for your replies on this issue :) I look forward to going my hair back after this ordeal!

FetchImageApp.java

...
...
...
     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);
    }
...
...
...

© Stack Overflow or respective owner

Related posts about java

Related posts about image