I've browsed several questions on this site related to this issue and most of them say to use the getClass().getResource()
method. However, when I do this, it causes an IllegalArgumentException in Eclipse. After I export my project as a runnable JAR, double-clicking doesn't open it and running it in Command Prompt gives me this:
Microsoft Windows [Version 10.0.19043.1645]
(c) Microsoft Corporation. All rights reserved.
C:\Users\benaa>cd C:\Users\benaa\Documents\School\2420\RouteFinder
C:\Users\benaa\Documents\School\2420\RouteFinder>java -jar RouteFinder.jar
javax.imageio.IIOException: Can't read input file!
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1308)
at routeFinder.RouteApp.<init>(RouteApp.java:134)
at routeFinder.RouteApp$1.run(RouteApp.java:55)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
java.lang.NullPointerException
at routeFinder.RouteApp.<init>(RouteApp.java:142)
at routeFinder.RouteApp$1.run(RouteApp.java:55)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
C:\Users\benaa\Documents\School\2420\RouteFinder>
Here is the statement in question:
BufferedImage img = null;
try
{
img = ImageIO.read(new File("images/New Trax Map Edited.png"));
} catch (IOException e)
{
e.printStackTrace();
}
Again, I've tried the getClass.getResource();
method but it keeps throwing an IllegalArgumentException in Eclipse:
java.lang.IllegalArgumentException: input == null!
I've also tried reformatting so the image input uses a throws declaration at the beginning of the class instead of surrounding it with a try/catch, but the exact same thing happens. I have no idea what I'm doing wrong.
For additional info, here is my current file structure (files are copied over from their original destinations, not linked):
I can provide additional info as needed.