Message from @Paragram Slide - AZ
Discord ID: 495332861965238272
```java
// Inserts the specified element at the specified position in this list.
// Shifts the element currently at that position (if any) and any subsequent
// elements to the right (adds one to their indices).
// if(index < 0 or index > this.size), throws IndexOutOfBoundsException.
// E.g, if this list is [dummy]->["A"]->["B"]->["C"] with size = 3.
// add(0,D) will result in [dummy]->["D"]->["A"]->["B"]->["C"].
// Continuing on the previous add() call, add(1,"E") will
// change the existing list to [dummy]->["D"]->["E"]->["A"]->["B"]->["C"].
public void add(int index, Object o) {
ListNode newNode = new ListNode(o);
newNode.next = get(index);
get(index - 1).next = newNode;
this.size++;
}
```
maybe this method is messed up
no wait that one should be okay
```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;
}
```
this one might be the one with the problem
why --?
ya lol I just noticed that
that's definitely a mistake
doesn't fix the null pointer exception though
is there a stack trace?
this?
```java
Exception in thread "main" java.lang.NullPointerException
at MyLinkedList$ListNode.access$3(MyLinkedList.java:11)
at MyLinkedList.contains(MyLinkedList.java:59)
at MyLinkedListTester.testContains(MyLinkedListTester.java:119)
at MyLinkedListTester.main(MyLinkedListTester.java:216)
```
this is what I have on the contains method now
```java
// Returns true if this list contains the specified element o.
// More formally, returns true if and only if this list contains at least one element e
// such that (o==null ? e==null : o.equals(e)).
// Note: you have to handle the case where a list node stores null data element.
public boolean contains(Object o) {
for (int i = 0; i < size; i++) {
if (o == null && get(i).data == null) {
return true;
}
else if (Objects.equals(get(i).data, o)) {
return true;
}
}
return false;
}
```
@ThisIsChris Do you know how to fix this stuff?
or know who does?
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?
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
that's definitely messing something up
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