Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Image input = null?

  1. #1
    Junior Member
    Join Date
    May 2014
    Location
    Inverness, United Kingdom
    Posts
    11
    My Mood
    Cheerful
    Thanks
    0
    Thanked 1 Time in 1 Post

    Exclamation Image input = null?

    I get the following error when I try to run my jar file from a Batch file and CMD manually (The batch file is just set to run the jar and record any output to a text file, and open it after).

    Exception in thread "main" java.lang.IllegalArgumentException: input == null! 
       at javax.imageio.ImageIO.read(Unknown Source) 
       at com.github.bewd.project.main.Main.<init>(Main.java:37) 
       at com.github.bewd.project.main.Main.main(Main.java:76)

    However running from Netbeans, it works perfectly; JFrame and all. I'm not sure if it's something wrong with my classpath (which I don't think is likely) or something in my code that's affecting it, here's the code:

    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.math.*;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Random;
    import java.util.Scanner;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class Main extends JPanel{
     
       public static final String imgPathCard = "./img/cards/";
       public static final String imgPathLogo = "./img/logos/";
       public static final String fontPath = "./fonts/";
       private static final int appVersionMajor = 0;
       private static final double appVersionMinor = 1.3;
       private static final String appName = "Name";
       private static final String appStage = "A";
       private static final String versionString = appName + " (" + appVersionMajor + "." + appVersionMinor + " " + appStage + ")";
       private BufferedImage sdk1, sdk2, sdk3, sdk4, sdk5, sdk6, sdk7, sdk8, sdk9, sdk10, bc;
     
       public Main() {
     
       try {
            sdk1 = ImageIO.read(getClass().getResourceAsStream(imgPathCard + "sdk001.png"));
            sdk2 = ImageIO.read(getClass().getResourceAsStream(imgPathCard + "sdk002.png"));
            sdk3 = ImageIO.read(getClass().getResourceAsStream(imgPathCard + "sdk003.png"));
            sdk4 = ImageIO.read(getClass().getResourceAsStream(imgPathCard + "sdk004.png"));
            sdk5 = ImageIO.read(getClass().getResourceAsStream(imgPathCard + "sdk005.png"));
        } 
        catch (IOException e) {
            Logger LogErr = Logger.getLogger(Main.class.getName());
            System.err.println(appName + " " + "Caught IOException: " + e.getMessage());
        }
     
    }
     
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(sdk1, 0, 0, Card.cardWidth / 5, Card.cardHeight / 5, null);
        g.drawImage(sdk2, Card.cardLong, 0, Card.cardWidth / 5, Card.cardHeight / 5, null);
        g.drawImage(sdk3, Card.cardLong * 2, 0, Card.cardWidth / 5, Card.cardHeight / 5, null);
        g.drawImage(sdk4, Card.cardLong * 3, 0, Card.cardWidth / 5, Card.cardHeight / 5, null);
        g.drawImage(sdk5, Card.cardLong * 4, 0, Card.cardWidth / 5, Card.cardHeight / 5, null);
    }
     
    public static void main(String[] args) {
     
            Main main = new Main();
            JFrame frame = new JFrame(versionString);
            frame.add(main);
            frame.setSize(1020, 680);
            frame.setVisible(true);
            frame.setResizable(false);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        }
    }

    Now I've created a separate package "resources/img/cards" rather than have it in the "main/img" package.

    And have tidied up the code:

    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.math.*;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Random;
    import java.util.Scanner;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class Main extends JPanel{
     
       public static final String imgPathCard = "./img/cards/";
       public static final String imgPathLogo = "./img/logos/";
       public static final String fontPath = "./fonts/";
       private static final int appVersionMajor = 0;
       private static final double appVersionMinor = 1.3;
       private static final String appName = "Name";
       private static final String appStage = "A";
       private static final String versionString = appName + " (" + appVersionMajor + "." + appVersionMinor + " " + appStage + ")";
       private BufferedImage sdk1, sdk2, sdk3, sdk4, sdk5, sdk6, sdk7, sdk8, sdk9, sdk10, bc;
       public JFrame frame = new JFrame(versionString);
     
       public Main() {
     
       try {
            sdk1 = ImageIO.read(getClass().getResourceAsStream(imgPathCard + "sdk001.png"));
            sdk2 = ImageIO.read(getClass().getResourceAsStream(imgPathCard + "sdk002.png"));
            sdk3 = ImageIO.read(getClass().getResourceAsStream(imgPathCard + "sdk003.png"));
            sdk4 = ImageIO.read(getClass().getResourceAsStream(imgPathCard + "sdk004.png"));
            sdk5 = ImageIO.read(getClass().getResourceAsStream(imgPathCard + "sdk005.png"));
        } 
        catch (IOException e) {
            Logger LogErr = Logger.getLogger(Main.class.getName());
            System.err.println(appName + " " + "Caught IOException: " + e.getMessage());
        }
     
    }
    public void initFrame(){
                frame.add(this);
                frame.setSize(1020, 680);
                frame.setResizable(false);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                frame.repaint();
                frame.validate();
                frame.setVisible(true);
     
    }
     
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(sdk1, 0, 0, Card.cardWidth / 5, Card.cardHeight / 5, null);
        g.drawImage(sdk2, Card.cardLong, 0, Card.cardWidth / 5, Card.cardHeight / 5, null);
        g.drawImage(sdk3, Card.cardLong * 2, 0, Card.cardWidth / 5, Card.cardHeight / 5, null);
        g.drawImage(sdk4, Card.cardLong * 3, 0, Card.cardWidth / 5, Card.cardHeight / 5, null);
        g.drawImage(sdk5, Card.cardLong * 4, 0, Card.cardWidth / 5, Card.cardHeight / 5, null);
    }
     
    public static void main(String[] args) {
     
            Main main = new Main();
            main.initFrame();
        }
    }

    Now from Netbeans I get:
    Exception in thread "main" java.lang.IllegalArgumentException: input == null!
    	at javax.imageio.ImageIO.read(ImageIO.java:1348)
    	at com.github.bewd.project.main.Main.<init>(Main.java:37)
    	at com.github.bewd.project.main.Main.main(Main.java:73)
    Java Result: 1

    Any help/advice how I can go about fixing this?


  2. #2
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Image input = null?

    The classpath that is set when you execute your program (via NetBeans, batch file or command line) does have something to do with this. The API documentation for getResourceAsStream() states,
    "Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader."

    Since the class loader searches for resources based on the classpath, the classpath that is set, explicitly or implicitly (e.g., by NetBeans), will determine if getResourceAsStream() returns an InputStream object or null (if the resource is not found). The IllegalArgumentException that you're getting is due to the latter, i.e., the path you provide to getResourceAsStream() does not lead to the image file.

    Have you confirmed that your jar file contains the image files? If so, what is the path in the jar file where the image files are located? If you have trouble working this out, post here the contents of your jar file. You can use "jar -tvf <jar_file_name>" to print out the contents of the jar file together with their paths.

    If your image files are deliberately placed outside the jar file, you can add
    System.out.println(getClass().getResource(""));
    right before
    sdk1 = ImageIO.read(getClass().getResourceAsStream(imgPathCard + "sdk001.png"));
    to see the folder at which getResourceAsStream() is looking, and work out if the path you provide to it leads to the image files.

Similar Threads

  1. Illegal Argument Exception: input == null
    By Ecen in forum Exceptions
    Replies: 4
    Last Post: January 8th, 2014, 11:29 AM
  2. Re: Illegal Argument Exception: input == null
    By eglentine in forum Object Oriented Programming
    Replies: 1
    Last Post: January 8th, 2014, 08:37 AM
  3. how to input image file in java code.
    By jerim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 27th, 2013, 10:52 AM
  4. Replies: 2
    Last Post: February 14th, 2011, 04:36 PM
  5. [SOLVED] Web Service Schema input parameter is null
    By wilky in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 8th, 2010, 09:23 AM