Message from @Jacob
Discord ID: 495335933684744213
where do you get the null?
what line
the same line as the if statement
should I paste the entire files in here or would that be too big?
for (int i = 0; i < index && cur != null; ++i)
zip them and email to [email protected]
It's too late for that
since it has to be turned in soon
but thanks
I'll just see what I can get fixed now
try changes to that for loop
okay thanks
what would cause the for look to cause a null pointer exception?
I suspect cur is null
and the recursion
```public ListNode get(int index) throws IndexOutOfBoundsException{
ListNode cur = head;
for (int i = 0; i < index && cur != null; i++) {
if (i == index) {
return get(i);
}
cur = cur.next;
}
return null;
}
```
```return get(i);```
the recursion
oh ya
and what is head
head is defined earlier
member var?
```java
public MyLinkedList() {
this.head = new ListNode(null); //with a dummy head node
this.size = 0;
}
```
okay that exception is fixed I think
maybe let's see if we can do anything with one more thing
is this linked list or tree node?
linked list
huh I get a null pointer exception from somewhere else, too
```java
------------------testAddLast()----
Exception in thread "main" java.lang.NullPointerException
at MyLinkedList$ListNode.access$2(MyLinkedList.java:11)
at MyLinkedList.add(MyLinkedList.java:205)
at MyLinkedListTester.testAddLast(MyLinkedListTester.java:42)
at MyLinkedListTester.main(MyLinkedListTester.java:214)
```
```java
public static void testAddLast() { //passed
System.out.println("------------------testAddLast()----");
init();
list3.add("A");
System.out.println(list3);
list3.add("B");
System.out.println(list3);
list3.add(null);
System.out.println(list3);
list3.add("C");
System.out.println(list3);
drawLine();
}
```
```java
//Add the object e to the end of this list.
// it returns true, after e is successfully added.
public boolean add(Object e) {
ListNode newNode = new ListNode(e);
newNode.next = null;
get(this.size - 1).next = newNode;
this.size++;
return true;
}
```
any glaring problems here?
that get() could return null
hmmm
any ideas on how to fix that?
after init()
how many nodes in list3
println after init()
4
what is size