沉浸020903 2022-11-21 21:54 采纳率: 78.9%
浏览 2
已结题

java数据为空,类的问题

问题遇到的现象和发生背景

问题:我输入 true 和 5之后报错Cannot invoke "dianti.set(boolean, int)" because "this.a" is null
这是什么原因我要怎么改?
题目:5.编写一个程序,模拟电梯得功能。功能接口包括电梯上行按钮、下行按钮、楼层选择与电梯在行驶过程中得楼层显示。
(1) 由用户选择按上行按钮还就是下行按钮,选择操作后再由用户输入要进入得楼层,进而电梯开始运行,显示所到得每一楼层层数.
(2) 如果就是上行,则选择输入得楼层号不能比当前楼层号小,否则应给出不合法提示。
(3) 如果就是下行,则选择输入得楼层号不能比当前楼层号大,否则应给出不合法提示。
(4) 电梯一旦开始运作就会始终运行,直到窗口关闭。
(5) 电梯在经过不同楼层时,最好每个楼层得显示之间能有延迟,最终停靠得楼层得输出形式能更加醒目。如果可以,在电梯最初开始运行时,能在电梯由内部显示当前日期。

用代码块功能插入代码,请勿粘贴截图
import java.util.*;
public class sadfaf {

    public static void main(String[] args) {


        while (true){
            System.out.println("请输入上行true下行false");
            Scanner x=new Scanner(System.in);
            boolean l =x.nextBoolean();
            System.out.println("请输入你想去的楼层");
            Scanner y=new Scanner(System.in);
            int m =x.nextInt();
            user5 i=new user5(l,m);
            i.zuodianti(l,m);
        }
    }
}

public class dianti {
    int louceng=1;
    boolean shangxia; //上1下0
    void dianti1(int louceng){
        this.louceng=louceng;
    }
    public void set(boolean x,int y){
        this.shangxia=x;
        if(x){
            if(y>louceng){
                for(int i=louceng+1;i<y+1;i++){
                    System.out.println("当前楼层为"+i);
                    try {
                        Thread.sleep(2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    dianti1(i);
                }
            }
            else
                System.out.println("输入错误");
        }
        if(!x){
            if(y<louceng){
                for(int i=louceng-1;i>y-1;i--){
                    System.out.println("当前楼层为"+i);
                    try {
                        Thread.sleep(2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            else
                System.out.println("输入错误");
        }
    }
    void show(){
        System.out.println("当前楼层为"+louceng);
    }
}


public class user5 {
    boolean x;
    int y;
    dianti a;
    public user5(boolean x,int y){
        this.x=x;
        this.y=y;
    }
    void zuodianti(boolean x,int y){
        a.set(x,y);
    }
}
运行结果及报错内容

请输入上行true下行false
true
请输入你想去的楼层
5
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "dianti.set(boolean, int)" because "this.a" is null
at user5.zuodianti(user5.java:10)
at sadfaf.main(sadfaf.java:32)

我想要达到的结果

程序正常运行

  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2022-11-21 22:21
    关注

    dianti对象为空,导致调用方法报错了。

    
    public class user5 {
        boolean x;
        int y;
        dianti a=new dianti();
        public user5(boolean x,int y){
            this.x=x;
            this.y=y;
        }
        void zuodianti(boolean x,int y){
            a.set(x,y);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月21日
  • 已采纳回答 11月21日
  • 创建了问题 11月21日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。