nullpointerexception
程序代码:
/** * Created by Kaosaier on 7/3/2017. */ import java.util.Random; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class PongBall { private final int TABLE_WIDTH = 300; private final int TABLE_HEIGHT = 400; private final int RACKET_Y = 340; private final int RACKET_HEIGHT = 20; private final int RACKET_WIDTH = 80; private final int BALL_SIZE = 20; private final int BWidth = 100; private final int BHeight = 20; private int BrickX = 0; private int BrickY = 0; private final int[] Bx = {0 , 100 , 200 , 0 , 100 , 200 , 0 , 100 , 200 , 0 , 100 , 200 , 0 , 100 , 200 , 0 , 100 , 200 , 0 , 100 , 200 , 0 , 100 , 200 , 0 , 100 , 200 , 0 , 100 , 200}; private final int[] By = {0 , 0 , 0 , 20 , 20 , 20 , 40 , 40 , 40 , 60 , 60 , 60 , 80 , 80 , 80 , 100 , 100 , 100 , 120 , 120 , 120 , 140 , 140 , 140 , 160 , 160 , 160 , 180 , 180 , 180}; private Button play; private Button retry; private Button exit; private Frame f = new Frame("PongBall"); Random rand = new Random(); private int ySpeed = 5; private double xyRate = rand.nextDouble() - 0.5; private int ballX = 150; private int ballY = 70; private int racketX = 150; private boolean isLose = false; private MyCanvas tableArea; Timer timer; private int xSpeed = (int)(ySpeed * xyRate * 2); public void init()throws NullPointerException { play.setLabel("Play"); retry.setLabel("Retry"); exit.setLabel("Exit"); f.add(play); f.add(retry); f.add(exit); play.setVisible(true); play.setBounds(100 , 290 , 100 , 50); play.setActionCommand("game start"); retry.setVisible(false); retry.setBounds(50 , 220 , 50 , 50); retry.setActionCommand("restart"); exit.setVisible(false); exit.setBounds(100 , 220 , 50 , 50); exit.setActionCommand("exit"); tableArea.setPreferredSize(new Dimension(TABLE_WIDTH , TABLE_HEIGHT)); f.add(tableArea); ActionListener actionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String Command = e.getActionCommand(); if(Command.equals("game start")){ tableArea = new MyCanvas(); play.setVisible(false); } if (Command.equals("restart")){ tableArea = new MyCanvas(); retry.setVisible(false); } if (Command.equals("exit")){ System.exit(0); } } }; KeyAdapter keyProcessor = new KeyAdapter() { public void keyPressed(KeyEvent ke) { if (ke.getKeyCode() == KeyEvent.VK_LEFT) { if (racketX > 0) racketX -= 10; } if (ke.getKeyCode() == KeyEvent.VK_RIGHT) { if (racketX < TABLE_WIDTH - RACKET_WIDTH) racketX += 10; } } }; f.addKeyListener(keyProcessor); tableArea.addKeyListener(keyProcessor); ActionListener taskPerformer = evt -> { System.out.println("x: " + ballX + " y: " + ballY); if (ballX <= 0 || ballX >= TABLE_WIDTH - BALL_SIZE) { xSpeed = -xSpeed; } if (ballX >= racketX && ballX <= racketX + RACKET_WIDTH - BALL_SIZE/2 && ballY == RACKET_Y - BALL_SIZE || ballY <= 0) { ySpeed = -ySpeed; } if (ballY > RACKET_Y - BALL_SIZE) { timer.stop(); isLose = true; tableArea.repaint(); retry.setVisible(true); exit.setVisible(true); } ballY += ySpeed; ballX += xSpeed; tableArea.repaint(); }; timer = new Timer(45, taskPerformer); timer.start(); f.pack(); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.setVisible(true); } public static void main(String[] args) throws NullPointerException { new PongBall().init(); } class MyCanvas extends Canvas { public void paint(Graphics g) { if (isLose) { g.setColor(new Color(0 , 17, 255)); g.fillRect(0 , 0 , 300 , 400); g.setColor(new Color(254, 255, 6)); g.setFont(new Font("Times" , Font.ITALIC, 30)); g.drawString("game over:-(" , 75 ,200); } else { g.setColor(new Color(63, 58, 62)); g.fillRect(0 , 0 , 300 , 400); g.setColor(new Color(253, 255, 94)); g.fillOval(ballX , ballY , BALL_SIZE, BALL_SIZE); g.setColor(new Color(165, 163, 200)); g.fillRect(racketX , RACKET_Y, RACKET_WIDTH , RACKET_HEIGHT); for (int i = 0;i >= 30;i++){ for (int x = 0;x >= 30;x++){ BrickX = Bx[x]; } for (int y = 0;y >= 30;y++){ BrickY = Bx[y]; } g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255))); g.fillRect(BrickX , BrickY , BWidth , BHeight); } } } } }
nullpointerexception已经throws了,可是还抛出nullpointerexception,怎么回事?