Search code examples
androidscalalibgdx

libGDX Cannot read file from Gdx.files.internal


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


Solution

  • 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.