×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    3

    Unhappy NullPointerException when using retreived Image

    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
    Code:
    ...
    ...
    ...
         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
    Code:
    ...
    ...
    ...
        /*--------------------------------------------------
         * 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);
        }
    ...
    ...
    ...

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: NullPointerException when using retreived Image

    Quote Originally Posted by emdiesse View Post
    If I change public getImage() to public static getImage() and replace anImg.getImage() with ImageFetcher.getImage() the image is successfully displayed!!!
    Then, I'd suggest you make that change. I see no reason for this method to be an instance method.

    It sounds like anImg is null.

    Can you give me the link to the wiki article, please?

    Graham.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    3

    Re: NullPointerException when using retreived Image

    Quote Originally Posted by grahamhughes View Post
    Then, I'd suggest you make that change. I see no reason for this method to be an instance method.
    Why would I want to make it static? The whole idea of my not using static is so I could make multiple instances of the ImageFetcher.java that can be assigned different url's and so once loaded the instances would have different images...

    If I rename getImage to loadImage and use getImage to simply retreive the Image field from the instance in question so I don't have to load it again.

    ImageFetcher.getImage();

    would be very limiting in comparison to me using

    img1.getImage();
    img2.getImage();
    img3.getImage();

    If I was going to do that would it be even simpler to just stick it in my main class?

    At the moment I only have the single URL in that method through ease and laziness.

    It sounds like anImg is null.
    I just don't understand why it's returning null when it's public but an image when it's static, it makes no sense to me.

    Can you give me the link to the wiki article, please?
    Whoops, it wasn't from the wiki.
    Sorry it was from here: http://www.java2s.com/Code/Java/J2ME...ewapngfile.htm

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    3

    Re: NullPointerException when using retreived Image

    Now I feel a fool... Moral of this story...

    If you delete the objects initiation from the constructor, and then you need to use it... put it back!

    Solved by simple adding

    anImg = new ImageFetcher();

Similar Threads

  1. Image to byte[] (without getRGB)
    By lospatzos in forum Mobile Java Media (Graphics & Sounds)
    Replies: 2
    Last Post: 2009-07-28, 17:46
  2. Image Problem
    By Spider_Eg in forum Mobile Java General
    Replies: 2
    Last Post: 2007-01-30, 13:52
  3. AknIconUtils is caching the image
    By Tatanka.nbr1 in forum Symbian
    Replies: 12
    Last Post: 2007-01-05, 19:08
  4. saving jpeg image on grid list
    By flicker82 in forum Symbian
    Replies: 0
    Last Post: 2005-01-21, 04:22
  5. Nokia Image Converter
    By davidpurdie in forum General Development Questions
    Replies: 0
    Last Post: 2004-02-18, 15:31

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •