0

I am using collision to detect the red rectangle when the bullet from the oval hits it. But when I try to run it gives me these errors. The bullet only comes out when I press SPACE. I wanted to test it so that I can use it when I create a simple game.

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class JavaGame2 extends JPanel implements KeyListener,Runnable{

//variables
JFrame frame;
int x, y, xDir, yDir,bx,by;
Rectangle bullet;
boolean readyTofire, shot = false;


//constructor for game
public JavaGame2(){

    frame = new JFrame("Java Game");
    int x=150;
    int y=150;


}



//drawings
public void paintComponent(Graphics g){

    super.paintComponent(g);
    this.setBackground(Color.WHITE);

    Rectangle rec = new Rectangle(50, 20, 50, 50);
    g.setColor(Color.BLUE);
    g.fillOval(x, y, 55, 55);
    g.fillRect(x+23, y-15, 10, 20);
    g.setColor(Color.RED);
            g.fillRect(rec.x, rec.y, rec.width, rec.height);


    if(shot){
        g.setColor(Color.BLACK);
        g.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
    }


            //this collision part is giving me an error********************
    if(bullet.intersects(rec));
        g.drawString("collision!", 50, 20);


             repaint();

}

public void setxDir(int xdir){
    xDir = xdir;
}
public void setyDir(int ydir){
    yDir = ydir;
}



//key event listener keypressed

public void keyPressed(KeyEvent e) {
    int code = e.getKeyCode();
    if(code == KeyEvent.VK_UP){
        setyDir(-1);
     }
     if(code == KeyEvent.VK_DOWN){
        setyDir(+1);
     }
     if(code == KeyEvent.VK_LEFT){
        setxDir(-1);
     }
     if(code == KeyEvent.VK_RIGHT){
        setxDir(+1);

     }
     if(code == KeyEvent.VK_SPACE){
        if(bullet == null ){

            readyTofire = true;
            if(readyTofire){
                bx = x+26;
                by = y-15;
                bullet = new Rectangle(bx, by, 5, 3);
                shot = true;

            }

        }
     }
}

//key event listener for key released
public void keyReleased(KeyEvent e) {
    int code = e.getKeyCode();
    if(code == KeyEvent.VK_UP){
        setyDir(0);
     }
     if(code == KeyEvent.VK_DOWN){
        setyDir(0);
     }
     if(code == KeyEvent.VK_LEFT){
        setxDir(0);
     }
     if(code == KeyEvent.VK_RIGHT){
        setxDir(0);

     }
     if(code == KeyEvent.VK_SPACE){
         readyTofire = false;
         if(bullet.y <= -5){
             bullet = new Rectangle(0, 0, 0, 0);
             shot = false;
             readyTofire = true;

         }
     }
}

public void keyTyped(KeyEvent e) {

}

//shot of bullet
public void shoot(){

    if(shot){
        bullet.y--;
    }

}

//movement of the oval
public void move(){
    x += xDir;
    y += yDir;
    if(x <= 0){
        x = 0;
    }
    else if(x >= 500){
        x = 500;
    }
    else if(y <= 0){
        y = 0;
    }
    else if(y >= 500){
        y = 500;
    }

}

//thread
public void run() {

    try{
        while(true){
            shoot();
            move();



            Thread.sleep(5);
        }

    }
    catch(Exception e){
        System.out.println("error!");
    }
}




}

.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at JavaGame2.paintComponent(JavaGame2.java:47)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1100(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(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$1.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)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at JavaGame2.paintComponent(JavaGame2.java:47)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1100(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(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$1.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)
Renboy
  • 451
  • 4
  • 21

2 Answers2

2

In the paintComponent method you need to initialize bullet object of Rectangle to ensure there is no such exception

Balwinder Singh
  • 2,272
  • 5
  • 23
  • 34
  • I did and it ran but the red rectangle has already been detected and I haven't event pressed the space bar to shoot bullet yet. – Renboy Apr 10 '14 at 04:48
0

well if shot == false there is no bullet I'm assuming? So you would get an exception. Try putting asserts throughout your code when bullet should be null, or not null. That may catch the bug where it originates.

A simple fix is to change this

if(bullet.intersects(rec));

to

if(bullet != null && bullet.intersects(rec));
over_optimistic
  • 1,399
  • 2
  • 18
  • 27
  • There's no problem with the shot, it's this line if(bullet.intersects(rec)); g.drawString("collision!", 50, 20); – Renboy Apr 10 '14 at 04:12
  • yes. But you only instantiate bullet at the same time you make shot=true. What about the case when shot=true never has happened, but the draw cycle starts? – over_optimistic Apr 10 '14 at 04:20
  • I instantiated the bullet in the paintComponent and it somehow fixed the error. However, It has already detected the red rectangle and I haven't event pressed the space bar yet to shoot the bullet. Now what? – Renboy Apr 10 '14 at 04:45
  • I updated the answer. There might be a more deeper bug in your code though. Learn about java asserts to help you catch bugs. – over_optimistic Apr 10 '14 at 04:49
  • Thanks for your help, I will try to understand the rest of the problem. – Renboy Apr 10 '14 at 05:55