1

I have a bit of a problem with my libGDX project. In class(high school, don't think of too much), we have to make a hangman game. With some of my friends, we started to make it in Scala. Well, it works, kind of. But the problem is that I cannot read the files from which the words are generated. It's a yaml file(words.yml), inside the android sub-project's assets. It is packed inside the .jar file, I checked it, however when I try to read it using Gdx.files.internal it stops with a NullPointerException. It's bugging me because I was able to read the image files. I thought it is impossible to read using Gdx.files.internal from not the main file from the core project. But that doesn't seem to be the case since it crashes as well if I put it in that file.

The field where it dies:

private val map: (Int, Array[String]) = {
    val yaml: Yaml = new Yaml //using the snakeyaml library

    //Debug
    println("DBG")
    println(Gdx.files.local("words.yml").path()) //<-------It crashes here
    println("more DBG")

    val tmp: util.LinkedHashMap[String, Any] = yaml.load(
        scala.io.Source.fromFile(Gdx.files.internal("words.yml").file()) mkString) 
       //^^^^^or here if I remove the debug part^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    (tmp.get("wordCount").asInstanceOf[Int],
    tmp.get("words").toString.replaceAll("[\\[\\]]","").split(",")
        .map((s: String) => {
            if(s(0) == ' ') s.substring(1)
            else s
        }))
}

Error msg:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.infosoftcorp.wordpress.ish.Save.<init>(Save.scala:7)
    at com.infosoftcorp.wordpress.ish.InfoSoftHangman.<init>(InfoSoftHangman.scala:35)
    at com.infosoftcorp.wordpress.ish.desktop.DesktopLauncher.main(DesktopLauncher.java:17)
Caused by: java.lang.NullPointerException
    at com.infosoftcorp.wordpress.ish.FileHandler$.<init>(FileHandler.scala:25)
    at com.infosoftcorp.wordpress.ish.FileHandler$.<clinit>(FileHandler.scala)
... 3 more

File structure:

.
├── android
│   ├── android.iml
│   ├── AndroidManifest.xml
│   ├── assets
│   │   ├── icon.png
│   │   ├── img
│   │   │   ├── 10.png
│   │   │   ├── 11.png
│   │   │   ├── 1.png
│   │   │   ├── 2.png
│   │   │   ├── 3.png
│   │   │   ├── 4.png
│   │   │   ├── 5.png
│   │   │   ├── 6.png
│   │   │   ├── 7.png
│   │   │   ├── 8.png
│   │   │   ├── 9.png
│   │   │   └── line.png
│   │   └── words.yml
│   ├── build.gradle
│   ├── ic_launcher-web.png
│   ├── libs
│   │   ├── armeabi
│   │   │   ├── libgdx-bullet.so
│   │   │   └── libgdx.so
│   │   ├── armeabi-v7a
│   │   │   ├── libgdx-bullet.so
│   │   │   └── libgdx.so
│   │   └── x86
│   │       ├── libgdx-bullet.so
│   │       └── libgdx.so
│   ├── proguard-project.txt
│   ├── project.properties
│   ├── res
│   │   ├── drawable-hdpi
│   │   │   └── ic_launcher.png
│   │   ├── drawable-mdpi
│   │   │   └── ic_launcher.png
│   │   ├── drawable-xhdpi
│   │   │   └── ic_launcher.png
│   │   ├── drawable-xxhdpi
│   │   │   └── ic_launcher.png
│   │   ├── values
│   │   │   ├── strings.xml
│   │   │   └── styles.xml
│   │   └── values-hu
│   │       └── strings.xml
│   └── src
│       └── com
│           └── infosoftcorp
│               └── wordpress
│                   └── ish
│                       └── android
│                           └── AndroidLauncher.java
├── asd.html
├── bitbucket-pipelines.yml
├── build
│   ├── android-profile
│   │   ├── profile-2018-02-12-00-57-01-965.rawproto
│   │   ├── profile-2018-02-12-00-58-00-422.rawproto
│   │   ├── profile-2018-02-12-00-59-18-263.rawproto
│   │   ├── profile-2018-02-12-00-59-55-075.rawproto
│   │   ├── profile-2018-02-12-01-00-22-650.rawproto
│   │   └── profile-2018-02-12-01-03-02-363.rawproto
│   ├── generated
│   │   ├── mockable-android-26.jar
│   │   └── mockable-android-27.jar
│   └── intermediates
│       ├── lint-cache
│       │   ├── api-versions-6-27.0.1.bin
│       │   ├── typos-en.txt-2.bin
│       │   └── typos-hu.txt-2.bin
│       └── proguard-files
│           ├── proguard-android-optimize.txt-2.3.0
│           └── proguard-android.txt-2.3.0
├── build.gradle
├── core
│   ├── build
│   │   ├── classes
│   │   │   └── main
│   │   │       └── com
│   │   │           └── infosoftcorp
│   │   │               └── wordpress
│   │   │                   └── ish
│   │   │                       ├── FileHandler$$anonfun$1.class
│   │   │                       ├── FileHandler.class
│   │   │                       ├── FileHandler$.class
│   │   │                       ├── InfoSoftHangman$$anonfun$create$1.class
│   │   │                       ├── InfoSoftHangman$$anonfun$drawLines$1$1.class
│   │   │                       ├── InfoSoftHangman.class
│   │   │                       ├── InfoSoftHangman$InputHandler.class
│   │   │                       ├── Parsing$$anonfun$parseString$1.class
│   │   │                       ├── Parsing$$anonfun$removeEach$1.class
│   │   │                       ├── Parsing.class
│   │   │                       ├── Parsing$.class
│   │   │                       ├── Parsing$DecLives.class
│   │   │                       ├── Parsing$HMSignal.class
│   │   │                       ├── Parsing$ParseError.class
│   │   │                       ├── Parsing$ParsingResult.class
│   │   │                       ├── Save.class
│   │   │                       └── Save$.class
│   │   ├── libs
│   │   │   └── core-1.0.jar
│   │   └── tmp
│   │       ├── compileScala
│   │       └── jar
│   │           └── MANIFEST.MF
│   ├── build.gradle
│   ├── core.iml
│   └── src
│       └── com
│           └── infosoftcorp
│               └── wordpress
│                   └── ish
│                       ├── FileHandler.scala
│                       ├── InfoSoftHangman.scala
│                       ├── Parsing.scala
│                       └── Save.scala
├── desktop
│   ├── build
│   │   ├── classes
│   │   │   └── main
│   │   │       └── com
│   │   │           └── infosoftcorp
│   │   │               └── wordpress
│   │   │                   └── ish
│   │   │                       └── desktop
│   │   │                           └── DesktopLauncher.class
│   │   ├── dependency-cache
│   │   ├── libs
│   │   │   └── desktop-1.0.jar
│   │   └── tmp
│   │       └── <chaos>
│   ├── build.gradle
│   ├── desktop.iml
│   ├── out
│   │   └── production
│   │       └── classes
│   └── src
│       └── com
│           └── infosoftcorp
│               └── wordpress
│                   └── ish
│                       └── desktop
│                           └── DesktopLauncher.java
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradle.properties
├── gradlew
├── gradlew.bat
├── is-hangman.iml
├── is-hangman.ipr
├── is-hangman.iws
├── local.properties
├── README.md
└── settings.gradle

I added the working directory for the desktop sub-project

bodand
  • 117
  • 3
  • 8
  • Which line causes the error? – Code-Apprentice Feb 13 '18 at 16:20
  • println(Gdx.files.local("words.yml").path()), since I put the debug there, if I take the debug part out scala.io.Source.fromFile(Gdx.files.internal("words.yml").file()) mkString – bodand Feb 13 '18 at 16:25
  • Please [edit] your question to show that. – Code-Apprentice Feb 13 '18 at 16:35
  • I put a comment in the code //<-------It crashes here – bodand Feb 13 '18 at 16:44
  • `Gdx.files.local("words.yml")` returns `null`, which means it isn't finding the `words.yml` file. Please edit your question to show the directory structure of your project and what folder contains `words.yml`. – Code-Apprentice Feb 13 '18 at 19:28
  • You first have to copy the file in assets to somewhere on the filesystem. The library expects a file path. A file in Gdx.files.local/internal. Assets is a completely different place. And a resource. No file storage. – greenapps Feb 13 '18 at 19:31
  • @Code-Apprentice Added the structure of the assets folder(its night-time around here so I am only working from memory, but I am sure of the assets folder's content, I will edit the question to contain the full project structure when I wake up and get to my computer) – bodand Feb 13 '18 at 20:47
  • @greenapps I'm not I follow, sorry I am kind of a noob – bodand Feb 13 '18 at 20:49
  • If this `assets` directory is in your `android` project and you are running the `desktop` project, then you need to set the working directory for the `desktop` project to be able to find it. See https://stackoverflow.com/questions/22822767/file-not-found-when-running-new-libgdx-project for tips how to do this in various IDEs. If you are running from the command-line, then use the gradle tasks which deal with this issue automagically. – Code-Apprentice Feb 13 '18 at 20:53
  • I set that, it works with the images perfectly, but for some reason it cannot find the yaml file – bodand Feb 13 '18 at 20:54
  • I added the full project directory – bodand Feb 14 '18 at 16:26

1 Answers1

0

So I was really stupid that is what happened. When the new Save is initialized it wasn't called inside the create() function, but before it

Well, sometimes I do stupid stuff. Sorry for asking stupid questions you could never answer as I didn't post the code.

bodand
  • 117
  • 3
  • 8