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?