在java中常常會看到有關空指針異常的錯誤,而且難以找出這樣的錯誤,索性就總結一下。java
· 產生空指針異常的緣由:ide
一、字符串變量沒有初始化。String str。你對字符串操做就會產生空指針異常。this
二、if(a.equals(null))或者if(a.equals("")),都有可能產生空指針異常。spa
三、java類沒有實例化。這裏的p->null,因此產生了空指針異常。指針
class person{ private String name; public person(String name){ this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } public class TestNullPointer { public static void main(String[] args) { person per = null; System.out.println(per.getName()); } }
這裏插入一個關於鏈表的NullPointerException。但願能給本身一些警示,能搞更加嚴謹。code
interface IMyLinkList{ public void clear();//清空 public int size(); public boolean isEmpty(); public boolean add(int value); public void add(int index,int value); public int get(int index); public int set(int index,int value); public int remove(int index); } class MyLinkList implements IMyLinkList{ private int TheSize; @SuppressWarnings("unused") private int modCount=0; private Node Begin; private Node End; public MyLinkList(){ DoClear(); } private static class Node{ public int data; public Node prveious; public Node next; public Node(int data, Node prveious, Node next) { super(); this.data = data; this.prveious = prveious; this.next = next; } } private void DoClear(){ Begin = new Node(0, null, null); End = new Node(0, Begin, null); Begin.next=End; TheSize=0; modCount++; } @Override public void clear() { DoClear(); } @Override public int size() { return TheSize; } @Override public boolean isEmpty() { return size()==0; } @Override public boolean add(int value) { add(size(),value);//###############這裏有問題 return true; } @Override public void add(int index, int value) { addBefore(getNode(index,0,size()),value);//獲得index這個節點。在前邊插入一個節點 } @Override public int get(int index) {//獲得這個節點的data數據 return getNode(index).data; } @Override public int set(int index, int value) {//設置index節點處的打他數據 Node p = getNode(index); int oldData = p.data; p.data = value; return oldData; } @Override public int remove(int index) { return remove(getNode(index)); } //初始化的時候出現了問題、 private void addBefore(Node p,int value){//添加一個新節點,只要new一個節點,而且改變節點的前驅和後繼 Node newNode = new Node(value,p.prveious,p); newNode.prveious.next=newNode; p.prveious=newNode; TheSize++; modCount++; } private Node getNode(int index){//獲得 return getNode(index,0,size()-1); } //getNode(index,0,size()) private Node getNode(int index,int lower,int upper){ Node p = null; //拋出異常 if(index<lower||index>upper){ throw new IndexOutOfBoundsException(); } //找到 if(index<size()/2){ p=Begin.next; for(int i=0;i<size()/2;i++){ p=p.next; } }else{ // 可是這麼卻沒有對p進行賦值。產生了空指針異常//p=End.previous
for(int i=size();i>index;i--){ p=p.prveious; } } return p; 而後若是判斷範圍在else中,就GG } private int remove(Node p){ p.next.prveious=p.prveious; p.prveious.next=p.next; TheSize--; modCount++; return p.data; } } public class MyfirstLinkedList { public static void main(String[] args) { IMyLinkList MyLinkedList = new MyLinkList(); System.out.println(MyLinkedList.add(12)); System.out.println(MyLinkedList.size()); } }