java中空指针异常问题

yang_chen 2008-05-20 12:55:09
package dataStruct;
import java.util.Random;

public class LineLink {
private Node head;
private int nElems;
public LineLink(){
head = null;
nElems = 0;
}
public boolean isEmpty(){
return nElems == 0;
}
public boolean insert(int x){
return insert(x,nElems);
}
public boolean insert(int x,int index){
if (index < 0 || index > nElems) {
System.out.println("待插入元素的索引号超过范围");
return false;
}
Node n = new Node(x);
if(head == null){
head = n;
}else{
Node p = head;
int count = 0;
while(count < index){
p = p.next;
count ++;
}
n.next = p.next;
p.next = n;
}
nElems ++;
return true;
}
public Node find(int key){
if(isEmpty()){
System.out.println("对不起,此链表是空的,无数据可查");
return null;
}else{
Node current = head;
int count = 0;
while(current.data != key){
if(current.next == null){
System.out.println("对不起,此链表中没有您要查找的数据");
return null;
}else{
current = current.next;
count ++;
}
}
System.out.println("您要查找的数据在此链表中的"+count+"位置");
return current;
}
}
public Node remove(int x){
return remove(x,nElems);
}
public Node remove(int x,int index){
if (index < 0 || index > nElems - 1 || nElems == 0) {
System.out.println("待删除元素的索引号超过范围或表已空");
return null;
}else{
Node current = head;
Node temp = head;
int count = 0;
while(count < index && current.data != x){
if(current.next == null){
System.out.println("对不起,此链表中没有您要查找的数据");
return null;
}else{
temp = current;
current = current.next;
count ++;
}
}
temp.next = current.next;
current.next = null;
return current;
}
}
public void display() {
if (nElems > 0) {
Node p = head;
while (p != null) {
System.out.print(p.data + " ");
p = p.next;
}
System.out.println();
} else {
System.out.println("这是一个空表");
}
}
public static void main(String[] args){
LineLink link = new LineLink();
Random ran = new Random();
link.display();
for(int i = 0; i < 20; i ++){
link.insert(ran.nextInt(50));
}
link.display();
}
}
class Node{
public int data;
public Node next;
public Node(int data){
this.data = data;
this.next = null;
}
}

运行后抛出:
Exception in thread "main" java.lang.NullPointerException
at dataStruct.LineLink.insert(LineLink.java:32)
at dataStruct.LineLink.insert(LineLink.java:15)
at dataStruct.LineLink.main(LineLink.java:101)
...全文
127 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yang_chen 2008-05-20
  • 打赏
  • 举报
回复
//自己解决了,谢谢
package dataStruct;
import java.util.Random;

public class LineLink {
private Node head;
private int nElems;
public LineLink(){
head = null;
nElems = 0;
}
public boolean isEmpty(){
return nElems == 0;
}
public boolean insert(int x){
return insert(x,nElems);
}
public boolean insert(int x,int index){
if (index < 0 || index > nElems) {
System.out.println("待插入元素的索引号超过范围");
return false;
}
Node n = new Node(x);
if(head == null){
head = n;
}else{
Node p = head;
int count = 0;
Node current=head;
while(count < index){
current = p;
p = p.next;
count ++;
}
n.next = current.next;
current.next = n;
//p = n;
}
nElems ++;
return true;
}
public Node find(int key){
if(isEmpty()){
System.out.println("对不起,此链表是空的,无数据可查");
return null;
}else{
Node current = head;
int count = 0;
while(current.data != key){
if(current.next == null){
System.out.println("对不起,此链表中没有您要查找的数据");
return null;
}else{
current = current.next;
count ++;
}
}
System.out.println("您要查找的数据在此链表中的"+count+"位置");
return current;
}
}
public Node remove(int x){
return remove(x,nElems);
}
public Node remove(int x,int index){
if (index < 0 || index > nElems - 1 || nElems == 0) {
System.out.println("待删除元素的索引号超过范围或表已空");
return null;
}else{
Node current = head;
Node temp = head;
int count = 0;
while(count < index && current.data != x){
if(current.next == null){
System.out.println("对不起,此链表中没有您要查找的数据");
return null;
}else{
temp = current;
current = current.next;
count ++;
}
}
temp.next = current.next;
current.next = null;
return current;
}
}
public void display() {
if (nElems >0) {
Node p = head;
while (p != null) {
System.out.print(p.data + " ");
p = p.next;
}
System.out.println();
} else {
System.out.println("这是一个空表");
}
}
public static void main(String[] args){
LineLink link = new LineLink();
Random ran = new Random();
link.display();
for(int i = 0; i < 20; i ++){
link.insert(ran.nextInt(50));
}
link.display();
}
}
class Node{
public int data;
public Node next;
public Node(int data){
this.data = data;
this.next = null;
}
}
wjh0205 2008-05-20
  • 打赏
  • 举报
回复
你p.next本来就是空。怎么能再让p=p.next呢,这样会导致p也等于空。
我是小辉辉 2008-05-20
  • 打赏
  • 举报
回复
public boolean insert(int x,int index){
if (index < 0 ¦ ¦ index > nElems) {
System.out.println("待插入元素的索引号超过范围");
return false;
}
Node n = new Node(x);
if(head == null){
head = n;
}else{
Node p = head;
int count = 0;
while(count < index){
p = p.next;
count ++;
}
n.next = p.next;
p.next = n;
}
nElems ++;
return true;
}
改成:
public boolean insert(int x,int index){
if (index < 0 ¦ ¦ index > nElems) {
System.out.println("待插入元素的索引号超过范围");
return false;
}
Node n = new Node(x);
if(head == null){
head = n;
}else{
Node p = head;
int count = 0;
while(count < index){
p = p.next;
count ++;
}
// n.next = p.next;
// p.next = n;
}
nElems ++;
return true;
}
你看看过不过,我诂计是你的那个p在while循环后以到最后的元素了,p.next为null了,所以报空指针异常了

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧