Reputation: 1217
I am running my project on NetBeans but I am not able to run .jar file successfully. 1 . I was developing my project using Java Class library , and then later found there is some problem in manifest files . . 2. Then to solve above problem I created project as JavaApplication project, now manifest looks like this
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.6.0_22-b22 (Sun Microsystems Inc.)
Class-Path:
Main-Class: gameloftbraker.GameLoftBraker
on terminal it throws following exception:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:204)
at gameloftbraker.Ball.<init>(Ball.java:26)
at gameloftbraker.Arena.gameInit(Arena.java:71)
at gameloftbraker.Arena.addNotify(Arena.java:67)
at java.awt.Container.addNotify(Container.java:2584)
at javax.swing.JComponent.addNotify(JComponent.java:4687)
at java.awt.Container.addNotify(Container.java:2584)
at javax.swing.JComponent.addNotify(JComponent.java:4687)
at java.awt.Container.addNotify(Container.java:2584)
at javax.swing.JComponent.addNotify(JComponent.java:4687)
at javax.swing.JRootPane.addNotify(JRootPane.java:754)
at java.awt.Container.addNotify(Container.java:2584)
at java.awt.Window.addNotify(Window.java:707)
Thanks for help in advance. I hope I am not missing anything. There is no problem in code as it build and runs successfully , If code is needed let me know .
Ball.java
////////////////
package gameloftbraker;
import javax.swing.ImageIcon;
public class Ball extends Graphc implements Commn {
protected String ball = "../images/ball.png"; //address to ball graphic
protected int xdir;
protected int ydir;
public Ball(){
xdir = 2;
ydir = -2;
ImageIcon img = new ImageIcon(this.getClass().getResource(ball));
image = img.getImage();
height = image.getHeight(null);
width = image.getWidth(null);
resetState();
}
// rest of the functions * it works perfectly as project but unable to run in .jar
}
Upvotes: 1
Views: 1697
Reputation: 1500345
It would help if you'd show us the code for Ball
, highlighting the lines shown in the stacktrace... but my guess is that you haven't bundled the graphics for your game into the jar file correctly.
One thing to point out:
There is no problem in code as it build and runs successfully
Well it's not running successfully now, is it? Building and running successfully in one environment doesn't mean there's no problem in the code. For example, you could have hard-coded filenames in there which aren't present on any other machine - I'm not suggesting that actually is the case here, I'm just saying that "works on my machine" isn't proof that there's no problem in the code.
EDIT: Now that we can see your code, it looks like ball.png
may not be in the jar file. Or the problem is your use of ..
in a resource name. Try changing the resource name to:
// TODO: Make this private static final unless you have any reason not to...
protected String ball = "/images/ball.png";
Upvotes: 1
Reputation: 830
If you want to use data (e.g. picture) outside the jar just use this:
ImageIcon img = new ImageIcon(ball);
on IDE run you have only folder, so it works. In jar file you have to leave the jar file and I think it's not possible with your way
Upvotes: 1