title: Reading from text file to load images from resource folder
body: <p>I wanted to make a way so that I can load all the Images in my source folder without having to code each line for each image, but I keep getting an error and have tried different ways to do this and I still can't figure it out. Is it just impossible?</p>
  
  <p>Here is how it works: I save all the images I am going to need on a text file (in this case <code>startUp.txt</code>), then I store the lines in the text file in a linked-list(String) then I use a loop to get the images and store those in a linked-list(Image) so that I don't need to write code for every single Image to load.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>package com.game.task;
  
  import java.awt.Image;
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.util.LinkedList;
  import java.util.Scanner;
  
  import javax.swing.ImageIcon;
  
  public class imagesTest {
  
  public LinkedList&lt;Image&gt; storeImages = new LinkedList&lt;Image&gt;();
  private LinkedList&lt;String&gt; storeStrings = new LinkedList&lt;String&gt;();
  
  public imagesTest() {
      load();
      readimage();
  }
  
  public void readimage() {
  
      for(int index = 0; index &lt; storeStrings.size(); index++){
          Image temp = new ImageIcon(this.getClass().getClassLoader().getResource("/res/"+storeStrings.get(index))).getImage();
          storeImages.add(temp);
      }
  
  }
  
  private void load() {
      File file = new File("Data/startUp.txt");
      try {
          Scanner read = new Scanner(file);
          while (read.hasNextLine()) {
              storeStrings.add(read.nextLine());
          }
  
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      }
  }
  
  public static void main(String[] arsg) {
      imagesTest t = new imagesTest();
  }
  }
  </code></pre>
  
  <p>Exception:</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
  at javax.swing.ImageIcon.&lt;init&gt;(ImageIcon.java:217)
  at com.game.task.imagesTest.readimage(imagesTest.java:24)
  at com.game.task.imagesTest.load(imagesTest.java:41)
  at com.game.task.imagesTest.&lt;init&gt;(imagesTest.java:17)
  at com.game.task.imagesTest.main(imagesTest.java:45)
  </code></pre>