title: java NullPointerException existed Image
body: <p>What I want to do is when the user selects an image, this image must be copied to a specific folder and then use the image.</p>
  
  <p>this is what I use to copy the image:</p>
  
  <pre><code>Files.copy(
      Paths.get("C:/../img.png"),
      Paths.get("C:/../src/bgs").resolve(array[array.length-1]),
      StandardCopyOption.REPLACE_EXISTING);
  </code></pre>
  
  <p>which works fine. The problem is that if I use the image immediately after this command, I get <code>NullPointerException</code> (I don't have this problem if I use something like <code>Thread.sleep(2500)</code> between the two commands). So basically I need a way to check if the file is ready to be used.</p>
  
  <p>So far I used the following code</p>
  
  <pre><code>// copy file
  File f = new File(new_path);
  sout(f.exists());
  // use file
  Image background = Toolkit.getDefaultToolkit().getImage(
      getClass().getResource("/bgs/img.png"));
  </code></pre>
  
  <p>which prints true, but when I use it I get <code>NullPointerException</code> again.. any ideas?</p>
  
  <p>Exception:</p>
  
  <pre><code>Uncaught error fetching image:
  java.lang.NullPointerException
      at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:115)
      at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:125)
      at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:263)
      at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:205)
      at sun.awt.image.ImageFetcher.run(ImageFetcher.java:169)
  </code></pre>