0

I have the following Problem. I have Images in the Folder with the Path src/resources. Now I want to use them in the JavaFX application.

ClassLoader classLoader = getClass().getClassLoader();
InputStream markerInputStream = classLoader.getResourceAsStream("/img/marker.gif");
SurfaceImage surfaceImage = null;
try {
surfaceImage = new SurfaceImage(ImageIO.read(markerInputStream),sector);
} catch (IOException e)
{e.printStackTrace();}

This is the StackTrace:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(ImageIO.java:1348)
    at map.map.setMarkerPosition(main.java:184)
    at map.map$1.mouseClicked(main.java:151)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
    at java.awt.Component.processMouseEvent(Component.java:6538)
    at java.awt.Component.processEvent(Component.java:6300)
    at java.awt.Component.dispatchEventImpl(Component.java:4891)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

I don't know where the mistake is I have tested the Path /resources/marker.png only /marker.png

In the pom.xml of this module is the following one.

<artifactId>application</artifactId>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>2.5.3</version>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </plugin>
            </plugins>
            <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.fxml</include>
                    </includes>
                </resource>
            </resources>
        </build>
        <dependencies>
            <dependency>
<...>
            </dependency>
            <dependency>
                <groupId>gov.nasa</groupId>
                <artifactId>worldwind</artifactId>
                <version>2.0.0-986</version>
            </dependency>
            <dependency>
                <groupId>com.metsci.glimpse</groupId>
                <artifactId>glimpse-extras-worldwind</artifactId>
                <version>2.1.2</version>
            </dependency>
        </dependencies>
    </project>

Can you help me?

Jonas Wolff
  • 203
  • 1
  • 4
  • 11
  • Open the JAR to see the contents (ie. `jar tf your.jar | grep gif`). Is your GIF there (or was it PNG, your code use `.gif` your text writes `.png`)? Is it at the path you expected? Could be you need to add `**/*.gif,**/*.png` to the `include` tag for the Maven assembly plugin? – Harald K Jun 08 '16 at 14:49

1 Answers1

0

If classLoader.getResourceAsStream("/img/marker.gif"); is returning null you must ensure that resource is in your classpath.

Build your JAR or WAR and look inside for marker.gif. Maybe it doesn't exist or is under different path.

  • You shuld have something like this: ` src/resources *.* ` – Marco A. Hernandez Jun 09 '16 at 09:08
  • ok I changed it. In the jar are the pictures but the inputstream is null. I changed the Path in the program to /marker.gif because the pictures are in saved at the first level of the jar – Jonas Wolff Jun 09 '16 at 10:36
  • If you are using getClassLoader() you don't havo to use "/" before the name of the resource. Read this to understand how the findResource works and how to solve common problems: http://stackoverflow.com/questions/676250/different-ways-of-loading-a-file-as-an-inputstream – Marco A. Hernandez Jun 09 '16 at 11:01