# TiledMapReader error in Java

**URL:** <https://discourse.mapeditor.org/t/tiledmapreader-error-in-java/3551>\
**Category:** Questions\
**Created:** [November 22, 2018, 2:43am UTC](https://discourse.mapeditor.org/t/tiledmapreader-error-in-java/3551 "2018-11-22T02:43:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Skijearz](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@Skijearz](https://discourse.mapeditor.org/u/Skijearz)\
**Post date:** [November 22, 2018, 2:43am UTC](https://discourse.mapeditor.org/t/tiledmapreader-error-in-java/3551/1 "2018-11-22T02:43:50Z")

</div>

Hello, im trying to use the TiledMap editor with the libtiled libary.  
Problem is now that if i try to use TMXMapReader.readMap(“file”) i will get the following error,  
my code [https://pastebin.com/VymY7x0X](https://pastebin.com/VymY7x0X).  
just commented half of it out because the error is specific to the readMap part.

```auto
javax.imageio.IIOException: Can't read input file!
	at javax.imageio.ImageIO.read(Unknown Source)
	at org.mapeditor.core.TileSet.importTileBitmap(TileSet.java:105)
	at org.mapeditor.io.TMXMapReader.unmarshalTileset(TMXMapReader.java:309)
	at org.mapeditor.io.TMXMapReader.unmarshalTilesetFile(TMXMapReader.java:220)
	at org.mapeditor.io.TMXMapReader.unmarshalTileset(TMXMapReader.java:243)
	at org.mapeditor.io.TMXMapReader.buildMap(TMXMapReader.java:721)
	at org.mapeditor.io.TMXMapReader.unmarshal(TMXMapReader.java:764)
	at org.mapeditor.io.TMXMapReader.readMap(TMXMapReader.java:791)
	at de.hsh.inform.darkit.gui.TileReader.buildMap(TileReader.java:39)
	at de.hsh.inform.darkit.gui.GameWindowController.startGame(GameWindowController.java:18)
	at de.hsh.inform.darkit.gui.mainMenuController.startGame(mainMenuController.java:11)
	at de.hsh.inform.darkit.gui.mainMenuController.gameStart(mainMenuController.java:22)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at sun.reflect.misc.Trampoline.invoke(Unknown Source)
	at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
	at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
	at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
	at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
	at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
	at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
	at javafx.event.Event.fireEvent(Event.java:198)
	at javafx.scene.Node.fireEvent(Node.java:8411)
	at javafx.scene.control.Button.fire(Button.java:185)
	at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
	at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
	at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
	at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
	at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
	at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
	at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
	at javafx.event.Event.fireEvent(Event.java:198)
	at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
	at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
	at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
	at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
	at java.security.AccessController.doPrivileged(Native Method)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
	at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
	at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
	at com.sun.glass.ui.View.notifyMouse(View.java:937)
	at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
	at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
	at java.lang.Thread.run(Unknown Source)

```

---

<div class="post-metadata">

**Author:** ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)\
**Post date:** [November 22, 2018, 8:23pm UTC](https://discourse.mapeditor.org/t/tiledmapreader-error-in-java/3551/2 "2018-11-22T20:23:56Z")

</div>

I think the problem is that your map is loaded from a resource:

```java
map = mapReader.readMap(TileReader.class.getResource("/de/hsh/inform/darkit/gui/map1.tmx").toString());

```

It may be interesting to put a breakpoint in `importTileBitmap` to see the value of `sourcePath`, so that we know which path it fails to load.

In the tests I see the following code to load a map from a resource:

```java
    public void testReadingSewersMap() throws Exception {
        // Arrange
        URL url = getUrlFromResources("sewers/sewers.tmx");

        // Act
        Map map = new TMXMapReader().readMap(url.getPath());

        ...
    }

    ...

    private URL getUrlFromResources(String filename) {
        ClassLoader classLoader = this.getClass().getClassLoader();
        return classLoader.getResource(filename);
    }

```

Is there a different between using `getString` or `getPath` that might explain the error?

Alternatively, maybe you are on Windows and you are affected by the issue where `File.separatorChar` is used in the following bit of code in `readMap`:

```java
        xmlPath = filename.substring(0,
                filename.lastIndexOf(File.separatorChar) + 1);

```

In which case it may help to replace “`/`” with `File.separatorChar` in the string you’re passing into `readMap`.

---

<div class="post-metadata">

**Author:** ![Skijearz](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@Skijearz](https://discourse.mapeditor.org/u/Skijearz)\
**Post date:** [November 23, 2018, 1:16am UTC](https://discourse.mapeditor.org/t/tiledmapreader-error-in-java/3551/3 "2018-11-23T01:16:20Z")

</div>

Ty this already helped a lot but, i guess the error is something with the file paths.  
As i used the url .getPath() method to call readMap i got an error that the tile file .tsx was not found at “java.io.FileNotFoundException: C:\Users\Dominic\git\test\map1.tmx”(created a new project to get the hang of it" but that is not the src of the project or something its the first folder from the eclipse project.  
src of the project should be  
“C:\Users\Dominic\git\test\src\application”

---

<div class="post-metadata">

**Author:** ![Skijearz](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@Skijearz](https://discourse.mapeditor.org/u/Skijearz)\
**Post date:** [November 29, 2018, 12:31am UTC](https://discourse.mapeditor.org/t/tiledmapreader-error-in-java/3551/4 "2018-11-29T00:31:26Z")

</div>

okay i tried it just like the test example

```auto
public void testReadingSewersMap() throws Exception {
        // Arrange
        URL url = getUrlFromResources("sewers/sewers.tmx");

        // Act
        Map map = new TMXMapReader().readMap(url.getPath());

        ...
    }
```

for the map.tmx it should be fine but it cant find the tsx file :

```auto
java.io.FileNotFoundException: C:\Users\Dominic\git\test\map1.tsx (Das System kann die angegebene Datei nicht finden)
	at java.io.FileInputStream.open0(Native Method)
	at java.io.FileInputStream.open(Unknown Source)
	at java.io.FileInputStream.<init>(Unknown Source)
	at java.io.FileInputStream.<init>(Unknown Source)
	at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
	at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source)
	at java.net.URL.openStream(Unknown Source)
	at org.mapeditor.io.TMXMapReader.unmarshalTileset(TMXMapReader.java:242)
	at org.mapeditor.io.TMXMapReader.buildMap(TMXMapReader.java:721)
	at org.mapeditor.io.TMXMapReader.unmarshal(TMXMapReader.java:764)
	at org.mapeditor.io.TMXMapReader.readMap(TMXMapReader.java:791)
	at application.Main.start(Main.java:63)
	at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
	at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
	at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
	at java.security.AccessController.doPrivileged(Native Method)
	at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
	at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
	at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
	at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
	at java.lang.Thread.run(Unknown Source)
Exception in Application start method
java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
	at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
	at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
	at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
	at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
	at application.Main.start(Main.java:72)
	at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
	at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
	at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
	at java.security.AccessController.doPrivileged(Native Method)
	at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
	at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
	at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
	at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
	... 1 more
Exception running application application.Main
```

and if i change the source of the Tileset, to match the path where it is located, in the map1.tmx

```auto
 <tileset firstgid="1" source="map1.tsx"/>
```

i get the following error :

```auto
javax.imageio.IIOException: Can't read input file!
	at javax.imageio.ImageIO.read(Unknown Source)
	at org.mapeditor.core.TileSet.importTileBitmap(TileSet.java:105)
	at org.mapeditor.io.TMXMapReader.unmarshalTileset(TMXMapReader.java:309)
	at org.mapeditor.io.TMXMapReader.unmarshalTilesetFile(TMXMapReader.java:220)
	at org.mapeditor.io.TMXMapReader.unmarshalTileset(TMXMapReader.java:243)
	at org.mapeditor.io.TMXMapReader.buildMap(TMXMapReader.java:721)
	at org.mapeditor.io.TMXMapReader.unmarshal(TMXMapReader.java:764)
	at org.mapeditor.io.TMXMapReader.readMap(TMXMapReader.java:791)
	at application.Main.start(Main.java:63)
	at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
	at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
	at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
	at java.security.AccessController.doPrivileged(Native Method)
	at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
	at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
	at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
	at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
	at java.lang.Thread.run(Unknown Source)
Exception in Application start method
java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
	at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
	at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
	at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
	at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
	at application.Main.start(Main.java:72)
	at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
	at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
	at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
	at java.security.AccessController.doPrivileged(Native Method)
	at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
	at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
	at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
	at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
	... 1 more
Exception running application application.Main
```

The breakpoint in `importTileBitmap` gets me this

```auto
  sourcePath = ../../../semester_3_projekt/test/src/application/Room_blue.png
```

Another question for me is, will the libtiled.jar allow me to build the files into a jar because everytime i try to build an .jar file and start it, it will search in the folder where my .jar file is and not in the jar itself.

```auto
C:/Users/Dominic/Desktop/ java -jar test.jar
```

the run examlpe above would give me this location where the libtiled.jar searches for my resources

```auto
file:C:/Users/Dominic/Desktop/
```
