下面这个程序我在jdk1.6中编译通过了,但就是执行不了,错误提示为
Exception in thread "main" java.lang.NullPointerException
        at UseStudent.main(UseStudent.java:13)下面这个程序的功能是实现学生信息的增加和查询,参数全部通过args传入,并以end结尾
例如在dos窗口中输入:java UseStudent 1 abc 88 2 edf 76 2 end
那么最后会弹出结果:76
其中:1 abc 88 和2 edf 76 都是学生信息,后面的2是要查询的学生学号,end结束
希望有高手帮忙解决一下,小弟万分感谢
public class UseStudent
{
public static void main(String args[])
{
int i;
for (i = 0; !(args[i].equals("end")); i++)
;
int size = i;
size /= 3;
Student a[] = new Student[size];
for (i = 0; i < size; i++)
{
a[i].SetRecord(Integer.parseInt(args[3 * i]), args[3 * i + 1], Integer.parseInt(args[3 * i + 2]));
}
int searchNum = Integer.parseInt(args[3 * i]);
int searchScore = -1;
for (i = 0; i < size; i++)
{
if ((searchScore = a[i].GetRecord(searchNum)) != -1)
break;
}
if (searchScore == -1)
System.out.println("无此人!");
else
System.out.println(searchScore);
}
}class Student
{
int num;
String name;
int score; public void SetRecord(int _num, String _name, int _score)
{
num = _num;
name = _name;
score = _score;
} public int GetRecord(int _num)
{
if (num == _num)
return score;
else
return -1;
}
}

解决方案 »

  1.   


    public class UseStudent {
    public static void main(String args[]) {
    int i;
    for (i = 0; !(args[i].equals("end")); i++)
    ;
    int size = i;
    size /= 3;
    Student a[] = new Student[size];
    for (i = 0; i < size; i++) {
    a[i] = new Student();//楼主的没有这句,上面是对数组初始化,
        //但是数组中的每一个元素都是Student类的对象,要new的
    a[i].SetRecord(Integer.parseInt(args[3 * i]), args[3 * i + 1],
    Integer.parseInt(args[3 * i + 2]));
    }
    int searchNum = Integer.parseInt(args[3 * i]);
    int searchScore = -1;
    for (i = 0; i < size; i++) {
    if ((searchScore = a[i].GetRecord(searchNum)) != -1)
    break;
    }
    if (searchScore == -1)
    System.out.println("无此人!");
    else
    System.out.println(searchScore);
    }
    }class Student {
    int num;
    String name;
    int score; public void SetRecord(int _num, String _name, int _score) {
    num = _num;
    name = _name;
    score = _score;
    } public int GetRecord(int _num) {
    if (num == _num)
    return score;
    else
    return -1;
    }
    }错误原因请看代码注释!
      

  2.   


            for (i = 0; i < size; i++)
            {
             a[i] = new Student();
                a[i].SetRecord(Integer.parseInt(args[3 * i]), args[3 * i + 1], Integer.parseInt(args[3 * i + 2]));
            }
      

  3.   

    package test;
    public class UseStudent
    {
    static String[] arg={"1","abc","88","2","ww","99","2"};

        public static void main(String[] args)
        {
        
         Student[] students=new Student[args.length/3];
         for(int i=0;i<args.length/3;i++){
         students[i]=new Student(Integer.parseInt(args[3*i]),args[3*i+1],Integer.parseInt(args[3*i+2]));
         if(students[i].num==Integer.parseInt(args[args.length-1])){
         System.out.println("The student:"+students[i].num+"Score:"+students[i].score);
         System.out.println();
         }
         }
        }
        
    }class Student
    {
        int num;
        String name;
        int score;    public Student(int num,String name,int score){
         this.num=num;
         this.name=name;
         this.score=score;
        }
    }
      

  4.   

    package test;
    public class UseStudent
    {
    static String[] arg={"1","abc","88","2","ww","99","2"};

        public static void main(String[] args)
        {
        
         Student[] students=new Student[args.length/3];
         for(int i=0;i<args.length/3;i++){
         students[i]=new Student(Integer.parseInt(args[3*i]),args[3*i+1],Integer.parseInt(args[3*i+2]));
         if(students[i].num==Integer.parseInt(args[args.length-1])){
         System.out.println("The student:"+students[i].num+"Score:"+students[i].score);
         System.out.println();
         }
         }
        }
        
    }class Student
    {
        int num;
        String name;
        int score;    public Student(int num,String name,int score){
         this.num=num;
         this.name=name;
         this.score=score;
        }
    }
      

  5.   

    楼主用这写的程序去命令行运行
    Java UseStudent 1 ww 99 2 abc 88 3 wuhan 100 3
    运行结果为The student:3 Score:100
      

  6.   

    一度空间!数组中的每一个元素都是Student类的对象,要new的
    Student[] students=new Student[args.length/3];
        for(int i=0;i<args.length/3;i++){
                students[i]=new                               Student(Integer.parseInt(args[3*i]),args[3*i+1],Integer.parseInt(args[3*i+2]));
        if(students[i].num==Integer.parseInt(args[args.length-1])){
        System.out.println("The student:"+students[i].num+"Score:"+students[i].score);