简体   繁体   English

无法让我的方法工作以访问其本机类中的另一个方法

[英]Can't get my method to work to be able to access another method in it's native class

The purpose of my assignment is to generate a report based on data read in from a file.我的任务的目的是根据从文件中读取的数据生成报告。 The file is properly read in, and the values are calculated correctly.文件已正确读入,并且值已正确计算。 However, one of my linked lists, StudentList, throws a NullPointException that's marked at the getCourse() at other times, and I'm not sure of how to actually solve it.但是,我的一个链表 StudentList 在其他时候抛出了一个在 getCourse() 中标记的 NullPointException,我不确定如何实际解决它。 Below will be the code for the StudentNode class getCourse() comes from and the specific linked list.下面将是 StudentNode 类 getCourse() 的代码和具体的链表。 The class StudentNode is originally in a separate file from the insert method.类 StudentNode 最初与 insert 方法位于一个单独的文件中。 Ultimately, I'm not understanding what I should do with my getCourse() so that the NPE is not thrown, and so I can still use that method to access the insert method to add a node.最终,我不明白我应该用我的 getCourse() 做什么,以便不抛出 NPE,所以我仍然可以使用该方法访问插入方法以添加节点。

public class StudentNode {
    private String lastName; //holds lastName
    private String firstName; //holds firstName
    private String id; //holds identification number
    private CourseList course; //allows access to traverse method for printing all classes of a student
    private StudentNode next; //self referential variable
public StudentNode(){
    //Precondition: must be called via object creation
    //Postcondition: sets values of lastName, firstName, id  and course to empty strings, and gpa to 0.0
    new StudentNode("", "", "");
}

public StudentNode(String lastName, String firstName, String id){
    //Precondition: must be called via object creation with correct parameters
    //Postcondition: sets values of lastName, firstName, id, course and gpa to values passed in parameter
    this.setLastName(lastName);
    this.setFirstName(firstName);
    this.setId(id);
}

public void setLastName(String lastName) {
    //Precondition: passed variable is a String
    //Postcondition: this.lastName is set to value of lastName
    this.lastName = lastName;
}//end setLastName

public String getName() {
    //Precondition: none
    //Postcondition: returns firstName
    return this.lastName + " " + this.firstName;
}

public void setFirstName(String firstName) {
    //Precondition: passed variable is a String
    //Postcondition: sets this.firstName to value of firstName
    this.firstName = firstName;
}

public String getId() {
    //Precondition: none
    //Postcondition: returns id
    return this.id;
}

public void setId(String id) {
    //Precondition: passed variable is a String
    //Postcondition: this.id is set to value of id
    this.id = id;
}

public void setNext(StudentNode node){
    this.next = node;
}

public StudentNode getNext(){
    return this.next;
}

public String getLastName() {
    return this.lastName;
}

public String getFirstName() {
    return this.firstName;
}

public CourseList getCourse() {
    return this.course;
}

public void setCourse(CourseList course) {
    this.course = course;
}

public void printStudent(){
    System.out.printf("%20s%10s\n", getName(), this.getId());
    course.traverse();
}

} }

public void insert(StudentNode node, CourseNode newCourse){
    if (head == null){
        head = node;
    }
    else {
        boolean inserted = false;
        StudentNode curr = head;
        StudentNode prev = null;

        while (curr.getNext() != null && !inserted) {
            int compareLast = node.getLastName().compareTo(curr.getLastName());
            //returns
            //< 0  - node goes before curr
            //== 0 - check first names
            //> 0 - node goes after curr
            if (compareLast < 0) { //if the first line is lexigraphically less than (comes before)
                if (curr == head) {
                    head = node;
                } else {
                    prev.setNext(node);
                }
                node.getCourse().insert(newCourse);
                node.setNext(curr);
                inserted = true;
            } else if (compareLast  == 0) { //last names are the same, check first names
                int compareFirst = node.getFirstName().compareTo(curr.getFirstName());
                if (compareFirst < 0) {
                    if (curr == head) {
                        head = node;
                    } else {
                        prev.setNext(node);
                    }
                    node.getCourse().insert(newCourse);
                    node.setNext(curr);
                    inserted = true;
                } else if (compareFirst == 0) { //same student
                    //System.out.println("Error, they exist already.");
                    if (curr == head) {
                        head = node;
                    } else {
                        prev.setNext(node);
                    }
                    curr.getCourse().insert(newCourse); //insert into list for current node
                    inserted = true;
                } else {
                    prev = curr; //advance previous to current
                    curr = curr.getNext(); //advance current to next field
                }
            } else { //the first line needs to be placed somewhere after the item compared to, continue
                prev = curr;
                curr = curr.getNext();
            }//end else
        }//end while

        //Checks very last node
        if (!inserted) {
            int compareLast = node.getLastName().compareTo(curr.getLastName());
            //returns
            //< 0  - node goes before curr
            //== 0 - check first names
            //> 0 - node goes after curr
            if (compareLast < 0) {
                if (curr == head) {
                    head = node;
                } else {
                    curr.setNext(node);
                }
                node.getCourse().insert(newCourse);
                node.setNext(curr);
            } else if (compareLast == 0) { //if last names are the same
                int compareFirst = node.getFirstName().compareTo(curr.getFirstName());
                if (compareFirst < 0) { //first name is lexigraphically less than second name
                    node.getCourse().insert(newCourse);
                    node.setNext(curr);
                } else if (compareFirst == 0) {
                    System.out.println("Error, same person.");
                } else { //the desired node goes after the current last node (on basis of first name)
                    curr.setNext(node);
                }
            } else { //the desired node goes after the current last node (on basis of last name)
                curr.setNext(node);
            }
        }//end if
    }//end else
}//end insert

You've not initialized node properly.您没有正确初始化节点。

The problem is here:问题在这里:

 node.getCourse().insert(newCourse);

Your insert() method, from your code seems to be of CourseList class.您的代码中的 insert() 方法似乎属于CourseList类。

insert() takes 2 explicit parameters as per your definition. insert() 根据您的定义采用 2 个显式参数。

You have passed only newCourse.您仅通过了新课程。 Hence the node object is null for that method.因此,该方法的节点对象为空。

You need to pass 2 arguments to insert().您需要将 2 个参数传递给 insert()。

Hope this helps you.希望这对你有帮助。

It will help you if you can trace back where the exception originally started by analyzing the error you get.如果您可以通过分析您得到的错误来追溯异常最初开始的位置,它将对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM