【问题标题】:How to initialize values using Set Methods in JAVA, when using Composition?使用组合时如何在 JAVA 中使用 Set 方法初始化值?
【发布时间】:2021-08-15 05:07:33
【问题描述】:

我正在解决 OOP (JAVA) 练习任务。在学习作文主题时,我遇到了一个练习任务,这让我非常难受。请检查我的代码并提出解决方案。我将不胜感激:) 练习题是:

对于下面描述的每个属性,选择适当的数据类型。每个类的所有属性都应该是私有的,并通过 get/set 方法公开。还要定义至少一个允许初始化对象的 2-3 个属性的构造函数。 用 courseCode 和 courseTitle 实例变量定义一个类 Course。 使用 countryCode、cityCode 和 lineNumber 属性定义 PhoneNumber 类。 使用 streetAddress、town、city、country 和 phoneNumber 属性定义类 Address。 phoneNumber 应为 PhoneNumber 类型。 定义具有姓名、电子邮件、CNIC、课程1、课程2 和地址属性的学生类。其中 course1 和 course2 应该是 Course 类型,并且 address 应该是 Address 类型。在 Student 类中定义一个构造函数,该构造函数只接受 CNIC、名称和地址。 创建一个 StudentTest 类。在其 main 方法中,创建一个名为 student1 的 Student 对象。完全初始化其所有属性。 CNIC、名称和地址应使用构造函数进行初始化。其他属性应使用 setter 方法进行初始化。所有属性的值应由用户给出。对象完全初始化后,使用 get 方法打印所有属性值。 制作另一个对象student2,假设用户也和student1住在同一个地址。重用student1的地址对象初始化student2地址。您不需要从 student2 对象的用户输入中获取属性。打印所有实例变量的值。

我的代码截图:

StudentTest Class

Address Class

PhoneNumber Class

Course Class

Student Class

我得到的错误:

ERROR

我的代码副本:

(学生测试班)

public class StudentTest {
    public static void main(String[] args) {

        PhoneNumber phoneNumber = new PhoneNumber(92, 042, 354);

        Address address = new Address("Street No.502", "HT", "Lahore", "Pakistan", phoneNumber);

        Student student1 = new Student("Muaz", 34603, address);

        student1.setEmail("testEmail@java.com");
        System.out.println(student1.getEmail());


        student1.getCourse1().setCourseCode(101);
        student1.getCourse1().setCourseTitle("OOP");

        student1.getCourse2().setCourseCode(102);
        student1.getCourse2().setCourseTitle("EMO");

        System.out.println(student1.getCourse1().getCourseCode());
        System.out.println(student1.getCourse1().getCourseTitle());
        System.out.println(student1.getCourse2().getCourseCode());
        System.out.println(student1.getCourse2().getCourseTitle());



    }
}

(学生班)

public class Student {
    //Variables
    private String name;
    private int cnic;
    private Address address;
    private String email;
    private Course course1;
    private Course course2;

    //Constructor
    public Student(String name, int cnic, Address address){ setName(name); setCnic(cnic); setAddress(address); }

    //Set and Get Methods
    public void setName(String name){ this.name = name; }
    public String getName(){ return name; }
    public void setCnic(int cnic){ this.cnic = cnic; }
    public int getCnic(){ return cnic; }
    public void setAddress(Address address){ this.address = address; }
    public Address getAddress(){ return address; }
    public void setEmail(String email){ this.email = email; }
    public String getEmail(){ return email; }
    public void setCourse1(Course course1){ this.course1 = course1; }
    public Course getCourse1(){ return course1; }
    public void setCourse2(Course course2){ this.course2 = course2; }
    public Course getCourse2(){ return course2; }
}

(地址类)

public class Address {

    private String streetAddress;
    private String town;
    private String city;
    private String country;
    private PhoneNumber phoneNumber;

    public Address(String streetAddress, String town, String city, String country, PhoneNumber phoneNumber){
        this.streetAddress = streetAddress;
        this.town = town;
        this.city = city;
        this.country = country;
        this.phoneNumber = phoneNumber;
    }
}

(电话号码类)

public class PhoneNumber {

    private int countryCode;
    private int cityCode;
    private int lineNumber;

    public PhoneNumber(int countryCode, int cityCode, int lineNumber){
        this.countryCode = countryCode;
        this.cityCode = cityCode;
        this.lineNumber = lineNumber;
    }
}

(课程班)

public class Course {

    private int courseCode;
    private String courseTitle;

    public void setCourseCode(int courseCode){
        this.courseCode = courseCode;
    }
    public int getCourseCode(){
        return courseCode;
    }
    public void setCourseTitle(String courseTitle){
        this.courseTitle = courseTitle;
    }
    public String getCourseTitle(){
        return courseTitle;
    }
}

错误:

Exception in thread "main" java.lang.NullPointerException
    at StudentTest.main(StudentTest.java:14)

Process finished with exit code 1

【问题讨论】:

  • 嗨,欢迎来到 StackOverflow(SO)。请注意,将代码和错误粘贴到问题中,而不是在问题中添加屏幕截图。这样,来自 SO 的任何人都可以自己检查。
  • 请将您的代码添加到您的问题中,而不是发布屏幕截图
  • 您需要将 course1 和 course2 初始化为非空值。

标签: java oop getter-setter composition


【解决方案1】:

您似乎还没有为 Course1 设置值。所以你试图检索一个不存在的值。

你应该打电话

Course course1 = new Course();
student1.setCourse1(course1);

【讨论】:

  • 感谢您的帮助 :) 但是您能解释一下这背后的逻辑吗?根据我的理解 course1 和 course2 是 Course 类型变量,它们都引用 courseCode 和 courseTitle。那么我们不应该直接初始化 courseCode 和 courseTitle 吗?
  • courseCode 和 courseTitle 属于 Course 类。如果不先初始化封闭类,就无法初始化类的变量。
【解决方案2】:

我可以从您的屏幕截图中收集到的信息,请执行以下操作以删除 NullPointerException 错误:

Student.java 类中将变量course1 初始化为private Course course1 = new Course()。对course2 执行相同操作。

【讨论】:

  • 感谢您的帮助 :) 但是您能解释一下这背后的逻辑吗?根据我的理解 course1 和 course2 是 Course 类型变量,它们都引用 courseCode 和 courseTitle。那么我们不应该直接初始化 courseCode 和 courseTitle 吗?
  • 您应该初始化每个类中的每个变量以防止错误。这个链接会告诉你初始化变量的重要性:softwareengineering.stackexchange.com/questions/223862/…
猜你喜欢
  • 2013-05-03
  • 1970-01-01
  • 2020-07-19
  • 2018-11-26
  • 1970-01-01
  • 2013-06-26
  • 1970-01-01
  • 2015-07-23
  • 2017-03-21
相关资源
最近更新 更多