下面这个程序我在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;
}
}
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;
}
}
解决方案 »
- android 开发wenservice问题(java.rmi.RemoteException cannot be resolved)
- java矩阵问题
- 问下JDK1.4版的用什么语法输入呀?
- java牺牲速度来实现垮平台是好还是坏,该不该改进?
- jmf如何保存摄像头数据为视频文件
- File是如何访问硬盘的
- ICTCLAS 中文分词系统修改版&Java版接口
- 迷茫
- 小弟总结的array,vertor,arraylist,hashable,hashmap等几个易混淆的概念,请大家来指正,补充
- java AES将加密之后的密文随机仿照一个解密会报Given final block not properly padded
- 怎么运行用记事本编写的java程序?
- 如何用正则表达式验证是否是视频文件(目前大部分格式)
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;
}
}错误原因请看代码注释!
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]));
}
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;
}
}
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;
}
}
Java UseStudent 1 ww 99 2 abc 88 3 wuhan 100 3
运行结果为The student:3 Score:100
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);