Heya guys, i have been trying to make this very famous ball game using java swing. although the primary code ihave used is from this link [Click Here](http://www.edu4java.com/en/game/game6.html)but been trying to modify it to make it look less complex..But everytime i run it it gives me following error:
Exception in thread "main" java.lang.NullPointerException
Exception in thread "main" java.lang.NullPointerException
at ballGame.Ball.collision(mainGame.java:209)
at ballGame.Ball.move(mainGame.java:196)
at ballGame.mainGame.main(mainGame.java:65)
so, plz can anybody help me with this??
package ballGame;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
class mainGame
{
private static Graphics2D g;
public static void main(String args[])
{
JFrame mainFrame=new JFrame();
mainFrame.setLocationByPlatform(true);
mainFrame.setLocation(500,60);
Racket rkt = new Racket(mainFrame);
Ball bl = new Ball(mainFrame);
addKeyListener(new KeyListener(){
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
rkt.keyReleased(e);
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
rkt.keyPressed(e);
}
});
setFocusable(true);
bl.move();
rkt.move();
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
bl.paint(g2d);
rkt.paint(g2d);
mainFrame.setSize(300, 400);
mainFrame.setVisible(true);
mainFrame.setResizable(false);
}
private static void setFocusable(boolean b) {
// TODO Auto-generated method stub
}
private static void addKeyListener(KeyListener keyListener) {
// TODO Auto-generated method stub
}
}
class Racket
{
private static final int Y=330, width=60,height=10;
int x=0, xa=0;
JFrame jf;
Racket()
{
}
Racket(JFrame j)
{
jf=j;
}
public void move()
{
if(x+xa>0 && x+xa < jf.getWidth()-width)
{
x+=xa;
}
}
public void paint(Graphics2D g)
{
g.fillRect(x,Y,width,height);
}
public void keyReleased(KeyEvent e)
{
xa=0;
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
xa=-1;
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
xa=1;
}
}
public Rectangle getBounds()
{
return new Rectangle(x,Y,width,height);
}
public int getTopY()
{
return Y;
}
}
class Ball
{
private static final int diameter=30;
int x=0;
int y=0;
int xa=1; //velocity for x co-ordinate
int ya=1; // velocity for y co-ordinate
JFrame jf;
Racket rt;
Ball(JFrame j)
{
jf=j;
}
public void move()
{
if(x+xa<0)
{
xa=1;
}
if(x+xa>jf.getWidth()-diameter)
{
xa=-1;
}
if(y+ya<0)
{
ya=1;
}
if(y+ya>jf.getWidth()-diameter)
{
ya=-1;
}
if(y+ya>jf.getHeight()-diameter)
{
// gameover();
}
if(collision())
{
ya=-1;
y=rt.getTopY()-diameter;
}
x=x+xa;
y=y+ya;
}
private Boolean collision()
{
return rt.getBounds().intersects(getBounds());
}
public void paint(Graphics2D g)
{
g.fillOval(x, y, diameter, diameter);
}
public Rectangle getBounds()
{
return new Rectangle(x,y,diameter,diameter);
}
}