【问题标题】:Can't get my method to work to be able to access another method in it's native class无法让我的方法工作以访问其本机类中的另一个方法
【发布时间】:2016-10-13 14:19:39
【问题描述】:

我的任务的目的是根据从文件中读取的数据生成报告。该文件被正确读入,并且值被正确计算。但是,我的一个链表 StudentList 会在其他时间抛出一个在 getCourse() 标记的 NullPointException,我不确定如何实际解决它。下面将是 StudentNode 类 getCourse() 的代码和具体的链表。 StudentNode 类最初位于与 insert 方法不同的文件中。最终,我不明白我应该如何处理我的 getCourse() 以便不抛出 NPE,因此我仍然可以使用该方法访问 insert 方法以添加节点。

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

【问题讨论】:

标签: java data-structures nullpointerexception linked-list


【解决方案1】:

你没有正确初始化节点。

问题出在这里:

 node.getCourse().insert(newCourse);

您的代码中的 insert() 方法似乎属于 CourseList 类。

insert() 根据您的定义采用 2 个显式参数。

你只通过了新课程。因此,该方法的节点对象为空。

您需要将 2 个参数传递给 insert()。

希望对你有所帮助。

如果您可以通过分析您得到的错误来追溯异常最初开始的位置,这将对您有所帮助。

【讨论】:

    猜你喜欢
    • 2014-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多