在JARing我的太空入侵者游戏之前,我测试了它。工作得很好。没有错误。现在,在我JAR之后,当我尝试运行它时,我在控制台中出现以下错误,以及上面的以下消息:
<terminated> SI[Java Application]C:\Program Files\Java\jre1.0.8_121\bin\javaw.exe(Dec 8, 2017, 5:52:28 PM)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sun.applet.AppletAudioClip.<init>(Unknown Source)
at java.applet.Applet.newAudioClip(Unknown Source)
at SIship.<init>(Unknown Source)
at SIbase.<init>(Unknown Source)
at SIpanel.userBaseAndEnemyInvaderCreation(Unknown Source)
at SIpanel.resetFields(Unknown Source)
at SIpanel.<init>(Unknown Source)
at SI.<init>(Unknown Source)
at SI$7.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我无法弄清问题是什么。除了命名几个类之外,它没有让我看到问题出在哪里。就我所见,JAR完美地运行程序,但现在我无法在Eclipse中运行它。
这是我的ANT JAR代码。关于什么可能出错的任何想法,如果有的话?
<?xml version="1.0" ?>
<project default="main">
<property name="folder.source" value="../src/" />
<property name="folder.class" value="../bin/" />
<property name="jar.name" value="../spaceinvaders.jar" />
<target name="main" depends="clean, compile, jar">
<echo>success</echo>
</target>
<target name="compile">
<echo>compiling...</echo>
<javac srcdir="${folder.source}" destdir="${folder.class}" />
<echo>done!</echo>
</target>
<target name="clean">
<echo>cleaning...</echo>
<delete dir="${folder.class}" includes="**/*" />
<delete file="${jar.name}" />
<echo>done!</echo>
</target>
<target name="jar">
<echo>JARing...</echo>
<jar destfile="${jar.name}">
<fileset dir="${folder.class}">
<include name="**/*.class" />
</fileset>
<fileset dir="${folder.source}">
<include name="**/*.gif" />
</fileset>
<fileset dir="${folder.source}">
<include name="**/*.wav" />
</fileset>
<manifest>
<attribute name="Main-Class" value="SI" />
</manifest>
</jar>
<echo>done!</echo>
</target>
</project>
SIship.Java
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Graphics2D;
import java.awt.Image;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;
public abstract class SIship extends SIthing {
protected Image imageDead;
private AudioClip aud = Applet
.newAudioClip(getClass().getResource("invaderkilled.wav"));
protected boolean active = true;
public SIship(int x, int y, int wid, int height, String hitImage) {
super(x, y, wid, height);
imageDead = getImage(hitImage);
}
protected boolean isItHit(SImissile mis) {
if (active == false) {
return false;
}
else {
return this.getX() < mis.getX() + mis.getWidth()
&& this.getX() + this.getWidth() > mis.getX()
&& this.getY() < mis.getY() + mis.getHeight()
&& this.getY() + this.getHeight() > mis.getY();
}
}
protected void hit() {
SwingUtilities.invokeLater(() -> aud.play());
active = false;
}
@Override
abstract public void draw(Graphics2D g2);
protected Image getImage(String filename) {
URL url = getClass().getResource(filename);
ImageIcon icon = new ImageIcon(url);
return icon.getImage();
}
}
答案:我明白了。原来......
<delete dir="${folder.class}" includes="**/*" />
...每次运行时都会删除项目中的所有.classes。糟糕。