| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2078 人关注过本帖
标题:nullpointerexception
只看楼主 加入收藏
kaosaier
Rank: 5Rank: 5
等 级:职业侠客
威 望:7
帖 子:124
专家分:303
注 册:2016-7-23
结帖率:88.24%
收藏
已结贴  问题点数:1 回复次数:2 
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,怎么回事?
搜索更多相关主题的帖子: public private int exit new 
2017-07-09 00:13
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:1 
一直往上抛,一直不处理。为啥不处理?

剑栈风樯各苦辛,别时冰雪到时春
2017-07-09 08:00
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
    private Button play = new Button();
    private Button retry = new Button();
    private Button exit = new Button();

加上吧,或者其他方式使其实例化,空值调用到哪都是报错

试了下,有点晃眼,有空帮你改下。得上课了

[此贴子已经被作者于2017-7-9 08:03编辑过]


剑栈风樯各苦辛,别时冰雪到时春
2017-07-09 08:02
快速回复:nullpointerexception
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014959 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved