微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用组合时,如何使用 JAVA 中的 Set 方法初始化值?

如何解决使用组合时,如何使用 JAVA 中的 Set 方法初始化值?

我正在解决 OOP (JAVA) 练习任务。在学习作曲这个主题时,我遇到了一个让我非常困难的练习任务。请检查我的代码并提出解决方案。我会很感激:) 实践问题是:

对于下面描述的每个属性,选择适当的数据类型。每个类的所有属性都应该是私有的,并通过 get/set 方法公开。还定义了至少一个构造函数,允许初始化对象的 2-3 个属性用 courseCode 和 courseTitle 实例变量定义一个类 Course。 使用 countryCode、cityCode 和 lineNumber 属性定义 PhoneNumber 类。 定义一个带有 streetAddress、城镇、城市、国家和电话号码属性的地址类。电话号码应为电话号码类型。 使用姓名、电子邮件、CNIC、course1、course2 和地址属性定义一个类 Student。其中 course1 和 course2 应该是 Course 类型,address 应该是 Address 类型。在 Student 类中定义一个构造函数,该构造函数只接受 CNIC、名称和地址。 创建一个 StudentTest 类。在其主要方法中,创建一个名为 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

解决方法

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

你应该打电话

KlaviyoApiError
,

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。