3

Firstable I'm sorry for my poor english. I'm developing an application in java for android, using libgdx. I want to write on a file to save some of the applications objects, and I'm using a ObjectOutputStream to do that. Here is an extract of the code:

            try {
                os_savedGame = new ObjectOutputStream(new FileOutputStream("savedGame.txt"));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                os_savedGame.writeObject(obj1);
                os_savedGame.writeObject(obj2);
            } catch (IOException e) {
                 e.printStackTrace();
            }

I ran it as a Desktop application and so far so good, but when I tried to run as an Android App I got the following error: Attempt to invoke virtual method 'void java.io.ObjectOutputStream.writeObject(java.lang.Object)' on a null object reference

I know that on the Desktop version, the file is stored in the android\assets folder, but I looked for the equivalent folder in my mobile phone and I couldn't find it. I searched for data/name_of_my_project in both internal and external memory and I wasn't able to find it. As previously I had a similar error, when I wasn't able to load a skin in the android version (saying that the skin doesn't exist) while I could in the desktop version, I think that my problem might be something related where the files are being stored in the android version. Can someone give me a tip on that please?

Thanks!

PR16
  • 39
  • 3

1 Answers1

2

I would recommend that you read the official documentation: https://github.com/libgdx/libgdx/wiki/File-handling

Internal files are relative to the application’s root or working directory on desktops, relative to the assets directory on Android, and relative to the core/assets/ directory of your GWT project. These files are read-only.

It mentions that files stored in your assets folder are read-only. For saving your game state I would recommend using the Preferences API which allows you to store key-value pairs and retrieve them easily. More information here: https://github.com/libgdx/libgdx/wiki/Preferences.

For converting your objects into strings here is another answer: How to serialize an object into a string

If you still want to write to a file you will have to use local storage which is explained further in the wiki (see top-most link).

Community
  • 1
  • 1
Basim Khajwal
  • 946
  • 6
  • 6
  • Thanks for the answer... I tried to follow the first link unsuccessfuly. I'd already read something about Preferences, but it is a requisite for this game to write and read files (teacher asked it). Anyway, as I say before, I think I had a similar problem when loading a skin. I got the following error: – PR16 May 16 '16 at 13:54
  • 05-16 14:49:49.235: E/AndroidRuntime(10163): com.badlogic.gdx.utils.GdxRuntimeException: Asset not loaded: uiskin.json at com.badlogic.gdx.assets.AssetManager.get(AssetManager.java:144) at screens.TestScreen.(TestScreen.java:29) at com.mygdx.game.MyGdxGame.create(MyGdxGame.java:27) at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:290) at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1505) at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240) – PR16 May 16 '16 at 13:59
  • Your error is because you need to load from an asset manager before you can retrieve the file, please read the documentation (or ask any questions if you are still confused - I'm only a student at 15 too :) ) – Basim Khajwal May 17 '16 at 16:59
  • I I'm doing that. It works in the desktop version. I only have this error in the android version – PR16 May 17 '16 at 18:31