Java链表NullPointerException
所以我的代码中有一些错误,但我不确定他们告诉我要更改什么。这是我的第一个链表代码。如果有人能帮我,我将不胜感激 这是我的链表Java链表NullPointerException,java,list,linked-list,Java,List,Linked List,所以我的代码中有一些错误,但我不确定他们告诉我要更改什么。这是我的第一个链表代码。如果有人能帮我,我将不胜感激 这是我的链表 public class MyLinkedList<E> { private Node<E> head = null; public void add(E element) { if(size() == 0) { head = new Node<E>(element); retu
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;
}
}
}
公共类MyLinkedList
{
私有节点头=null;
公共无效添加(E元素)
{
如果(size()=0)
{
head=新节点(元素);
返回;
}
节点光标=头部;
while(cursor.next!=null)
{
cursor=cursor.next;
}
cursor.next=新节点(元素);
}
公共void add(int索引,E元素)
{
节点光标=头部;
E温度,前;
对于(int x=0;x=size())
{
返回null;
}
节点光标=头部;
对于(int x=0;x=size())
{
返回null;
}
对于(int x=0;x
这是我的节点代码
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();
}
}
公共类节点
{
公共电子内容;
公共节点下一步;
公共节点(电子内容)
{
this.content=内容;
}
公共节点(E内容,节点下一步)
{
这(内容),;
this.next=next;
}
公共字符串toString()
{
返回content.toString();
}
}
这就是我们正在测试的代码
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)
公共类演示4
{
公共静态void main(字符串[]args)
{
MyLinkedList t=新的MyLinkedList();
t、 加上(“圣玛丽亚”);
t、 添加(“洛杉矶”);
t、 添加(“文图拉”);
t、 加上(“千橡树”);
t、 加上(0,“Orcutt”);
t、 添加(5,“Pismo”);
t、 添加(3,“圣路易斯奥比斯波”);
t、 第(1)组,“伦敦”);
t、 集(0,“圣地亚哥”);
t、 第(6)组,“东京”);
t、 添加(“西湖”);
t、 移除(“圣玛丽亚”);
System.out.println(“找到东京了吗?”+t.remove(“东京”);
t、 移除(“西湖”);
System.out.println(“迪拜被发现了吗?”+t.remove(“迪拜”);
t、 移除(“Pismo”);
System.out.println(“删除索引5,它包含:”+t.Remove(5));
System.out.println(“删除索引0。它包含:”+t.Remove(0));
System.out.println(“删除索引2,它包含:”+t.Remove(2));
System.out.println(“这是剩下的内容”);
对于(int x=0;x
看,Eclipse告诉你人类能告诉你的一切:)
这是您的错误:
Exception in thread "main" java.lang.NullPointerException
at MyLinkedList.add(MyLinkedList.java:35)
at MyLinkedListDemo.main(MyLinkedListDemo.java:12)
调用add()方法时,Eclipse在文件MyLinkedList.java的第35行中说“存在空指针异常”。这实际上是在第12行MyLinkedListDemo.java的main()中调用的
现在在该行上放置一个调试点,您将看到什么是null以及为什么是null。
当您尝试在null上调用某个对象时,您会得到一个NPE,因为您试图将值插入到内存中不存在的位置,所以您会得到一个null指针异常。删除添加到索引5和索引6的行,代码将正常工作
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!");
}
公共类演示4{
公共静态void main(字符串[]args){
MyLinkedList t=新的MyLinkedList();
t、 加上(“圣玛丽亚”);
t、 添加(“洛杉矶”);
t、 添加(“文图拉”);
t、 加上(“千橡树”);
t、 加上(0,“Orcutt”);
t、 添加(3,“圣路易斯奥比斯波”);
t、 第(1)组,“伦敦”);
t、 集(0,“圣地亚哥”);
t、 添加(“西湖”);
t、 移除(“圣玛丽亚”);
System.out.println(“找到东京了吗?”+t.remov