I'm working on my first Java Application, a game. What I have now compiles fine, but when I run it in command prompt, command prompt repeatedly prints:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Spike.loadpic(Spike.java:50)
at Spike.paintspike(Spike.java:55)
at Move.paint(Move.java:106)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5217)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1532)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1455)
at javax.swing.RepaintManager.paint(RepaintManager.java:1252)
at javax.swing.JComponent._paintImmediately(JComponent.java:5165)
at javax.swing.JComponent.paintImmediately(JComponent.java:4976)
at javax.swing.RepaintManager$3.run(RepaintManager.java:811)
at javax.swing.RepaintManager$3.run(RepaintManager.java:794)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:794)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)
at javax.swing.RepaintManager.access$1100(RepaintManager.java:62)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1680)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
And it doesn't start the Application(When I load one less image it eventually stops and it does work).
These are the parts of code, which I think might cause the problem:
@SuppressWarnings("serial")
public class Move extends JPanel implements Runnable {
private synchronized void start(){
if(running){
return;
}
running = true;
thread = new Thread(this);
thread.start();
}
public void run(){
private Thread thread;
boolean running = false;
long tijd = System.nanoTime();
double ticks = 60.0;
double ns = 1000000000/ticks;
double delta=0;
while(running){
long tijd2 = System.nanoTime();
delta+=(tijd2-tijd)/ns;
tijd=tijd2;
if(delta>1){
player.move();
for(int i=0;i<zappelin.length;i++){
zappelin[i].movezappelin();
}
repaint();
delta--;
}
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
if(player.x<400){g2d.translate(0,0);}
else if(player.x+400>levellength){g2d.translate(-levellength+800,0);}
else{g2d.translate(-(player.x-400),0);}
player.paintplayer(g2d);
for(int i=0;i<ground.length;i++){
ground[i].paintground(g2d);
}
for(int j=0;j<spike.length;j++){
spike[j].paintspike(g2d);
}
for(int k=0;k<zappelin.length;k++){
zappelin[k].paintzappelin(g2d);
}
}
public static void main(String[] args){
/*Set frame, create Move game, etc */
game.start();
}
/*Some other stuff*/
}
public class Player{
loaded=false;
private void loadpic(){
player[0] = new ImageIcon("Images/Playa.png").getImage();
player[1] = new ImageIcon("Images/Playal.png").getImage();
player[2] = new ImageIcon("Images/Playar.png").getImage();
loaded = true;
game.repaint();
}
public void paintplayer(Graphics2D g){
if(!loaded){
loadpic();
}
if(loaded){
if(xa==-1){
g.drawImage(player[1], x, y, null);
}
else if(xa==1){
g.drawImage(player[2], x, y, null);
}
else{
g.drawImage(player[0], x, y, null);
}
}
}
/*Other stuff*/
}
public class Zappelin{
private void loadpic(){
zappelin[0] = new ImageIcon("Images/zappelinl.png").getImage();
zappelin[1] = new ImageIcon("Images/zappelinr.png").getImage();
zappelin[2] = new ImageIcon("Images/explosion.png").getImage();
loaded = true;
game.repaint();
}
public void paintzappelin(Graphics2D g) {
if(dead){return;}
if(!loaded){
loadpic();
}
else{
if(xaz==-1||xaz==0){g.drawImage(zappelin[0], xz, yz, null);}
else{g.drawImage(zappelin[1],xz,yz,null);
//g.drawImage(zappelin[2], 10,10, 64, 64, 0,0,64,64,null);
}
}
if(bullet){
g.setColor(Color.RED);
g.fillOval(xbullet,ybullet,4,4);
}
}
/*Other stuff*/
}
public class Spike{
private void loadpic(){
switch (dimension){
case "normaal":
spike = new ImageIcon("Images/Spike.png").getImage();
test=1337;
break;
case "links":
spike = new ImageIcon("Images/spikel.png").getImage();
break;
case "rechts":
spike = new ImageIcon("Images/spiker.png").getImage();
break;
case "boven":
spike = new ImageIcon("Images/spikeb.png").getImage();
break;
default:
spike = new ImageIcon("Images/Spike.png").getImage();
break;
}
loaded = true;
game.repaint();
}
public void paintspike(Graphics2D g) {
if(!loaded){
loadpic();
}
else{
if(dimension=="normaal"||dimension=="boven"){
for(int i=0; i<amount;i++){
g.drawImage(spike, xg+15*i, yg, null);}
}
if(dimension=="links"||dimension=="rechts"){
for(int i=0; i<amount;i++){
g.drawImage(spike, xg, yg+20*i, null);
}
}
}
}
}
I am aware that I should be using spritesheets instead of single images and I going to fix it, but I don't think that will solve this problem completely (commenting out all the image loading also doesn't fix it).
I can't find a solution to this anywhere, so it would be great if someone knows how to fix this.
EDIT: I tried some things out and it turns out that the command prompt eventually stops printing this error and starts the application when I only use g.drawImage(image, x, y, null) methods, but it repeats it infinitely when I use a g.drawImage(image, dx1,dy1,dx2,dy2,sx1,sy1,sx2,sy2,null) method.