| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2532 人关注过本帖
标题:老是空指针异常,改了好些遍,都没成功,大家帮我看一下吧
只看楼主 加入收藏
jjyy9822
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-6-24
收藏
 问题点数:0 回复次数:5 
老是空指针异常,改了好些遍,都没成功,大家帮我看一下吧


import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Demo extends JFrame {
    MyJPanel myPanel;
   
    public static void main(String[] args) {
        Demo demo = new Demo();
    }
   
    public Demo(){
        myPanel = new MyJPanel();
        //System.out.println(myPanel);
        this.add(myPanel);
        MyListener listener = new MyListener(myPanel);
        this.addKeyListener(listener);
        this.setSize(400,300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
}

class MyJPanel extends JPanel{
    int x = 10;
    int y = 10;
    int speed = 1;
   
    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public int getX() {
        return x;
    }
   
    public void setX(int x) {
        this.x = x;
    }
   
    public int getY() {
        return y;
    }
   
    public void setY(int y) {
        this.y = y;
    }
   
   
    public void paint(Graphics g){
        super.paint(g);
        g.fillOval(x,y,10,10);
    }
}

class MyListener implements KeyListener{
   
    MyJPanel myPanel;
   
        //此处报告空指针异常
    int x = myPanel.getX();
    int y = myPanel.getY();
   
    int speed = myPanel.getSpeed();
   
    public MyListener(){
        
    }
   
    public MyListener(MyJPanel myPanel){
        this.myPanel = myPanel;
    }
   
    public void keyTyped(KeyEvent e) {
        
    }

   
    public void keyPressed(KeyEvent e) {
        
        if (e.getKeyCode() == KeyEvent.VK_W) {
            myPanel.setY(y - speed);
        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            myPanel.setY(y + speed);
        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            myPanel.setX(x - speed);
        } else if (e.getKeyCode() == KeyEvent.VK_D) {
            myPanel.setX(x + speed);
        }
        myPanel.repaint();
        
    }

   
    public void keyReleased(KeyEvent e) {
        
    }
   
}



备注那里为什么会出现空指针异常  求大神帮忙解决
搜索更多相关主题的帖子: listener public import 
2016-06-24 21:39
jjyy9822
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-6-24
收藏
得分:0 
Exception in thread "main" java.lang.NullPointerException
    at cn.itcast_02.MyListener.<init>(Demo.java:70)
    at cn.itcast_02.Demo.<init>(Demo.java:21)
    at cn.itcast_02.Demo.main(Demo.java:14)

2016-06-24 21:43
dc小菜鸟
Rank: 2
等 级:论坛游民
威 望:2
帖 子:6
专家分:22
注 册:2016-7-15
收藏
得分:0 
在你注释的地方你是声明了一个 myPanel变量,但是你没有给这个变量赋一个对象,这个变量的引用就是null,所以会报exception。你可以把 myPanel打印出来看看是不是null

[此贴子已经被作者于2016-7-15 18:23编辑过]

2016-07-15 18:20
平常心q
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:120
专家分:550
注 册:2016-3-31
收藏
得分:0 
MyListener listener = new MyListener(myPanel);
this.addKeyListener();

这两行代码有问题,注释后,不会报错,但按键就有问题了。这说明按键有问题
2016-07-15 20:23
Joss
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2016-7-8
收藏
得分:0 
回复 2楼 jjyy9822
package demo01;

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test01 extends JFrame {
    MyJPanel myPanel;
   
    public static void main(String[] args) {
        Test01 demo = new Test01();
    }
   
    public Test01(){
        myPanel = new MyJPanel();
        //System.out.println(myPanel);
        this.add(myPanel);
        MyListener listener = new MyListener(this.myPanel);
        this.addKeyListener(listener);
        this.setSize(400,300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
}

class MyJPanel extends JPanel{
    int x = 10;
    int y = 10;
    int speed = 1;
   
    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public int getX() {
        return x;
    }
   
    public void setX(int x) {
        this.x = x;
    }
   
    public int getY() {
        return y;
    }
   
    public void setY(int y) {
        this.y = y;
    }
   
   
    public void paint(Graphics g){
        super.paint(g);
        g.fillOval(x,y,10,10);
    }
}

class MyListener implements KeyListener{
   
    MyJPanel myPanel ;
   
        //此处报告空指针异常
   /* int x = myPanel.getX();
    int y = myPanel.getY();*/
    int x;
    int y;
    int speed;

    //int speed = myPanel.getSpeed();
   
    public MyListener(){
        
    }
   //修改之后
    public MyListener(MyJPanel myPanel){
        this.myPanel = myPanel;
        x = myPanel.getX();
        y = myPanel.getY();
        speed = myPanel.getSpeed();

    }
   
    public void keyTyped(KeyEvent e) {
        
    }

   
    public void keyPressed(KeyEvent e) {
        
        if (e.getKeyCode() == KeyEvent.VK_W) {
            myPanel.setY(y - speed -10);
        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            myPanel.setY(y + speed  +10);
        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            myPanel.setX(x - speed-10);
        } else if (e.getKeyCode() == KeyEvent.VK_D) {
            myPanel.setX(x + speed +10);
        }
        myPanel.repaint();
        
    }

   
    public void keyReleased(KeyEvent e) {
        
    }
   
}

这样修改可以运行
2016-07-15 22:23
stm32
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2017-4-30
收藏
得分:0 
2017-04-30 21:14
快速回复:老是空指针异常,改了好些遍,都没成功,大家帮我看一下吧
数据加载中...
 
   



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

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