I'm working on a memory game and I'm trying to use a get a value from a LinkedList to put in a selected Linked List. Here is my code:
import java.awt.GridLayout;
import java.awt.Image;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class Runner{
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LinkedList<Image> icon = new LinkedList<Image>();
for (int i = 0; i < 20; i++) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream((i + 1) + ".jpg");
try {
Image logo = ImageIO.read(input);
icon.push(logo);
}
catch (IOException e) {
e.printStackTrace();
}
}
Random rand = new Random();
LinkedList<Image> selected = new LinkedList<Image>();
for (int i = 0; i < 10; i++) {
int randomNum = rand.nextInt(20);
// Randomly pick one from the array of faces
Image face = icon.get(randomNum);
// Push 2 copies onto array
selected.push(face);
selected.push(face);
// Remove from faces array so we don't re-pick
icon.remove(randomNum);
}
Collections.shuffle(selected);
List<Card> tiles = new ArrayList<Card>();
int NUM_COLS = 5;
int NUM_ROWS = 4;
frame.setLayout(new GridLayout(NUM_ROWS, NUM_COLS));
for (int i = 0; i < NUM_COLS; i++) {
for (int j = 0; j < NUM_ROWS; j++) {
tiles.add(new Card(i * 39 + 10, j * 39 + 40, selected.pop()));
}
}
for (int i = 0; i < tiles.size(); i++) {
frame.add(tiles.get(i));
tiles.get(i).drawFaceDown();
}
for (int i = 0; i < tiles.size(); i++) {
frame.add(tiles.get(i));
tiles.get(i).drawFaceUp();
}
frame.pack();
frame.setVisible(true);
}
}
My problem is that I've tried putting images in my icon LinkedList however I'm getting an error:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at Runner.main(Runner.java:30)