title: Java.lang.NullPointerException when using retrieved Image (Unless method is used statically!)
body: <p>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!!!)</p>
  
  <p>Instead of any image being displayed this is written to the output terminal in netbeans:
  Msg: Java.lang.NullPointerException</p>
  
  <p>HOWEVER, If I change public getImage() to public static getImage() and replace anImg.getImage() with ImageFetcher.getImage() the image is successfully displayed!!!</p>
  
  <p>Thank you for your replies on this issue :)
  I look forward to going my hair back after this ordeal!</p>
  
  <p>FetchImageApp.java</p>
  
  <pre><code>...
  ...
  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());
          }
       }
  ...
  ...
  ...
  </code></pre>
  
  <p>ImageFetcher.java</p>
  
  <pre><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);
      }
  ...
  ...
  ...
  </code></pre>
  
  <p>Here is the listener code as per request :)</p>
  
  <pre><code>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.");
      }
  }
  </code></pre>