Node class is an internal class in my code? - java

I'm asked this question and I'm not really sure what it means,
In our LinkedList class, the Node class is an internal class. Briefly
describe two things that would have to be coded differently if the
Node class was a separate class from the LinkedList class.
Here's the code for my LinkedList class:
public class LinkedList<T> implements LinkedListInterface<T> {
private Node head;
private Node tail;
private int count;
public LinkedList() {
head = null;
tail = null;
count = 0;
}
class Node {
T data;
Node next;
Node(T data) {
this.data = data;
next = null;
}
}
public Node getHead() {
return head;
}
public int add(T item) {
Node newNode = new Node(item);
if (isEmpty()) {
head = newNode;
} else {
tail.next = newNode;
}
tail = newNode;
count++;
return count;
}
public void add(T item, int pos) throws ListException {
if (pos < 1 || pos > count + 1) {
throw new ListException("Invalid position to insert at");
}
Node newNode = new Node(item);
if (count == 0) {
add(item);
} else if (pos == count + 1) {
add(item);
} else if (pos == 1) {
newNode.next = head;
head = newNode;
count++;
} else {
Node prev = jump(pos - 2);
newNode.next = prev.next;
prev.next = newNode;
count++;
}
}
private Node jump(int numJumps) {
Node nd = head;
for (int i = 0; i < numJumps; i++) {
nd = nd.next;
}
return nd;
}
public LinkedList<T> combine(LinkedList<T> obj2) {
Node list1 = this.getHead();
Node list2 = obj2.getHead();
if (list2 == null) {
return this;
}
if(list1==null) {
return obj2;
}
Node temp=list1;
while(temp.next!=null) {
temp = temp.next;
}
temp.next=list2;
return this;
}
public int contains(T item) {
Node nd = this.head;
for (int pos = 1; pos <= count; pos++) {
if (nd.data.equals(item)) {
return pos;
}
nd = nd.next;
}
return 0;
}
public LinkedList<T> copy() {
LinkedList<T> l = new LinkedList<T>();
try {
for (int pos = 1; pos <= count; pos++)
l.add(retrieve(pos));
}
catch(ListException e) {
System.out.println("Should not occur (Copy)");
}
return l;
}
public boolean equals(Object list) {
if (list == null) {
return false;
}
LinkedList myLinkedList = (LinkedList)list;
if(myLinkedList.getClass() != this.getClass()) {
return false;
}
if(myLinkedList.length() != this.length()) {
return false;
}
try{
for (int pos = 1; pos <= count; pos++) {
if(!myLinkedList.retrieve(pos).equals(this.retrieve(pos))) {
return false;
}
}
}
catch (ListException e) {
System.out.println("Should not occur");
}
return true;
}
public boolean isEmpty() {
return length() == 0;
}
public int length() {
return count;
}
#SuppressWarnings("unchecked")
public T remove(int pos) throws ListException {
if (pos < 1 || pos > count) {
throw new ListException("Invalid position to remove from");
}
Node removedItem = null;
if (count == 1) {
removedItem = head;
head = null;
tail = null;
} else if (pos == 1) {
removedItem = head;
head = head.next;
} else if (pos == count) {
removedItem = tail;
Node prev = jump(pos - 2);
prev.next = null;
tail = prev;
} else {
Node prev = jump(pos - 2);
removedItem = prev.next;
prev.next = prev.next.next;
}
count--;
return removedItem.data;
}
public int remove(T item) {
int numRemoved = 0;
int pos = -1;
try {
while ((pos = contains(item)) > 0) {
remove(pos);
numRemoved++;
}
} catch (ListException e){
System.out.print(e);
}
return numRemoved;
}
int find_length(Node list){
int count = 0;
while (list!=null) {
count++;
list=list.next;
}
return count;
}
public void replace(T item, int pos) throws ListException {
if (pos < 1 || pos > count + 1) {
throw new ListException("Invalid position to replace at");
}
Node list = this.getHead();
int length = find_length(list);
if(pos<1||pos>length){
return;
}
while(list!=null){
pos--;
if(pos==0){
list.data=item;
return;
}
list=list.next;
}
}
public T retrieve(int pos) throws ListException {
T item = null;
if (pos < 1 || pos > count) {
throw new ListException("Invalid position to retrieve from");
}
if (pos == 1) {
return head.data;
}
Node prev = jump(pos - 2);
item = prev.next.data;
return item;
}
public LinkedList<T> reverse() {
LinkedList<T> l = new LinkedList<T>();
try {
for (int pos = count; pos > 0; pos--)
l.add(retrieve(pos));
}
catch (ListException e) {
System.out.println("Should not occur (Reverse)");
}
return l;
}
public String toString() {
String temp = "";
Node nd = head;
while (nd != null) {
temp += nd.data + "-";
nd = nd.next;
}
return temp;
}
}
What I'm trying to get is the since the Node class is located in the same class as my LinkedList class, it is referred to as an internal class. If it were to be separate, would I have to implement a Node class similarly to the LinkedListInterface<T> class? What else would have to be coded differently?

Related

Generic type error: argument lists differ in length

I'm stuck with a linked list implementation where a Node class and a list interface using generic types are required. The LinkedList class should implement the list interface and consist of Node objects. However, I have the following error: Executing command: javac -d . LinkedList.java
LinkedList.java:31: error: constructor Node in class Node cannot be applied to given types;
Node temp = new Node(data,null);
^
required: Object
found: T#1,
reason: actual and formal argument lists differ in length
where T#1,T#2 are type-variables:
T#1 extends List declared in class LinkedList
T#2 extends Object declared in class Node
LinkedList.java:41: error: constructor Node in class Node cannot be applied to given types;
I also cannot figure out why the compiler does not like: public class LinkedList<T extends Node<T> & List<T>>?
public class Node<T> {
private T data;
private Node<T> next;
private Node(T data, Node<T> next) {
this.data = data;
this.next = next;
}
private Node(T data) {
this(data,null);
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node<T> getNext() {
return next;
}
public void setNext(Node<T> next) {
this.next = next;
}
}
The List interface:
public interface List<T> {
void addAtIndex(T data, int index);
T getAtIndex(int index);
T removeAtIndex(int index);
T remove(T data);
void clear();
boolean isEmpty();
int size();
}
LinkedList class:
import java.util.NoSuchElementException;
public class LinkedList<T extends List<T>> {
private Node<T> head;
private Node<T> tail;
private int size;
LinkedList() {
this.head = null;
this.tail = null;
this.size = 0;
}
public Node<T> getHead() {
return head;
}
public Node<T> getTail() {
return tail;
}
public void addAtIndex(T data, int index) {
if ((index < 0) || (index >= size)) {
throw new IllegalArgumentException("Your index is out of the list bounds");
}
else if (data == null) {
throw new IllegalArgumentException("You cannot add null data to the list");
}
else {
if (index == 0) {
Node<T> temp = new Node(data,null);
temp.setNext(head);
head = temp;
size ++;
}
else if (index < (size-1) && index > 0){
Node<T> current = head;
for (int i = 0; i < index; i++) {
current = current.getNext();
}
Node<T> temp = new Node(data,null);
temp.setNext(current.getNext());
current.setNext(temp);
size ++;
}
else {
Node<T> current = head;
for (int i = 0; i < size; i++) {
current = current.getNext();
}
Node<T> temp = new Node(data,null);
current.setNext(temp);
tail = temp;
size++;
}
}
}
public T getAtIndex(int index) {
if ((index < 0) || (index >= size)) {
throw new IllegalArgumentException("Your index is out of the list bounds");
}
else {
Node<T> current = head;
for (int i = 0; i< index+1; i++) {
current = current.getNext();
}
return current.getData();
}
}
public T removeAtIndex(int index) {
T removed_data = null;
if ((index < 0) || (index >= size)) {
throw new IllegalArgumentException("Your index is out of the list bounds");
}
else {
if (size == 0) {
removed_data = head.getData();
head = null;
tail = null;
size = 0;
}
else if (index == 0 && size > 0) {
Node<T> removed = head;
removed_data = removed.getData();
head = head.getNext();
removed = null;
size--;
}
else if (index < (size-1) && size > 0) {
Node<T> current = head;
for (int i = 0; i< index; i++) {
current = current.getNext();
}
Node<T> removed = current.getNext();
removed_data = removed.getData();
current.setNext(removed.getNext());
removed = null;
size--;
}
else if (index == (size-1) && size > 0) {
Node<T> current = head;
for (int i = 0; i< index; i++) {
current = current.getNext();
}
Node<T> removed = current.getNext(); //current is tail
removed_data = removed.getData();
current.setNext(null);
removed = null;
tail = current;
size--;
}
} return removed_data;
}
public T remove(T data) {
T toReturn = null;
if (data == null) {
throw new IllegalArgumentException("You cannot remove null data to the list");
}
else {
Node<T> current = head;
int counter = 0;
while (current != null) {
current = current.getNext();
counter++;
if (current.getData() == data) {
toReturn = removeAtIndex(counter);
}
}
if (counter == size) {
throw new NoSuchElementException("The data is not present in the list.");
}
} return toReturn;
}
public void clear() {
head = null;
tail = null;
size = 0;
}
public boolean isEmpty() {
return (head == null);
}
public int size() {
Node<T> current = head;
int count = 0;
while (current != null) {
current = current.getNext();
count++;
}
return count;
}
}

How can I delete any node at random out of a Doubly Linked List in Java?

I'm making a Doubly Linked List that allows you to insert at the front and rear, as well as deleting any node from the list as long as it exists. The problem is that it doesn't work and gives off and either gives off a NullPointerException or it just says That Integer does not exist even though it does exist.The code is:
public class Numbers {
Node head = null; //Head of the list
Node tail = null; //end of the doubly list
int size = 0;
public void FrontInsert(int data) {
Node n = new Node();
if (head == null) {
head = n;
} else {
n.prev = head;
head.next = n;
head = n;
}
size++;
}
public void RearInsert(int data) {
Node n = new Node();
if (head == null) {
head = n;
tail = n;
} else {
n.next = tail;
tail.prev = n;
tail = n;
}
size++;
}
public void Delete(int x) {
if (size == 0) {
System.out.println("The list is empty.");
}
if (head.data == x) {
head = head.next;
if (head != null) {
head.prev = null;
}
size--;
return;
}
tmp = head;
while (tmp != null && tmp.data != x) {
tmp = tmp.next;
}
if (tmp == null) {
System.out.println("That integer does not exist.");
return;
}
if (tmp.data == x) {
tmp.prev.next = tmp.next;
if (tmp.next != null) {
tmp.next.prev = tmp.prev;
}
}
size--;
}
public void printList() {
while (head != null) {
System.out.print(head.data + " ");
head = head.prev;
}
}
public static void main(String[] args) {
Numbers nu = new Numbers();
}
class Node {
Node prev;
Node next;
int data;
public void Node(int data) {
this.data = data;
next = null;
prev = null;
}
}
}
Try this and check output
public class Numbers {
Node head = null;
Node tail = null;
int size = 0;
public void FrontInsert(int data) {
Node n = new Node(data);
if (head == null) { // first insert
head = n;
tail = n;
} else {
n.next = head;
head.prev = n;
head = n;
}
size++;
}
public void RearInsert(int data) {
Node n = new Node(data);
if (head == null) {
head = n;
tail = n;
} else {
n.prev = tail;
tail.next = n;
tail = n;
}
size++;
}
public void Delete(int index) { // index is the position to be remove
if (size == 0) {
System.out.println("The list is empty."); return;
}else if(index < 0 || index > size -1){
System.out.println("Index outOf Bound."); return;
}
Node currentNode = head;
for(int i = 1; i <= index ; i++){
currentNode = currentNode.next;
}
//remove
if (index == 0) {
currentNode.next.prev = null;
head = currentNode.next;
} else if (index == size - 1) {
currentNode.prev.next = null;
tail = currentNode.prev;
} else {
if (currentNode.prev != null) // Ensure its not header
currentNode.prev.next = currentNode.next;
if (currentNode.next != null) // Ensure its not tail
currentNode.next.prev = currentNode.prev;
}
size--;
}
public void printList() {
Node tmp = head;
while (tmp != null) {
System.out.print(tmp.data + " ");
tmp = tmp.next;
}
System.out.println();
}
public static void main(String[] args) {
Numbers nu = new Numbers();
nu.FrontInsert(1);nu.printList();
nu.FrontInsert(2);nu.printList();
nu.RearInsert(3);nu.printList();
nu.FrontInsert(4);nu.printList();
nu.RearInsert(3);nu.printList();
nu.FrontInsert(4);nu.printList();
nu.RearInsert(3);nu.printList();
nu.RearInsert(3);nu.printList();
nu.FrontInsert(4);nu.printList();
System.out.println();
nu.Delete(4);
nu.printList();
}
class Node {
Node prev;
Node next;
int data;
public Node(int data) {
this.data = data;
next = null;
prev = null;
}
}
}
Well, your head and tail were mutually exclusive, i mean when you add something to the tail of the list, you were only giving one side reference not both side, you have to says
tail.next = n; n.prev = tail; and tail = n.
Here is a working code:
public class Numbers {
Node head = null; //Head of the list
Node tail = null; //end of the doubly list
int size = 0;
public void FrontInsert(int data) {
Node n = new Node(data);
if (head == null) {
head = n;
tail = head;
} else {
n.next = head;
head.prev = n;
head = n;
}
size++;
}
public void RearInsert(int data) {
Node n = new Node(data);
if (head == null) {
head = n;
tail = head;
} else {
n.next = null;
tail.next = n;
n.prev = tail;
tail = n;
}
size++;
}
#SuppressWarnings("null")
public void Delete(int x) {
if (size == 0) {
System.out.println("The list is empty.");
return;
}
if (head.data == x) {
head = head.next;
if (head != null) {
head.prev = null;
}
size--;
return;
}
Node tmp = head;
while (true) {
if(tmp == null)
break;
if(tmp.data == x)
break;
System.out.println(tmp.data);
tmp = tmp.next;
}
if (tmp == null) {
System.out.println("That integer does not exist.");
return;
}
if (tmp.data == x) {
tmp.prev.next = tmp.next;
if (tmp.next != null) {
tmp.next.prev = tmp.prev;
}
}
size--;
}
public void printList() {
while (head != null) {
System.out.print(head.data + " ");
head = head.next;
}
}
public static void main(String[] args) {
Numbers nu = new Numbers();
nu.FrontInsert(2);
nu.FrontInsert(3);
nu.FrontInsert(6);
nu.RearInsert(8);
nu.RearInsert(20);
nu.Delete(8);
nu.printList();
// System.out.println(nu.head.data + "data");
// System.out.println(nu.head.next.data + "data");
}
class Node {
Node prev;
Node next;
private int data;
public Node(int data) {
this.data = data;
next = null;
prev = null;
}
}
}

Finding the oldest element in a min priority queue java

I am supposed to return the oldest element in a priority min queue along with said element. I have to use nodes, arrays are optional. This is what I got so far but I have an null pointer error on line 20 and i don't know how to fix it. Please help
public class MinHeap {
public static int timeStamp = 0;
public static int ts = 0;
public static int maxTime = 0;
public static Node root;
public MinHeap(){
this.root = null;
}
public static void insert(int id, int ts){
timeStamp++;
Node newNode = new Node(id);
if(root==null){
root = newNode;
}
Node current = root;
Node parent = null;
while(true){
parent = current;
if(id < current.data){
current = current.left;
if(current==null){
parent.left = newNode;
}
}else{
current = current.right;
if(current==null){
parent.right = newNode;
}
}
}
}
public static Node delete(int x, Node n){
timeStamp++;
if(n==null)
return n;
if(x == n.data){
if(n.left == null && n.right == null){
return null;
}else if(n.left == null){
n.right = delete(x, n.right);
return n;
}else if(n.right == null){
n.left = delete(x, n.left);
return n;
}else{
Node tempNode = findMin(n.right);
n.right = delete(tempNode.data, n.right);
n.data = tempNode.data;
return n;
}
}
if(x < n.data){
n.left = delete(x, n.left);
return n;
}else{
n.right = delete(x, n.right);
return n;
}
}
public static void display(Node root){
if(root!=null){
display(root.left);
System.out.print(" " + root.data);
display(root.right);
}
}
public static Node findMin(Node n){
if(n == null){
return null;
}
if(n.left == null){
return n;
}
return findMin(n.left);
}
public static int maxAge(){
int currentAge = 0;
currentAge = timeStamp - ts;
if(currentAge > maxTime)
maxTime = currentAge;
return maxTime;
}
public static void main(String [] arg){
MinHeap min = new MinHeap();
min.insert(1, ts = 0);
min.insert(2, ts = 1);
min.insert(3, ts = 2);
min.insert(4, ts = 4);
min.delete(1, root);
min.delete(2, root);
min.delete(3, root);
min.delete(4, root);
min.maxAge();
}
}
class Node{
int data;
Node left;
Node right;
public Node(int data){
this.data = data;
left = null;
right = null;
}
}
Your insert is broken. You forgot to break after finding the insert point, and you're not checking for the case where the key is already in the tree.
while (true) {
parent = current;
if (id < current.data) {
current = current.left;
if (current == null) {
parent.left = newNode;
// Break after insert
break;
}
} else if (id > current.data) {
current = current.right;
if (current == null) {
parent.right = newNode;
// Break after insert
break;
}
} else {
// Key exists.
break;
}
}

Erorr with the nullpointer

public class List {
private class Node {
public Node next = null;
Object element;
Node text;
Node(Object element, Node prevNode) {
this.element = element;
prevNode = this;
}
Node(Object element) {
this.element = element;
element = null;
}
}
private Node head;
private Node tail;
private int count;
public List() {
this.head = null;
this.tail = null;
this.count = 0;
}
public void add(Object item) {
if (head == null) {
// We have empty list
head = new Node(item);
tail = head;
} else {
// We have non-empty list
Node newNode = new Node(item, tail);
tail = newNode;
}
count++;
}
public Object remove(int index) {
if (index >= count || index < 0) {
throw new IndexOutOfBoundsException("Invalid index: " + index);
}
// Find the element at the specified index
int currentIndex = 0;
Node currentNode = head;
Node prevNode = null;
while (currentIndex < index) {
prevNode = currentNode;
currentNode = currentNode.next;
currentIndex++;
count--;
if (count == 0) {
head = null;
tail = null;
} else if (prevNode == null) {
head = currentNode.next;
} else {
prevNode.next = currentNode.next;
}
}
return currentNode.element;
}
public int remove(Object item) {
// Find the element containing searched item
int currentIndex = 0;
Node currentNode = head;
Node prevNode = null;
while (currentNode != null) {
if ((currentNode.element != null && currentNode.element
.equals(item))
|| (currentNode.element == null)
&& (item == null)) {
break;
}
prevNode = currentNode;
currentNode = currentNode.next;
currentIndex++;
}
if (currentNode != null) {
// Element is found in the list. Remove it
count--;
if (count == 0) {
head = null;
tail = null;
} else if (prevNode == null) {
head = currentNode.next;
} else {
prevNode.next = currentNode.next;
}
return currentIndex;
} else {
// Element is not found in the list
return -1;
}
}
public int indexOf(Object item) {
int index = 0;
Node current = head;
while (current != null) {
if ((current.element != null && current.element.equals(item))
|| (current.element == null) && (item == null)) {
return index;
}
current = current.next;
index++;
}
return -1;
}
public boolean contains(Object item) {
int index = indexOf(item);
boolean found = (index != -1);
return found;
}
public Object elementAt(int index) {
if (index >= count || index < 0) {
throw new IndexOutOfBoundsException("Invalid index: " + index);
}
Node currentNode = this.head;
for (int i = 0; i < index; i++) {
currentNode = currentNode.next;
}
return currentNode.element;
}
public int getLength() {
return count;
}
public static void main(String[] args) throws NullPointerException {
List shoppingList = new List();
shoppingList.add("Milk");
shoppingList.add("Honey");
shoppingList.add("Olives");
shoppingList.add("Beer");
shoppingList.remove("Olives");
System.out.println("We need to buy:");
for (int i = 0; i < shoppingList.getLength(); i++) {
System.out.println(shoppingList.elementAt(i));
}
System.out.println("Do we have to buy Bread? "
+ shoppingList.contains("Bread"));
}
}
Its giving me that in maina and index of have nullpointer.
Where is my mistake ?
And i want to know how to fix this problem. Thank you.
I would be very grateful if you could give me some feedback for code.
You do not set next when you create your Nodes, so next is always null.
This
Node(final Object element, Node prevNode) {
this.element = element;
prevNode = this;
}
should be
Node(final Object element, Node prevNode) {
this.element = element;
prevNode.next = this;
}
I understand there's something in your code that's giving you nullpointerException when you run the program, but there's no need of it being in the main method, any method called by main may be giving the exception. Still, try this:
Replace this line:
for (int i=0; i<shoppingList.getLength(); i++) {
by this one:
for (int i=0; i < shoppingList.length(); i++) {
if .length() doesn't work, maybe .size() will do.
for (int i=0; i<shoppingList.size(); i++) {
If you are using eclipse, netbeans or any other environment, post which line is throwing the NullPointerException. We can't help you unless you give us more data.

Java Linked list-NullPointerException [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 8 years ago.
so I am having a few errors in my code but Im not sure what they are telling me to change. This is my first linked list code. If anyone can help me out i would appreciate it.
This is my linked list
public class MyLinkedList<E>
{
private Node<E> head = null;
public void add(E element)
{
if(size() == 0)
{
head = new Node<E>(element);
return;
}
Node<E> cursor = head;
while (cursor.next != null)
{
cursor = cursor.next;
}
cursor.next = new Node<E>(element);
}
public void add(int index, E element)
{
Node<E> cursor = head;
E temp, before;
for(int x = 0; x < index; x++)
{
cursor = cursor.next;
}
before = cursor.content;
cursor.content = element;
while(cursor.next != null)
{
cursor = cursor.next;
temp = cursor.content;
cursor.content = before;
before = temp;
}
add(before);
}
public boolean remove(E element)
{
Node<E> cursor = head;
if (head.content == element)
{
head = cursor.next;
return true;
}
while(cursor.next != null)
{
if (cursor.next.content == element)
{
cursor.next = cursor.next.next;
}
else
{
cursor = cursor.next;
}
}
if (cursor.next == null)
{
return false;
}
return true;
}
public E remove(int index)
{
E result = null;
if (index < 0 || index >= size())
{
return null;
}
Node<E> cursor = head;
for (int x = 0; x < index; x++)
{
cursor = cursor.next;
}
result = cursor.content;
cursor = head;
for (int x = 0; x < index - 1; x++)
{
cursor = cursor.next;
}
if(index != 0)
{
cursor.next = cursor.next.next;
}
else
{
head = cursor.next;
}
return result;
}
public E set(int index, E element)
{
Node<E> cursor = head;
E temp;
for (int x = 0; x < index; x++)
{
cursor = cursor.next;
}
temp = cursor.content;
cursor.content = element;
return temp;
}
public boolean contains(E element)
{
Node<E> cursor = head;
while(cursor != null)
{
if(cursor.content == element)
{
return true;
}
cursor = cursor.next;
}
return false;
}
public E get(int index)
{
Node<E> cursor = head;
if (index < 0 || index >= size())
{
return null;
}
for (int x = 0; x < index; x++)
{
cursor = cursor.next;
}
return cursor.content;
}
public int indexOf(E element)
{
Node<E> cursor = head;
int index = 0;
while (cursor != null)
{
if(cursor.content == element)
{
return index;
}
index++;
cursor = cursor.next;
}
return -1;
}
public boolean isEmpty()
{
if (size() == 0)
{
return true;
}
return false;
}
public int size()
{
Node<E> cursor = head;
int count = 0;
while (cursor != null)
{
count++;
cursor = cursor.next;
}
return count;
}
public void dumpList()
{
Node<E> cursor = head;
while (cursor != null)
{
System.out.println(cursor.content);
cursor = cursor.next;
}
}
}
This is my node code
public class Node<E>
{
public E content;
public Node<E> next;
public Node(E content)
{
this.content = content;
}
public Node(E content, Node<E> next)
{
this(content);
this.next = next;
}
public String toString()
{
return content.toString();
}
}
and this is the code we are testing it with
public class Demo4
{
public static void main(String[] args)
{
MyLinkedList<String> t = new MyLinkedList<String>();
t.add("Santa Maria");
t.add("Los Angeles");
t.add("Ventura");
t.add("Thousand Oaks");
t.add(0, "Orcutt");
t.add(5, "Pismo");
t.add(3, "San Luis Obispo");
t.set(1, "London");
t.set(0, "San Diego");
t.set(6, "Tokyo");
t.add("Westlake");
t.remove("Santa Maria");
System.out.println("was Tokyo found? " + t.remove("Tokyo"));
t.remove("Westlake");
System.out.println("was Dubai found? " + t.remove("Dubai"));
t.remove("Pismo");
System.out.println("Remove index 5. It contained: " + t.remove(5));
System.out.println("Remove index 0. It contained: " + t.remove(0));
System.out.println("Remove index 2. It contained: " + t.remove(2));
System.out.println("Here's what's left over");
for (int x = 0; x < t.size(); x++)
{
System.out.println(t.get(x));
}
System.out.println("--------");
System.out.println("Cool! I didn't crash!");
}
}
my error in eclipse is the following
Exception in thread "main" java.lang.NullPointerException
at MyLinkedList.add(MyLinkedList.java:35)
at MyLinkedListDemo.main(MyLinkedListDemo.java:12)
See, Eclipse is telling you everything a human can tell you :)
this is your error:
Exception in thread "main" java.lang.NullPointerException
at MyLinkedList.add(MyLinkedList.java:35)
at MyLinkedListDemo.main(MyLinkedListDemo.java:12)
Eclipse says "there is a Null Pointer Exception" in line no 35 of the file MyLinkedList.java, when the add() method is called. This was actually invoked my the main() of MyLinkedListDemo.java in line no 12.
Now put a debug point on that line, and you see what is null and why it is null.
You get a NPE, when you try to invoke something on null
You are getting a null pointer exception because you are trying to insert value to a position that doesn't exist in the memory. Remove the lines that add to index 5 and 6 and your code will work.
public class Demo4 {
public static void main(String[] args) {
MyLinkedList<String> t = new MyLinkedList<String>();
t.add("Santa Maria");
t.add("Los Angeles");
t.add("Ventura");
t.add("Thousand Oaks");
t.add(0, "Orcutt");
t.add(3, "San Luis Obispo");
t.set(1, "London");
t.set(0, "San Diego");
t.add("Westlake");
t.remove("Santa Maria");
System.out.println("was Tokyo found? " + t.remove("Tokyo"));
t.remove("Westlake");
System.out.println("was Dubai found? " + t.remove("Dubai"));
t.remove("Pismo");
System.out.println("Remove index 5. It contained: " + t.remove(5));
System.out.println("Remove index 0. It contained: " + t.remove(0));
System.out.println("Remove index 2. It contained: " + t.remove(2));
System.out.println("Here's what's left over");
for (int x = 0; x < t.size(); x++) {
System.out.println(t.get(x));
}
System.out.println("--------");
System.out.println("Cool! I didn't crash!");
}
}

Categories