I'm unsure whether or not this has been answered before as I didn't see it under Similar Questions but here goes... For a school project, we have to create a doubly-linked list of Assignment
s and when I made my addAssignment(Assignment a)
function, I got a NullPointerException.
In our project, Assignment
s consist of a String
for the name of the assignment, int
s for the number of points earned and the number of points given, a double
for the percentage grade and a char
for the letter grade given. Node
s contain an Assignment
called "assignment" and "next" and "prev" Node
references. A DList
(our doubly-linked list) consists of a "head" and "tail" Node
at the start.
The code in question is included below. I've included comments detailing where my console gave me error messages.
In class Assignment
:
/**
* setter
* @param n = the name of the assignment
*/
public void setName(String n) {
name = n;
}
In class Node
:
/**
* default constructor
*/
public Node() {
assignment.setName("");
assignment.setEarnedPts(0);
assignment.setTotalPts(0);
next = prev = null;
}
/**
* n-argument constructor
* @param a = assignment
* @param e = # of earned points
* @param t = # of total possible points
* @param n = "next" node
* @param p = "previous" node
*/
public Node(String name, int e, int t, Node n, Node p) {
assignment.setName(name); // got an error here
assignment.setEarnedPts(e);
assignment.setTotalPts(t);
assignment.setGrade();
next = n;
prev = p;
}
Finally, in class DList
:
/**
* adds an assignment to the list
* @param a = the assignment
*/
public void addAssignment(Assignment a) {
// check to see if this is the first node in the list
Node temp = new Node(a.getName(), a.getEarnedPts(), a.getTotalPts(), tail, head); // got an error here
if (head == tail) {
head = temp;
}
else { // there are other node(s) in the list
Node p = head;
Node q = new Node();
while (p.getNext() != null) {
q = p; // set q equal to p, so that q will equal previous node
p = p.getNext(); // advance p by one node
}
p.setNext(temp);
p.setPrev(q); // ??? ask about this in class later
}
}
Unfortunately we don't have the entire code, but I suspect, that in your class Node you have a field like this:
class Node{
Assignment assignment;
If that's the case this is your problem: You did not initialize any value to the assignment variable and it is therefore Null. Java does not default initialize like, for example, C++ does. You will have to do it yourself, the constructors for the Node class then look like this:
public Node() {
assignment = new Assignment();
assignment.setName("");
assignment.setEarnedPts(0);
assignment.setTotalPts(0);
next = prev = null;
}
public Node(String name, int e, int t, Node n, Node p) {
assignment = new Assignment();
assignment.setName(name); // got an error here
assignment.setEarnedPts(e);
assignment.setTotalPts(t);
assignment.setGrade();
next = n;
prev = p;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.