我不知道我在哪里出错了,我检查并重新检查了我的add(int index,T obj)方法无数次,仍然得到同样的错误。这是我的代码,任何指针都会非常感谢。至少现在这个问题一直阻碍我的项目一天左右。NullPointerException当在指定的索引处将节点添加到双链表时Java
package edu.neumont.csc250;
class LinkedList<T> implements List<T>{
Node<T> head;
Node<T> tail;
int listCount;
public LinkedList(){
head = null;
listCount = 0;
}
@Override
public T get(int index) throws IllegalArgumentException {
if(index > size() - 1 || index < 0){
throw new IllegalArgumentException();
}
else{
Node<T> current = head;
for(int i = 0; i < index; i++)
{
current = current.next;
}
if(current.content != null){
return current.content;
}
else{
System.out.println("Null value.");
return null;
}
}
}
@Override
public void add(T obj) {
if(head == null){
head = new Node<T>(obj);
head.next = null;
tail = head;
listCount++;
}
else{
if(head.next == null){
head.next = new Node<T>(obj);
//head.next.next = null;
tail = head.next;
tail.prev = head;
listCount++;
}
else{
tail.next = new Node<T>(obj);
tail.next.prev = tail;
tail = tail.next;
tail.next = null;
listCount++;
}
}
}
@Override
public void add(int index, T obj) throws IllegalArgumentException {
// TODO Auto-generated method stub
Node<T> temp = new Node(obj);
Node<T> current = head;
for(int i = 0; i<=index; i++){
current = current.next;
}
temp.prev = current.prev;
current.prev = temp;
current.prev.next = current;
if(index == 0){
head = current.prev;
}
else if(index == size()+1){
tail = current.next;
}
listCount++;
}
@Override
public void replace(int index, T obj) throws IllegalArgumentException {
// TODO Auto-generated method stub
if(index > size() - 1 || index < 0){
throw new IllegalArgumentException();
}
else{
//get(index)
}
}
@Override
public T remove() {
head = head.next;
listCount--;
return null;
}
@Override
public T remove(int index) throws IllegalArgumentException {
// TODO Auto-generated method stub
if(index > size() - 1 || index < 0){
throw new IllegalArgumentException();
}
else{
listCount--;
}
return null;
}
@Override
public int size() {
return listCount;
}
public static void main(String[] args){
LinkedList<String> list = new LinkedList<String>();
list.add("Red");
list.add("Orange");
list.add("Yellow");
list.add("Green");
list.add("Blue");
list.add("Purple");
for(int a = 0; a < list.size(); a++){
System.out.println(list.get(a));
}
// System.out.println(list.get(0).toString());
// System.out.println(list.get(1).toString());
// System.out.println(list.get(2).toString());
// System.out.println(list.get(3).toString());
// System.out.println(list.get(4).toString());
// System.out.println(list.get(5).toString());
System.out.println("There are " + list.size() + " colors in the list.");
list.remove();
for(int b = 0; b < list.size(); b++){
System.out.println(list.get(b));
}
// System.out.println(list.get(0).toString());
// System.out.println(list.get(1).toString());
// System.out.println(list.get(2).toString());
// System.out.println(list.get(3).toString());
// System.out.println(list.get(4).toString());
//System.out.println(list.get(5));
System.out.println("There are " + list.size() + " colors in the list.");
list.add(0, "Red");
System.out.println(list.size());
for(int c = 0; c < list.size(); c++){
System.out.println(list.get(c));
}
// System.out.println(list.get(0).toString());
// System.out.println(list.get(1).toString());
// System.out.println(list.get(2).toString());
// System.out.println(list.get(3).toString());
// System.out.println(list.get(4).toString());
// System.out.println(list.get(5).toString());
System.out.println("There are " + list.size() + " colors in the list.");
}
class Node<T>{
T content;
Node<T> next;
Node<T> prev;
public Node(T content){
this.content = content;
}
public T getContents(){
return content;
}
public void printNode() {
System.out.print("{" + content + "} ");
}
}
}
这里是我的控制台读取如果它的任何帮助:
红 橙黄色 绿色 蓝 紫 有6种颜色在列表中。 橙色 黄色 绿色 蓝色 紫色 列表中有5种颜色。 红 黄绿色 蓝 紫色 异常 线程 “主” 显示java.lang.NullPointerException在 edu.neumont.csc250.LinkedList.get(LinkedList.java:26)在 edu.neumont.csc250 .LinkedList.main(LinkedList.java:161)
编辑:主要方法分离,如请求:
public static void main(String[] args){
LinkedList<String> list = new LinkedList<String>();
list.add("Red");
list.add("Orange");
list.add("Yellow");
list.add("Green");
list.add("Blue");
list.add("Purple");
for(int a = 0; a < list.size(); a++){
System.out.println(list.get(a));
}
// System.out.println(list.get(0).toString());
// System.out.println(list.get(1).toString());
// System.out.println(list.get(2).toString());
// System.out.println(list.get(3).toString());
// System.out.println(list.get(4).toString());
// System.out.println(list.get(5).toString());
System.out.println("There are " + list.size() + " colors in the list.");
list.remove();
for(int b = 0; b < list.size(); b++){
System.out.println(list.get(b));
}
// System.out.println(list.get(0).toString());
// System.out.println(list.get(1).toString());
// System.out.println(list.get(2).toString());
// System.out.println(list.get(3).toString());
// System.out.println(list.get(4).toString());
//System.out.println(list.get(5));
System.out.println("There are " + list.size() + " colors in the list.");
list.add(0, "Red");
System.out.println(list.size());
for(int c = 0; c < list.size(); c++){
System.out.println(list.get(c));
}
// System.out.println(list.get(0).toString());
// System.out.println(list.get(1).toString());
// System.out.println(list.get(2).toString());
// System.out.println(list.get(3).toString());
// System.out.println(list.get(4).toString());
// System.out.println(list.get(5).toString());
System.out.println("There are " + list.size() + " colors in the list.");
}
第26行提到哪一行? – squawknull 2011-04-18 03:36:13
当current.next为空时会发生什么?如果没有得到例外,你将无法发行电流,因为电流将是空的,不是吗? – 2011-04-18 03:37:15
另外,你可以发布主代码? – squawknull 2011-04-18 03:38:02