Message from Paragram Slide - AZ in MacGuyver - Skills & Academics #homework-help


2018-09-28 20:24:32 UTC  

```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++;
}
```

2018-09-28 20:24:37 UTC  

maybe this method is messed up

2018-09-28 20:25:25 UTC  

no wait that one should be okay

2018-09-28 20:25:46 UTC  

```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;
}
```

2018-09-28 20:25:55 UTC  

this one might be the one with the problem

2018-09-28 20:26:56 UTC  

why --?

2018-09-28 20:27:04 UTC  

ya lol I just noticed that

2018-09-28 20:27:07 UTC  

that's definitely a mistake

2018-09-28 20:27:19 UTC  

doesn't fix the null pointer exception though

2018-09-28 20:27:35 UTC  

is there a stack trace?

2018-09-28 20:28:57 UTC  

this?

2018-09-28 20:29:02 UTC  

```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)

```

2018-09-28 20:32:32 UTC  

this is what I have on the contains method now

2018-09-28 20:32:38 UTC  

```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;
}
```

2018-09-28 20:33:27 UTC  

@ThisIsChris Do you know how to fix this stuff?

2018-09-28 20:33:35 UTC  

or know who does?

2018-09-28 20:34:25 UTC  

where do you get the null?

2018-09-28 20:34:44 UTC  

what line

2018-09-28 20:35:03 UTC  

the same line as the if statement

2018-09-28 20:35:18 UTC  

should I paste the entire files in here or would that be too big?

2018-09-28 20:35:57 UTC  

for (int i = 0; i < index && cur != null; ++i)

2018-09-28 20:37:15 UTC  

zip them and email to [email protected]

2018-09-28 20:38:06 UTC  

It's too late for that

2018-09-28 20:38:12 UTC  

since it has to be turned in soon

2018-09-28 20:38:14 UTC  

but thanks

2018-09-28 20:38:22 UTC  

I'll just see what I can get fixed now

2018-09-28 20:38:33 UTC  

try changes to that for loop

2018-09-28 20:39:15 UTC  

okay thanks

2018-09-28 20:39:34 UTC  

what would cause the for look to cause a null pointer exception?

2018-09-28 20:40:01 UTC  

I suspect cur is null

2018-09-28 20:40:08 UTC  

and the recursion

2018-09-28 20:40:11 UTC  

```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;
}
```

2018-09-28 20:47:54 UTC  

```return get(i);```

2018-09-28 20:48:01 UTC  

the recursion

2018-09-28 20:48:04 UTC  

oh ya

2018-09-28 20:48:07 UTC  

and what is head

2018-09-28 20:48:09 UTC  

that's definitely messing something up

2018-09-28 20:48:15 UTC  

head is defined earlier

2018-09-28 20:48:27 UTC  

member var?

2018-09-28 20:48:36 UTC  

```java
public MyLinkedList() {
this.head = new ListNode(null); //with a dummy head node
this.size = 0;
}
```

2018-09-28 20:48:49 UTC  

okay that exception is fixed I think