Java NullPointerException&;具有继承性和子类的IOStream突然闭包

Java NullPointerException&;具有继承性和子类的IOStream突然闭包,java,io,nullpointerexception,class-hierarchy,Java,Io,Nullpointerexception,Class Hierarchy,之前有一个简短的背景,这样我们就可以在相同的波长上进行交流。我已经有大约8-10个大学编程课程,从数据结构到所有语言,到java和C++等特定的课程。我有点生疏了,因为我通常从编码开始休息2-3个月。这是我两年前开始考虑的一个个人项目 好的,具体到细节,还有一个特定的问题,我的变异函数有问题。似乎我试图错误地访问私有变量。问题是,我是否嵌套了太多的类,并试图以错误的方式变异基类变量。如果是这样,请为我指出正确的文献,或确认这是我的问题,以便我可以重新研究这些信息。谢谢 package Groce

之前有一个简短的背景,这样我们就可以在相同的波长上进行交流。我已经有大约8-10个大学编程课程,从数据结构到所有语言,到java和C++等特定的课程。我有点生疏了,因为我通常从编码开始休息2-3个月。这是我两年前开始考虑的一个个人项目

好的,具体到细节,还有一个特定的问题,我的变异函数有问题。似乎我试图错误地访问私有变量。问题是,我是否嵌套了太多的类,并试图以错误的方式变异基类变量。如果是这样,请为我指出正确的文献,或确认这是我的问题,以便我可以重新研究这些信息。谢谢

package GroceryReceiptProgram;

import java.io.*;
import java.util.Vector;

public class Date {

    private int hour, minute, day, month, year;

    Date() {
        try {
            BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("What's the hour? (Use 1-24 military notation");
            hour = Integer.parseInt(keyboard.readLine());
            System.out.println("what's the minute? ");
            minute = Integer.parseInt(keyboard.readLine());
            System.out.println("What's the day of the month?");
            day = Integer.parseInt(keyboard.readLine());
            System.out.println("Which month of the year is it, use an integer");
            month = Integer.parseInt(keyboard.readLine());
            System.out.println("What year is it?");
            year = Integer.parseInt(keyboard.readLine());
            keyboard.close();
        } catch (IOException e) {
            System.out.println("Yo houston we have a problem");
        }

    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    public void setHour() {
        try {
            BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("What hour, use military notation?");
            this.hour = Integer.parseInt(keyboard.readLine());
            keyboard.close();
        } catch (NumberFormatException e) {
            System.out.println(e.toString() + ":doesnt seem to be a number");
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    }

    public int getHour() {
        return hour;
    }

    public void setMinute(int minute) {
        this.minute = minute;
    }

    public void setMinute() {
        try (BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in))) {
            System.out.println("What minute?");
            this.minute = Integer.parseInt(keyboard.readLine());
        } catch (NumberFormatException e) {
            System.out.println(e.toString() + ": doesnt seem to be a number");
        } catch (IOException e) {
            System.out.println(e.toString() + ": minute shall not cooperate");
        } catch (NullPointerException e) {
            System.out.println(e.toString() + ": in the setMinute function of the Date class");
        }
    }

    public int getMinute() {
        return minute;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public void setDay() {
        try {
            BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("What day 0-6?");
            this.day = Integer.parseInt(keyboard.readLine());
            keyboard.close();
        } catch (NumberFormatException e) {
            System.out.println(e.toString() + ":doesnt seem to be a number");
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    }

    public int getDay() {
        return day;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public void setMonth() {
        try {
            BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("What month 0-11?");
            this.month = Integer.parseInt(keyboard.readLine());
            keyboard.close();
        } catch (NumberFormatException e) {
            System.out.println(e.toString() + ":doesnt seem to be a number");
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    }

    public int getMonth() {
        return month;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public void setYear() {
        try {
            BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("What year?");
            this.year = Integer.parseInt(keyboard.readLine());
            keyboard.close();
        } catch (NumberFormatException e) {
            System.out.println(e.toString() + ":doesnt seem to be a number");
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    }

    public int getYear() {
        return year;
    }

    public void set() {
        setMinute();
        setHour();
        setDay();
        setMonth();
        setYear();
    }

    public Vector<Integer> get() {
        Vector<Integer> holder = new Vector<Integer>(5);
        holder.add(hour);
        holder.add(minute);
        holder.add(month);
        holder.add(day);
        holder.add(year);
        return holder;
    }
};
他们的父类(正确的名称是什么?)

最后是主类,这样我可以访问所有这些工作

    package GroceryReceiptProgram;


    public class NewMain {


        public static void main(String[] args) {
            FoodGroup test = new FoodGroup();
        }
    }
如果有人对此感兴趣,这里有一个UML链接


在使用字段之前,您需要实例化它们。

您的
FoodGroup
构造函数中有一个问题,您尚未初始化一些实例变量,因此它们采用
null
默认值,这会给您带来问题:

public FoodGroup() {
    //...
    //null pointer exception here!
    expirationDate.set();
    //null pointer exception here!
    purchaseDate.set();
    //null pointer exception here!
    location.set();
    //...
}
这个问题可以通过在使用变量之前创建这些变量来解决:

public FoodGroup() {
    //...
    expirationDate = new Date();
    //not null pointer exception!
    expirationDate.set();
    //similar to the other cases
    //...
}
对本项目和未来项目的一些建议:

  • 不要创建名称与JavaAPI中的类相似的类。例如,您的
    Date
    类很容易被误解为
    java.util.Date
  • 不要使用
    Vector
    类,而是使用
    List List=new ArrayList()
    。更多信息请点击这里:和(这是来自)
  • 尝试为类使用公共构造函数。在您的情况下,您的程序将编译并运行,因为您的所有类都在同一个包中,但如果它们在不同的包中,则会出现问题,因为几乎所有构造函数都具有
    默认的
    可见性,并且只能由相同包的类看到。请注意:
  • FoodGroup
    不是父类,它是1)
    FoodGroup
    Date
    ,2)
    FoodGroup
    Location
    之间的“has-a”关系。它可以被看作是控制两个类实例的行为和活动的控制器
  • 当您在此处或其他站点发布代码时,请发布一个而不是整个代码。这样,任何人都会更容易帮助你

您应该使用扫描仪,而不是使用BufferedReader。它具有检查hasNextInt()的方法。您还应该使用日历,而不是您自己的日期对象。
    public FoodGroup() {
        try {
            setPrice();
            setCount();
            expirationDate.set(); // expiration date is null here
public FoodGroup() {
    //...
    //null pointer exception here!
    expirationDate.set();
    //null pointer exception here!
    purchaseDate.set();
    //null pointer exception here!
    location.set();
    //...
}
public FoodGroup() {
    //...
    expirationDate = new Date();
    //not null pointer exception!
    expirationDate.set();
    //similar to the other cases
    //...
}