显示链表:空指针异常错误
Display Linked List: Null Pointer Exception Error
我在尝试输出我的链表时一直收到空异常错误。
当我只打印条件名称和空罐测试值时,我没有得到那个错误!!!?不确定原因?
但是,当我尝试遍历整个链表以打印出来时,出现空异常错误
public class ItemLinkedList {
private ItemInfoNode head;
private ItemInfoNode tail;
private int size = 0;
public int getSize() {
return size;
}
public void addBack(ItemInfo info) {
size++;
if (head == null) {
head = new ItemInfoNode(info, null, null);
tail = head;
} else {
ItemInfoNode node = new ItemInfoNode(info, null, tail);
this.tail.next = node;
this.tail = node;
}
}
public ItemInfo removeFront() {
ItemInfo result = null;
if (head != null) {
size--;
result = head.info;
if (head.next != null) {
head.next.prev = null;
head = head.next;
} else {
head = null;
tail = null;
}
}
return result;
}
public static void main(String[] args) {
ItemLinkedList list = new ItemLinkedList();
list.addBack( new ItemInfo("Bicipital Tendonitis", 1, 0, 1, 1) );
list.addBack( new ItemInfo("Coracoid Impingement", 0, 1, 1, 1) );
list.addBack( new ItemInfo("Supraspinatus Impingement", 1, 0, 0, 1) );
list.addBack( new ItemInfo("Bicipital Tendonitis", 1, 0, 1, 1) );
list.addBack( new ItemInfo("Glenohumeral Dislcation", 0, 0, 1, 1) );
list.addBack( new ItemInfo("Clavicular Fracture", 1, 0, 1, 0) );
list.addBack( new ItemInfo("Labral Tear", 1, 1, 0, 0) );
list.addBack( new ItemInfo("SubAcromial Bursitis", 1, 0, 0, 0) );
while (list.getSize() > 0){
System.out.println( "Condition Name " + list.removeFront().getCondName() );
System.out.println( "\t Empy Can Test: " + list.removeFront().getEmptyCanTest() );
System.out.println( "\t Speed's Test: " + list.removeFront().getSpeedsTest() );
System.out.println( "\t Apprehension Test: " + list.removeFront().getApprehensionTest() );
System.out.println( "\t Pain Provocation Test: " + list.removeFront().getpainProvocationTest() );
System.out.println();
}
}
您的循环条件检查列表中是否至少有一个元素,但随后您尝试从列表中删除 5 个元素。
你应该在每次迭代中只调用一次 removeFront
:
while (list.getSize() > 0){
ItemInfo item = list.removeFront();
System.out.println( "Condition Name " + item.getCondName() );
System.out.println( "\t Empy Can Test: " + item.getEmptyCanTest() );
System.out.println( "\t Speed's Test: " + item.getSpeedsTest() );
System.out.println( "\t Apprehension Test: " + item.getApprehensionTest() );
System.out.println( "\t Pain Provocation Test: " + item.getpainProvocationTest() );
System.out.println();
}
我在尝试输出我的链表时一直收到空异常错误。
当我只打印条件名称和空罐测试值时,我没有得到那个错误!!!?不确定原因?
但是,当我尝试遍历整个链表以打印出来时,出现空异常错误
public class ItemLinkedList {
private ItemInfoNode head;
private ItemInfoNode tail;
private int size = 0;
public int getSize() {
return size;
}
public void addBack(ItemInfo info) {
size++;
if (head == null) {
head = new ItemInfoNode(info, null, null);
tail = head;
} else {
ItemInfoNode node = new ItemInfoNode(info, null, tail);
this.tail.next = node;
this.tail = node;
}
}
public ItemInfo removeFront() {
ItemInfo result = null;
if (head != null) {
size--;
result = head.info;
if (head.next != null) {
head.next.prev = null;
head = head.next;
} else {
head = null;
tail = null;
}
}
return result;
}
public static void main(String[] args) {
ItemLinkedList list = new ItemLinkedList();
list.addBack( new ItemInfo("Bicipital Tendonitis", 1, 0, 1, 1) );
list.addBack( new ItemInfo("Coracoid Impingement", 0, 1, 1, 1) );
list.addBack( new ItemInfo("Supraspinatus Impingement", 1, 0, 0, 1) );
list.addBack( new ItemInfo("Bicipital Tendonitis", 1, 0, 1, 1) );
list.addBack( new ItemInfo("Glenohumeral Dislcation", 0, 0, 1, 1) );
list.addBack( new ItemInfo("Clavicular Fracture", 1, 0, 1, 0) );
list.addBack( new ItemInfo("Labral Tear", 1, 1, 0, 0) );
list.addBack( new ItemInfo("SubAcromial Bursitis", 1, 0, 0, 0) );
while (list.getSize() > 0){
System.out.println( "Condition Name " + list.removeFront().getCondName() );
System.out.println( "\t Empy Can Test: " + list.removeFront().getEmptyCanTest() );
System.out.println( "\t Speed's Test: " + list.removeFront().getSpeedsTest() );
System.out.println( "\t Apprehension Test: " + list.removeFront().getApprehensionTest() );
System.out.println( "\t Pain Provocation Test: " + list.removeFront().getpainProvocationTest() );
System.out.println();
}
}
您的循环条件检查列表中是否至少有一个元素,但随后您尝试从列表中删除 5 个元素。
你应该在每次迭代中只调用一次 removeFront
:
while (list.getSize() > 0){
ItemInfo item = list.removeFront();
System.out.println( "Condition Name " + item.getCondName() );
System.out.println( "\t Empy Can Test: " + item.getEmptyCanTest() );
System.out.println( "\t Speed's Test: " + item.getSpeedsTest() );
System.out.println( "\t Apprehension Test: " + item.getApprehensionTest() );
System.out.println( "\t Pain Provocation Test: " + item.getpainProvocationTest() );
System.out.println();
}