当前位置: 代码迷 >> J2SE >> 有关此程序的空指针异常一直搞不清楚如何改
  详细解决方案

有关此程序的空指针异常一直搞不清楚如何改

热度:18   发布时间:2016-04-23 21:03:11.0
有关此程序的空指针错误一直搞不清楚怎么改
import java.util.Scanner;

public class Josephus {
int number,start,password,m;
String name;
Scanner input = new Scanner(System.in);
SeqList<Integer> listNum ;
SeqList<String> listName ;

public void Josephus1 (int start,int m) {
int i = start ;

while (listName.length() > 1) {
i = (i + m)%(listName.length() + 1);
System.out.println("删除" + listName.remove(i).toString());
m = Integer.parseInt(listNum.remove(i).toString());
}
if (listName.length() == 1) {
System.out.println(listName.toString());
}
}

public void JosephusName (String n) {
listName.append(n);
//return n;
}

public void JosephusNum (int n) {
listNum.append(n);
//return n;
}

public void made () {
System.out.println("请输入总人数(正整数)并按回车键确定");
this.number = input.nextInt();

if (this.number <= 0) {
System.out.println("请输入正整数");


this.listNum = new SeqList<Integer>(this.number);
this.listName = new SeqList<String>(this.number);
}


public void get() {

for(int i = 0;i < number;i++) {
System.out.println("请输入姓名及密码(正整数)并按回车键确定,(姓名和密码之间用空格隔开)");
this.name = input.next();
this.JosephusName(this.name);
this.password = input.nextInt();
this.JosephusNum(this.password);
}

System.out.println("输入开始报数人的位置及初始报数上限,(中间用空格隔开)");
this.start = input.nextInt();
this.m = input.nextInt();
this.Josephus1(this.start,this.m);
}

public static void main (String[] args) {
Josephus J1 = new Josephus();
J1.made();
J1.get();

}
}

class SeqList<T> {
private Object[] element;
private int len;
public SeqList (int size) {
this.element = new Object[size];
this.len = size;
}
public SeqList () { this(64); }
public boolean isEmpty () { return this.len == 0; }
public int  length () { return this.len; }
public T get (int i) {
if (i >= 0 && i < this.len)
return (T)this.element[i];
return null;
}
public void insert (int i,T x) {
if (x == null)
return;
if (this.len == element.length) {
Object[] temp = this.element;
this.element = new Object[temp.length*2];
for (int j = 0;j < temp.length;j++)
this.element[j] = temp[j];
}
if (i < 0)
i = 0;
if( i > this.len)
i = this.len;
for (int j = this.len - 1;j >= i;j--)
this.element[j + 1] = this.element[j];
this.element[i] = x;
this.len++;
}

public void append (T x) { insert(this.len,x); }

public T remove (int i) {
if (this.len == 0 || i < 0 || i >= this.len)
return null;
T old = (T)this.element[i];
for (int j = i;j < this.len - 1;j++)
this.element[j] = this.element[j+1];
this.element[this.len - 1] = null;
this.len--;
return old;
}
public void removeAll() { this.len = 0; }

public String toString () {
String str = "(";
if (this.len > 0)
str += this.element[0].toString();
for (int i = 1;i < this.len;i++)
str += "," + this.element[i].toString();
return str + ")";
}

}

------解决方案--------------------
楼主说实话  建议使用JDK的arrayList   
this.listNum = new SeqList<Integer>(this.number);
this.listName = new SeqList<String>(this.number);
这两行是导致空指针的原因,而且之后调用append(),并没有解决数组元素为null的问题
楼主可以自己查看
for(int j=0;j<listName.length();j++){
System.out.println(listName.get(j));
}
------解决方案--------------------
在遍历List的时候,最好不用remove方法。不然会报空指针异常