tdammon
tdammon

Reputation: 589

Java- can't read input file

I am trying to load a png image of a card to an object but I keep getting the following error:

"C:\Program Files\Java\jdk-9\bin\java" "-javaagent:C:\Users\trevo\Documents\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\lib\idea_rt.jar=60524:C:\Users\trevo\Documents\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\trevo\Desktop\Deck\out\production\Deck com.company.Card_Class
Exception in thread "main" javax.imageio.IIOException: Can't read input file!
    at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1308)
    at com.company.Card_Class.main(Card_Class.java:21)

Process finished with exit code 1

Here is my code:

package com.company;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Card_Class {
    private String suit, face;
    private int value;
    private BufferedImage cardimage;

    public Card_Class(String suit, String face, int value, BufferedImage cardimage) {
        this.suit = suit;
        this.face = face;
        this.value = value;
        this.cardimage = cardimage;
    }

    public static void main(String[] args) throws IOException {
        Card_Class KingOfAxes = new Card_Class("Diamonds", "King", 13, ImageIO.read(new File("KingOfAxes.png")));
        System.out.println("King");
    }
}

I have all my png card files in a folder labeled deck which is the project name.

Upvotes: 1

Views: 4635

Answers (1)

Phil Niehus
Phil Niehus

Reputation: 81

Try to write the full file path to the console to see if your file path is correct.

Maybe print the absolute path of your file to stdout to see if your path is correct. You should also check if your image exists and is readible before you use it. Here is an example for both:

public static void main(String[] args) throws IOException {
    System.out.println(new File("KingOfAxes.png").getAbsolutePath()); // Try this to pinpoint your issue
    File king = new File("KingOfAxes.png");

    if(king.canRead()){ // Check if your file exists and is readable before you use it
        JavaAssignmentPanel KingOfAxes = new JavaAssignmentPanel("Diamonds", "King", 13, ImageIO.read(new File("KingOfAxes.png")));
    } else{
        throw new IOException(king.getName() + " is not readable!"); // Not readable -> Throw exception
    }
    System.out.println("King");
}

Upvotes: 1

Related Questions