Load script and images from assets

Hello, I need help.
I need to upload images into memory for display by Nifty GUI.
I also need to load scripts from the assets.
I tried using what is mentioned here: http://hub.jmonkeyengine.org/forum/topic/loading-txt-files-from-assets/

But not working.

What I can do ?, what I need is to get more images of assets to then dynamically load the Java code.

[java]public class Main extends SimpleApplication {

public static void main(String[] args) {
    Main app = new Main();
    app.start();
}

public class FileLoader implements AssetLoader {

    public Object load(AssetInfo assetInfo) throws IOException {
        Scanner scan = new Scanner(assetInfo.openStream());
        StringBuilder sb = new StringBuilder();
        try {
            while (scan.hasNextLine()) {
                sb.append(scan.nextLine()).append('\n');
                System.out.println(sb);
            }
        } finally {
            scan.close();
        }
        return sb.toString();
    }
}

@Override
public void simpleInitApp() {
   assetManager.registerLoader(FileLoader.class, "txt");
   String input = (String) assetManager.loadAsset("Data/a.txt");
    System.out.println(">" + input);
}

@Override
public void simpleUpdate(float tpf) {
    //TODO: add update code
}

@Override
public void simpleRender(RenderManager rm) {
    //TODO: add render code
}

}[/java]

So where is the actuall problem?

On a first glance the above code should work, as long as your stuff is inside the assetlocators paths

[java]Grave: Cannot create locator of type mygame.Main$FileLoader, does the class have an empty and publically accessible constructor?
sep 08, 2014 9:39:01 AM com.jme3.app.Application handleError
Grave: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
at com.jme3.asset.DesktopAssetManager.loadAsset(DesktopAssetManager.java:288)
at com.jme3.asset.DesktopAssetManager.loadAsset(DesktopAssetManager.java:342)
at mygame.Main.simpleInitApp(Main.java:52)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:226)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:130)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:207)
at java.lang.Thread.run(Thread.java:744)

BUILD SUCCESSFUL (total time: 10 seconds)[/java]

and the file :

Image and video hosting by TinyPic

The answer to the question posed in the error message is “No it doesn’t”…

Ah, right missed that one, adding a public no args constructor should do it.

It will also need to be a static inner class or it won’t work either. (or make it a separate class) Non-static inner classes cannot be instantiated like normal classes.

Thanks @pspeed!

works, but to generate an executable not find the path.

Main.java
[java]public class Main extends SimpleApplication {

public static void main(String[] args) {
    Main app = new Main();
    app.start();
}

@Override
public void simpleInitApp() {

   assetManager.registerLoader(XMLLoader.class, "txt");
   String input = (String) assetManager.loadAsset("Data/a.txt");
    System.out.println("> RETURN " + input); 
  
}

}
[/java]

XMLLoader.java
[java]public class XMLLoader implements AssetLoader {

    public Object load(AssetInfo assetInfo) throws IOException {
        Scanner scan = new Scanner(assetInfo.openStream());
        StringBuilder sb = new StringBuilder();
        try {
            while (scan.hasNextLine()) {
                sb.append(scan.nextLine()).append('\n');
            }
        } finally {
            scan.close();
        }
        return sb.toString();
    }
}

[/java]

when I run it, I get:

Image and video hosting by TinyPic

but when I clean and build it and run. I get:

Image and video hosting by TinyPic

AssetNotFoundException :frowning:

Is the file in your assets directory? Did you do all of the asset-related tutorials?

Yes… The file is where it belongs, moreover, to execute works, but when ‘Clean & Build’ does not work.

Image and video hosting by TinyPic

The problem is to compile because the assets stored on a jar file and then you can not find the path.


I need this to load a script.
To load images I solved it with:

[java]NiftyImage oNiftyImage = oNifty.getRenderEngine().createImage(oNifty.getScreen(sNameScreen), sPathOfImageAsset, false);[/java]

@fdiaz said: The problem is to compile because the assets stored on a jar file and then you can not find the path.

This statement makes no sense to me. Either you are using a different definition of “compile” (in my definition, compile is turning .java files into .class files or some other actually compiling of source files into binaries) or you are still confused about how assets work.

All assets are in the assets.jar file. This is not a problem. I don’t know what “find the path” means in this case.

I think there is some part of your story that I’m missing. Though really I know little to nothing about nifty these days but it uses JME’s asset manager when used with JME… so…