0

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)
Sruthi Kurada
  • 29
  • 1
  • 7

3 Answers3

1

This is where the exception occur.

Image face = icon.get(randomNum);

According to the exception you get, the icon contains no elements. Which means following code doesn't add elements to your linked list.

try {
            for (int i = 0; i < 20; i++) {
                icon.push(ImageIO.read(new File(i+1 + ".jpg")));

            }
        } 

        catch (IOException e) {

        } 

Your catch statement does nothing here. Print something and see whether adding fails. Since you haven't posted your card class, I can't test it. If adding elements to icon fails, it must be due to failiure in File reading.

pahan
  • 567
  • 1
  • 8
  • 22
0
  1. Are you using JRE instead of JDK?
  2. You aren't adding any images to a List, your list is empty. Your random function returns random int between 0 and 20, but your image list size can be 0 or any number.
arseniyandru
  • 760
  • 1
  • 7
  • 16
0

It looks like the code in the try block that pushes Images into the 'icon' Linked List is failing, throwing an error, but since your catch block is empty, you don't see it. As a result your Linked List is empty and therefore any .get() call will throw an error. I suggest printing the error being thrown in the catch block and that may tell you the root of the problem.