assetManager.loadModel results in AssetNotFound Exception - java

The model location has been added to the class path. I also tried importing the model which placed it in a Users folder in the project. The .loadModel statement works in jmonkey but not in java:
rotor1 = assetManager.loadModel("Models/Rotor2/Rotor2.mesh.j3o");
I've tried several alternatives to ("Models/Rotor2/Rotor2.mesh.j3o")
("C:/Users/Eugene/Documents/JMonkey/BasicGame1/assets/Models/Rotor2")
("Rotor2.mesh.j3o")
("Users/Rotor2.mesh.j3o)
The actual exception is shown as:
Uncaught exception thrown in thread[LWJGL Renderer Thread,5,main]
AssetNotFoundException: Model/Rotor2/Rotor2.mesh.j3o
I've also tried other variations of the .loadModel statement, with the same error.
I could really use some help with this.

When you load a model it uses the jME3 asset system to do so. If you use the SDK to create your projects then it will create an assets folder for you and you can place the assets within that. The assets are actually embedded within the JAR of your program.
If you want to load assets from another location such as a folder on the local hard drive you will need to register your own asset locator. You can find this described here:
http://hub.jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_asset#loading_assets_from_custom_paths

Related

Is a 'res' class folder still necessary in Java when loading images?

I'm working on a game, and I've stumbled across a fews ways to load an image into my program. There are two common ways:
The first (No class folder needed):
ImageIcon ii = new ImageIcon("image.png");
Image i = ii.getImage();
When using this method, you simply add a folder to your Project Folder (In Eclipse) and DO NOT have to make it an external class folder.
The second option (Class folder needed):
Image i = ImageIO.read(ImageLoader.class.getResource("image.png"));
You can only load images this way if you made a resource folder (typically called 'res') and added it as an external class folder to your project.
My question is why would you use the second option instead of the first? From my limited experience, it seems like the first option works just the same as the second, just without having to turn the folder you're adding it to into an external class folder.
The first option doesn't work if you package your application as a jar. The second option works in both cases, as an exploded application in an ide or on the hard drive and packaged in a jar file.
new ImageIcon("image.png"); assumes that the image is stored on the file system (in your case, in the current working directory), which raises up some issues - as the working directory isn't always the same location that the jar is stored.
ImageIO.read(ImageLoader.class.getResource("image.png")) embeds the image within the Jar file which resolves the issues associated with the first method as the image is where ever the Jar file is.
It also simplifies installation, as you only need to track the jar files and not all the other "external" files.
ImageIcon doesn't report the reasons it fails. It loads the images in a separate thread, which make diagnosing issues difficult.
ImageIO loads the image in the current thread (and returns a fully realised image) but will also generate a IOException if the image fails to load for some reason, making a much better way to load images in general. It also supports (or has the capacity to support) a wider range of images.
In general, using ImageIO and embedded images is simpler and easier to work with and is generally the recommended approach for fixed resources

Referencing an image inside a jar file when running junit

I have been playing with the idea of using ImageMagic (im4java) to do a comparison of known good page renders against stored good pages.
I have got this working on a test site, but all my images (good, bad and differrent) are stored in my c:\temp folder. I have been toying with the idea of having the "expected" images kept inside the project folder structure, so when the project is checked out, the expected images are there.
(not saying this is a great solution, this is just something I have been playing with.)
So my test is stored in
/src/test/java/my.screen.test/compareTest.java
and I have my "expected" image in
/masterImages/test.png
I have tried various ways to reference this:
I included masterImages in the build path and then tried to use
InputStream input = getClass().getResourceAsStream("/masterImages/googleHomePage1.png");
(I then thought I could simply use input.toString() to pass into im4java - but the InputStream gave me nullpointer exception)
I also tried removing the masterImages from the buildpath and trying it that way.
I have also tried
String path = getClass().getClassLoader().getResource("masterImages/googleHomePage1.png").toString();
Again, null pointer. I know there is something stupid I am not seeing here, like I said this started as me playing but it's now annoying me why I can't get it to work.
Any insights into what I am missing greatly appreciated.
From - In java, how do you retrieve images from a jar file?
It is indeed simple: you use the various getResource() methods in java.lang.Class and java.lang.ClassLoader. For example, in your app, you could just write
treeURL = getClass().getResource("/images/tree.png");
This would find the file in an images directory at the root of the jar file. The nice thing about the getResource() methods is that they work whether the files are in a jar or not -- if the images directory is a real directory on disk, this will still work (as long as the parent of images is part of your class path.)
Thanks to all. I think I have this sorted. Turns out I had to add the folder containing the image to the classpath - I wrongly assumed that as I had masterImages/otherFolderName that any file inside otherFolderName would be included if I included masterImages. Turns out this is not the case (at least for me.)

Null pointer exception while loading image in JApplet

I am gettting a nullpointer excetion when i run the applet in browser, while its working fine through appletviewer.
titleicon=new ImageIcon(this.getClass().getClassLoader().getResource("image.jpg"));
img=new JLabel(titleicon)
File locations: Both .class file and image file are in same folder.
C:\project\game.class
C:\project\image.jpg
I tried differennt variations below, none is working in browser but all are fine with appletviewer:
titleicon=new ImageIcon(this.getClass().getResource("image.jpg"));
titleicon=new ImageIcon(getClass().getClassLoader().getResource("image.jpg"));
With a file location of
C:\project\game.class
C:\project\image.jpg
Class.getResource will never work, as this searches the classpath for the required resources.
The use of getResource assumes that the resources are embedded and are relative to the class path.
Without more details, it's difficult to make an accurate suggestion, but I would suggest that you get the images jared without your application code, this way, it will be easier to find.
If you're not using an IDE capable of Jaring your classes and resources, then you will need to have a look at Packaging Programs in JAR Files
Remember, Class#getResource is capable of doing a relative lookup. If the images are not stored in the same package as the class which is loading them, then you need to use an absolute path instead...

null pointer exception even though photo there

I realize this question has been asked in various ways, but I couldn't fix my problem despite this. I'm sure I've done everything correctly and yet it still throws the null pointer exception. Is this a Windows 7 issue with Eclipse?
I'm attaching a screen shot so you can see exactly how I have things laid out and that it isn't working still. I really want to trouble shoot this so I can move on in the tutorial I'm doing. This is so frustrating to be hinged on something so trivial!! My image star.png is located inside the image folder which is in the src folder where the package resides as well. Thank you!
To load an image from within an Eclipse project:
Create a new Source Folder. Do not use a regular folder like the
one already in your workspace.
Create a new package named "star3.images" in the folder you just created.
Copy the images you want (in your case, "star.png") into the package.
Load it using the following:
ImageIcon ii = new ImageIcon(getClass().getResource("images/star.png"));
For all other images, replace "star.png" with the name of whatever image you wish to load.
project; properties; java build path; libraries; add class folder
Add a class folder (called res or something) into the project folder then use:
new ImageIcon(this.getClass().getResource("/res/star.png")).getImage();

Java: NullPointerException from class.getResource( ... )

I was writing a small application and when I tried to create an ImageIcon I always got an exception. The exception was caused by this line of code:
prayerLevel.setIcon(new ImageIcon(getClass().getResource("/icons/icon_prayer.png")));
Now within my program, the folder /icons/ does exist. I don't know if it makes the difference but the class file is within a package, where as the icons folder is within the project folder (when you would see the bin and src folder).
I have looked around for a bit and I couldn't find a solution that could help me solve the problem. Perhaps any of you guys could help?
Edit: someone asked for my folder hierarchy:
I know the class file is not in the same folder as the icons are, but I've made applications where I had to load files from a different folder and doing /folder/ always used to work.
Edit 2:
System.out.println(getClass().getResource("/icons/icon_prayer.png") == null);
Prints true.
I believe the NPE is being thrown from the ImageIcon constructor as getResource is returning null.
Try the following:
getClass().getClassLoader().getResource("/icons/icon_prayer.png")
Or:
ClassLoader.getSystemResource("/icons/icon_prayer.png")
As far as I know getResource() will look into locations of known resources, in other words if the folder /icons/ is not seen as a resource folder it will not as you had expected. There are two ways of going around this as far as I know:
1) Set icons folder as a resource to the application, then you can use getResource() for instance
URL css_url = getClass().getResource("/resource/style.css");
For more info on this option, see http://lj4newbies.blogspot.com/2008/03/using-classgetresource-load-resource.html
2) Get the icon as a regular file without using getResource() method. This is actually adviced in Swing tutorials on Sun/Oracle own documentation .
Generally, applications provide their own set of images used as part
of the application, as is the case
with the images used by many of our
demos. You should use the Class
getResource method to obtain the path
to the image. This allows the
application to verify that the image
is available and to provide sensible
error handling if it is not. When the
image is not part of the application,
getResource should not be used and the
ImageIcon constructor is used
directly. For example:
ImageIcon icon = new
ImageIcon("images/middle.gif",
"a pretty but meaningless splat");
Hope this helps, good luck!
Old thread but since I bumped into a similar problem just now...
I'm using Eclipse and I copied a file to the "resources" folder using system commands (cp). However, eclipse threw a NullPointerException because I didn't refresh the "resources" folder. So the file was there but Eclipse didn't see it.
So in Eclipse: "Package Explorer" -> "resources" -> Mouse right click -> refresh. This fixed it for me.
I added my music, images, etc to a folder added to the build path. Then I just used
URL url="CurrentClass".class.getClassLoader().getResource("media file name not the path");
setIconImage(new ImageIcon(url.getPath()).getImage());
to set the image icon.
The only thing that can throw a NullPointerException in this line of code is the first ., which means that prayerLevel is null.

Categories