java代码帮忙看看哪里错了

来源:百度知道 编辑:UC知道 时间:2024/06/30 06:20:21
import java.io.*;
class RandomTest
{
public static void main(String[] args) throws Exception
{
Student s1=new Student(1,"wangj",98.11);
Student s2=new Student(2,"wang",10.2);
Student s3=new Student(3,"lisi",13.3);
RandomAccessFile raf=new RandomAccessFile("student.txt","rw");
s1.writeStudent(raf);
s2.writeStudent(raf);
s3.writeStudent(raf);
/*s1.readStudent(raf);
s2.readStudent(raf);
s3.readStudent(raf);*/
raf.close();
}
}
class Student
{
int num;
String name;
double score;
Student(int num,String name,double score)
{
this.num=num;
this.name=this.name;
this.score=score;
}
public void writeStudent(RandomAccessFile raf) throws IOException
{
raf.writeInt(num);
raf.writeUTF(name);
raf.writeDouble(score);
}
public void readS

空指针错误,Student(int num,String name,double score)
{
this.num=num;
this.name=this.name; //这里错了,要改为:this.name=name;
this.score=score;
}
中name是空的,没有传进去没有找到

空指针
这里this.name=this.name;错了

要改成this.name = name

RandomAccessFile raf=new RandomAccessFile("student.txt","rw");
可能没有找到student.txt文件。

this.name=name,改成这样就对了

this.name=this.name;错了哦