title: URLImage in InfiniteScrollAdapter in Simulator shows NPE (CodenameOne)
body: <p>My app features an <code>InfiniteScrollAdapter</code> populated with images through <code>URLImage</code> and <code>URLImage.ImageAdapter</code>. </p>
  
  <p>In the simulator (Iphone3GS or Xoom or GoogleNexus7), and NPE is shown the first time the <code>InfiniteScrollAdapter</code> appears, although the file does exist on the server.</p>
  
  <p><strong>Please note : In this test there was only one entry in the database. So on the image below what you should see is the same row (image + text) duplicated 3 times.</strong></p>
  
  <p><a href="https://i.stack.imgur.com/OtYfP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OtYfP.png" alt="NPE in InfiniteScrollAdapter"></a> </p>
  
  <p>Please note that the order in the undisplayed icon can differ </p>
  
  <p><a href="https://i.stack.imgur.com/cBiZm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cBiZm.png" alt="NPE with different order in the undisplayed icons"></a></p>
  
  <p>The code I used to download the image is :</p>
  
  <pre><code>Image tempPlaceholder = Image.createImage(
              ParametresGeneraux.SIZE_OF_REPORT_PIC_IN_PX,
              ParametresGeneraux.SIZE_OF_REPORT_PIC_IN_PX,
              ParametresGeneraux.accentColor);
      Graphics gr = tempPlaceholder.getGraphics();
      gr.setAntiAliased(true);
      gr.setColor(ParametresGeneraux.accentColor);
      gr.fillArc(0, 0, ParametresGeneraux.SIZE_OF_REPORT_PIC_IN_PX, ParametresGeneraux.SIZE_OF_REPORT_PIC_IN_PX, 0, 360);
  
      EncodedImage roundPlaceholder = EncodedImage.createFromImage(tempPlaceholder, true);
  
      final Image reportImage = URLImage.createToStorage(
                              roundPlaceholder,
                              photoFilenameInStorage,
                              currentReport.getPhotoPath(),
                              ParametresGeneraux.RESIZE_SCALE_WITH_ROUND_MASK
                      );
  </code></pre>
  
  <p>And here is the overridden imageAdapter method :</p>
  
  <pre><code>    public final static URLImage.ImageAdapter RESIZE_SCALE_WITH_ROUND_MASK = new URLImage.ImageAdapter() {
      @Override
      public EncodedImage adaptImage(EncodedImage downloadedImage, EncodedImage placeholderImage) {
          final Image[] tmp = new Image[1];
  
          if (!Display.getInstance().isEdt()) {
              // The image scaling has to be called from EDT
              Display.getInstance().callSeriallyAndWait(() -&gt; {
  
                  tmp[0] = downloadedImage.scaledLargerRatio(placeholderImage.getWidth(), placeholderImage.getHeight());
                  if (tmp[0].getWidth() &gt; placeholderImage.getWidth()) {
                      int diff = tmp[0].getWidth() - placeholderImage.getWidth();
                      int x = diff / 2;
                      tmp[0] = tmp[0].subImage(x, 0, placeholderImage.getWidth(), placeholderImage.getHeight(), true);
                  } else if (tmp[0].getHeight() &gt; placeholderImage.getHeight()) {
                      int diff = tmp[0].getHeight() - placeholderImage.getHeight();
                      int y = diff / 2;
                      tmp[0] = tmp[0].subImage(0, y, Math.min(placeholderImage.getWidth(), tmp[0].getWidth()),
                              Math.min(placeholderImage.getHeight(), tmp[0].getHeight()), true);
                  }
              });
          } else {
              tmp[0] = downloadedImage.scaledLargerRatio(placeholderImage.getWidth(), placeholderImage.getHeight());
              if (tmp[0].getWidth() &gt; placeholderImage.getWidth()) {
                  int diff = tmp[0].getWidth() - placeholderImage.getWidth();
                  int x = diff / 2;
                  tmp[0] = tmp[0].subImage(x, 0, placeholderImage.getWidth(), placeholderImage.getHeight(), true);
              } else if (tmp[0].getHeight() &gt; placeholderImage.getHeight()) {
                  int diff = tmp[0].getHeight() - placeholderImage.getHeight();
                  int y = diff / 2;
                  tmp[0] = tmp[0].subImage(0, y, Math.min(placeholderImage.getWidth(), tmp[0].getWidth()),
                          Math.min(placeholderImage.getHeight(), tmp[0].getHeight()), true);
              }
          }
  
          EncodedImage[] image2Return = new EncodedImage[1];
          if (!Display.getInstance().isEdt()) {
              // The image scaling has to be called from EDT
              Display.getInstance().callSeriallyAndWait(() -&gt; {
                  Image roundMask = Image.createImage(tmp[0].getWidth(), tmp[0].getHeight(), 0xff000000);
                  Graphics gr = roundMask.getGraphics();
                  gr.setColor(0xffffff);
  
                  gr.fillArc(0, 0, tmp[0].getWidth(), tmp[0].getHeight(), 0, 360);
                  Object mask = roundMask.createMask();
                  tmp[0] = tmp[0].applyMask(mask);
                  image2Return[0] = EncodedImage.createFromImage(tmp[0], false);
              });
          } else {
              Image roundMask = Image.createImage(tmp[0].getWidth(), tmp[0].getHeight(), 0xff000000);
              Graphics gr = roundMask.getGraphics();
              gr.setColor(0xffffff);
  
              gr.fillArc(0, 0, tmp[0].getWidth(), tmp[0].getHeight(), 0, 360);
              Object mask = roundMask.createMask();
              tmp[0] = tmp[0].applyMask(mask);
              image2Return[0] = EncodedImage.createFromImage(tmp[0], false);
          }
  
          return image2Return[0];
  
      } 
  </code></pre>
  
  <p>In the stacktrace, the NPE seems to stem from the overridden <code>URLImage.ImageAdapter</code> :</p>
  
  <blockquote>
    <p>java.lang.IllegalArgumentException: create image failed for the given
    image data of length: 0 at
    com.codename1.ui.Image.createImage(Image.java:654) at
    com.codename1.ui.EncodedImage.getInternal(EncodedImage.java:365) at
    com.codename1.ui.EncodedImage.getInternalImpl(EncodedImage.java:340)
    at com.codename1.ui.EncodedImage.getHeight(EncodedImage.java:522) at
    com.codename1.ui.Image.scaledLargerRatio(Image.java:899) at
    com.my.application.ParametresGeneraux$1.lambda$adaptImage$0(ParametresGeneraux.java:564)
    at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:95) at
    com.codename1.ui.Display.processSerialCalls(Display.java:1154) at
    com.codename1.ui.Display.edtLoopImpl(Display.java:1098) at
    com.codename1.ui.Display.invokeAndBlock(Display.java:1207) at
    com.codename1.ui.Display.invokeAndBlock(Display.java:1244) at
    com.codename1.ui.URLImage$DownloadCompleted.actionPerformed(URLImage.java:233)
    at com.codename1.ui.URLImage$4.onSucess(URLImage.java:301) at
    com.codename1.ui.URLImage$4.onSucess(URLImage.java:297) at
    com.codename1.util.CallbackDispatcher.run(CallbackDispatcher.java:53)
    at com.codename1.ui.Display.processSerialCalls(Display.java:1154) at
    com.codename1.ui.Display.edtLoopImpl(Display.java:1098) at
    com.codename1.ui.Display.mainEDTLoop(Display.java:999) at
    com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120) at
    com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
    [EDT] 0:0:0,1 - Codename One revisions:
    e5c43877074c18b4b5c7748d000e5cfac75ab749 2318</p>
    
    <p>[EDT] 0:0:0,1 - Exception: java.lang.NullPointerException - null
    java.lang.NullPointerException at
    com.codename1.impl.javase.JavaSEPort.scale(JavaSEPort.java:3996) at
    com.codename1.ui.Image.scale(Image.java:1007) at
    com.codename1.ui.Image.scaledImpl(Image.java:953) at
    com.codename1.ui.Image.scaled(Image.java:918) at
    com.codename1.impl.javase.JavaSEPort$71.save(JavaSEPort.java:7659) at
    com.codename1.ui.EncodedImage.scaledEncoded(EncodedImage.java:626) at
    com.codename1.ui.EncodedImage.scaled(EncodedImage.java:653) at
    com.codename1.ui.Image.scaledLargerRatio(Image.java:904) at
    com.my.application.ParametresGeneraux$1.lambda$adaptImage$0(ParametresGeneraux.java:564)
    at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:95) at
    com.codename1.ui.Display.processSerialCalls(Display.java:1154) at
    com.codename1.ui.Display.edtLoopImpl(Display.java:1098) at
    com.codename1.ui.Display.invokeAndBlock(Display.java:1207) at
    com.codename1.ui.Display.invokeAndBlock(Display.java:1244) at
    com.codename1.ui.URLImage$DownloadCompleted.actionPerformed(URLImage.java:233)
    at com.codename1.ui.URLImage$4.onSucess(URLImage.java:301) at
    com.codename1.ui.URLImage$4.onSucess(URLImage.java:297) at
    com.codename1.util.CallbackDispatcher.run(CallbackDispatcher.java:53)
    at com.codename1.ui.Display.processSerialCalls(Display.java:1154) at
    com.codename1.ui.Display.edtLoopImpl(Display.java:1098) at
    com.codename1.ui.Display.mainEDTLoop(Display.java:999) at
    com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120) at
    com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)</p>
  </blockquote>
  
  <p>Moreover, a glance in the .cn1 directory shows the URLImage storage file name with the suffix "ImageURLTMP" which does not appear when everything works without NPE.</p>
  
  <p>Finally, if I come back to this form later, everything works as expected (images were shown, no NPE). I tried to test for downloadedImage nullness in <code>imageAdapter</code> but the EncodedImage is not null.</p>
  
  <p>How can I avoid this NPE?</p>
  
  <p><strong>Edit March 1st 2017</strong> </p>
  
  <p>Following the answers from @Diamond and @Shai, I believe the NPE occurs because the InfiniteScrollAdapter wants to fill in the screen with rows and consequently launches the download of the same image simultaneously (because it is not in cache). So a solution could be to prevent the InfiniteScrollAdapter to loop (so it becomes finite). How can I do that ? </p>
  
  <p><em>Please also note that there is not 404 error, the Network monitor shows response code 200 as depicted below. However the image should not be downloaded 3 times, should it ?</em></p>
  
  <p><a href="https://i.stack.imgur.com/PUgJY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PUgJY.png" alt="No 404"></a></p>