java - 空指针异常和带有继承和子类的突然 IOStream 闭包

标签 java io nullpointerexception class-hierarchy

之前的简要背景,以便我们可以在相同的波长上进行通信。我已经上过大约 8-10 门关于编程的大学类(class),从数据结构到所有语言,再到特定语言(例如 java 和 c++)。我有点生疏了,因为我通常会休息 2-3 个月不写代码。这是我两年前开始考虑的一个个人项目。

好吧,具体到细节,还有一个具体问题,我的更改器(mutator)函数遇到了问题。看来我试图错误地访问私有(private)变量。问题是,我是否嵌套了太多的类并试图以错误的方式改 rebase 类变量。如果是这样,请向我指出正确的文献,或者确认这是我的问题,以便我可以重新研究这些信息。谢谢

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;
    }
};

这显然是 Date 类,接下来是另一个基类 Location。

package GroceryReceiptProgram;

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

    public class Location {

        String streetName, state, city, country;
        int zipCode, address;

        Location() {
            try {
                BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("What is the street name");
                streetName = keyboard.readLine();
                System.out.println("Which state?");
                state = keyboard.readLine();
                System.out.println("Which city?");
                city = keyboard.readLine();
                System.out.println("Which country?");
                country = keyboard.readLine();
                System.out.println("Which zipcode?");//if not u.s. continue around this step
                zipCode = Integer.parseInt(keyboard.readLine());
                System.out.println("What address?");
                address = Integer.parseInt(keyboard.readLine());
            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }

        public void setZipCode(int zipCode) {
            this.zipCode = zipCode;
        }

        public void setZipCode() {
            try {
                BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("What zipCode?");
                this.zipCode = 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 void set() {
            setAddress();
            setCity();
            setCountry();
            setState();
            setStreetName();
            setZipCode();
        }

        public int getZipCode() {
            return zipCode;
        }

        public void setAddress(int address) {
            this.address = address;
        }

        public void setAddress() {
            try {
                BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("What minute?");
                this.address = 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 getAddress() {
            return address;
        }

        public void setStreetName(String streetName) {
            this.streetName = streetName;
        }

        public void setStreetName() {
            try {
                BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("What minute?");
                this.streetName = keyboard.readLine();
                keyboard.close();
            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }

        public String getStreetName() {
            return streetName;
        }

        public void setState(String state) {
            this.state = state;
        }

        public void setState() {
            try {
                BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("What minute?");
                this.state = keyboard.readLine();
                keyboard.close();
            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }

        public String getState() {
            return state;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public void setCity() {
            try {
                BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("What minute?");
                this.city = keyboard.readLine();
                keyboard.close();
            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }

        public String getCity() {
            return city;
        }

        public void setCountry(String country) {
            this.country = country;
        }

        public void setCountry() {
            try {
                BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("What minute?");
                this.country = keyboard.readLine();
                keyboard.close();
            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }

        public String getCountry() {
            return country;
        }
    };

他们的父类(正确的名称是什么?)类

package GroceryReceiptProgram;

    import java.io.*;

    public class FoodGroup {

        private int price, count;
        private Date purchaseDate, expirationDate;
        private Location location;
        private String name;

        public FoodGroup() {
            try {
                setPrice();
                setCount();
                expirationDate.set();
                purchaseDate.set();
                location.set();
            } catch (NullPointerException e) {
                System.out.println(e.toString() + ": in the constructor of the FoodGroup class");
            }
        }

        public void setPrice(int price) {
            this.price = price;
        }

        public void setPrice() {
            try (BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in))) {
                System.out.println("What Price?");
                price = 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() + ": in the FoodGroup class, setPrice function");
            } catch (NullPointerException e) {
                System.out.println(e.toString() + ": in FoodGroup class. SetPrice()");
            }
        }

        public int getPrice() {
            return price;
        }

        public void setCount(int count) {
            this.count = count;
        }

        public void setCount() {
            try (BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in))) {
                System.out.println("What count?");
                count = 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() + ": in the FoodGroup class, setCount()");
            } catch (NullPointerException e) {
                System.out.println(e.toString() + ": in FoodGroup class, setCount");
            }
        }

        public int getCount() {
            return count;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setName() {
            try {
                BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("What minute?");
                this.name = keyboard.readLine();

            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }

        public String getName() {
            return name;
        }

        public void setLocation(Location location) {
            this.location = location;
        }

        public Location getLocation() {
            return location;
        }

        public void setPurchaseDate(Date purchaseDate) {
            this.purchaseDate = purchaseDate;
        }

        public void setPurchaseDate() {
            this.purchaseDate.set();
        }

        public Date getPurchaseDate() {
            return purchaseDate;
        }

        public void setExpirationDate(Date expirationDate) {
            this.expirationDate = expirationDate;
        }

        public void setExpirationDate() {
            this.expirationDate.set();
        }

        public Date getExpirationDate() {
            return expirationDate;
        }
    }

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

    package GroceryReceiptProgram;


    public class NewMain {


        public static void main(String[] args) {
            FoodGroup test = new FoodGroup();
        }
    }

如果有人进一步感兴趣,这里有一个 UML 链接。

https://www.dropbox.com/s/1weigjnxih70tbv/GRP.dia

最佳答案

您的 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
    //...
}

对此和 future 项目的一些建议:

  • 不要创建与 Java API 中的类名称相似的类。例如,您的Date类很容易被误解 java.util.Date .
  • 请勿使用 Vector类,而是使用 List<YourClass> list = new ArrayList<YourClass>() 。更多信息请点击:What are the differences between ArrayList and Vector?Why is Java Vector class considered obsolete or deprecated? (此来自Jon Skeet)。
  • 尝试为您的类使用公共(public)构造函数。在您的情况下,您的程序将编译并运行只是因为所有类都在同一个包中,但如果它们位于不同的包中,则会出现问题,因为几乎所有构造函数都有 default可见性,只能被同一包的类看到。请注意:Controlling Access to Members of a Class
  • FoodGroup不是父类,而是 1) FoodGroup 之间的“has-a”关系和Date , 2) FoodGroupLocation 。它可以被视为一个 Controller ,控制两个类实例的行为和 Activity 。
  • 当您在此处或其他网站发布代码时,发布 SSCCE而不是你的整个代码。这样,任何人都会更容易帮助您。

关于java - 空指针异常和带有继承和子类的突然 IOStream 闭包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12667140/

相关文章:

java Main < inputfile 不起作用(Main 是一个用于测试 ANTLR 语法的 java 类)

PYTHON:交替读取 2 个文件中的行并 append 到第三个文件

java - 线程异常 "AWT-EventQueue-0"错误

java - 尝试从异步类 android 中的 url 解析 JSON 数据时出现空指针异常

java - 你如何简单地替换 htmlspecialchars ?

java - 为什么要用移位运算符声明变量?

c - 流缓冲区是如何在 C 中实现的?

java - AWS Lambda 调用异常 "No LambdaFunction annotation for method invoke"

java - 从 Windows 运行时出现 Phoenix CsvBulkLoadTool 错误

java - 为什么在我的代码中无法为 null 对象赋值?我的代码有一个简单的二叉搜索树插入函数 :