java抛出nulllpointerexception异常

代码如下importjava.io.*;publicclassRandomAccessFileDemo{ publicstaticvoidRandomWrite... 代码如下import java.io.*;public class RandomAccessFileDemo { public static void RandomWriteFile(File file){  Employee[] employees=new Employee[4];  employees[0]=new Employee("张三",20);  employees[0]=new Employee("李四",21);  employees[0]=new Employee("王五",22);  employees[0]=new Employee("牛六",23);  RandomAccessFile randomaccessfile=null;  try{   randomaccessfile=new RandomAccessFile(file,"rw");  }catch(FileNotFoundException e){   e.printStackTrace();}  try{   for(Employee e1:employees){    randomaccessfile.writeChars(e1.getName());    randomaccessfile.writeInt(e1.getage());   }   randomaccessfile.close();  }catch(IOException e){e.printStackTrace();  } } private static String readName(RandomAccessFile randomaccessfile)throws IOException{  char[]name=new char[8];  for(int i=0;i<name.length;i++)   name[i]=randomaccessfile.readChar();   return new String(name).replace('\0',' ');    } public static Employee[]RandomReadFile(File file)throws Exception{  RandomAccessFile randomaccessfile=new RandomAccessFile(file,"r");  Employee[]employee=new Employee[4];  int num=(int)randomaccessfile.length()/Employee.size();  for(int i=0;i<num;i++){   randomaccessfile.seek((i)*Employee.size());   employee[i]=new Employee(readName(randomaccessfile),randomaccessfile.readInt());  }  randomaccessfile.close();  return employee;   } public static void main(String[] args)throws Exception {  String filename=args[0]==null?"E:\newfile.txt":args[0];  File file=new File(filename);  RandomWriteFile(file);  Employee[]employee=RandomReadFile(file);  for(Employee e:employee){   System.out.println("name="+e.getName()+"\t|\tage="+e.getage());  } }}class Employee{String name;int age;final static int Len=8;public Employee(String name,int age){ if(name.length()>Len){  name=name.substring(0,8);}  else{  while(name.length()<Len)   name=name+"\u0000";  } this.name=name; this.age=age;}public String getName(){ return name;}public int getage(){ return age;}public void setName(String name){ this.name=name;}public void setage(int age){ this.age=age;}public static int size(){ return 2*8+4;}}在DOS环境下运行提示nullpointerexception,行数分别书16和46 展开
 我来答
lylegend13
2011-11-19 · TA获得超过4695个赞
知道大有可为答主
回答量:1701
采纳率:0%
帮助的人:1702万
展开全部
你好,有两处错误

1、
把employees[0]=new Employee("张三",20);
employees[0]=new Employee("李四",21);
employees[0]=new Employee("王五",22);
employees[0]=new Employee("牛六",23);
改为
employees[0] = new Employee("张三", 20);
employees[1] = new Employee("李四", 21);
employees[2] = new Employee("王五", 22);
employees[3] = new Employee("牛六", 23);

2、
把改为String filename=args[0]==null?"E:\newfile.txt":args[0];
改为String filename = !(args != null && args.length != 0 && args[0] != null) ? "e:\\newfile.txt" : args[0];
小白AI绘画
2011-11-19 · TA获得超过1358个赞
知道小有建树答主
回答量:303
采纳率:0%
帮助的人:464万
展开全部
employees[0] = new Employee("张三", 20);
employees[0] = new Employee("李四", 21);
employees[0] = new Employee("王五", 22);
employees[0] = new Employee("牛六", 23);
给数组赋值的时候,你只给第0位赋了4个值,后面3个当然是空指针了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
g634866293
2011-11-19
知道答主
回答量:37
采纳率:0%
帮助的人:23.9万
展开全部
employees[0] = new Employee("张三", 20);
employees[1] = new Employee("李四", 21);
employees[2] = new Employee("王五", 22);
employees[3] = new Employee("牛六", 23);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2011-11-19
展开全部
从代码异常处的对象开始往前推啊, 自己找找看,你的格式这么烂,谁能知道你哪行是哪行,,,流汗!!!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式