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
展开
4个回答
展开全部
你好,有两处错误
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];
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];
展开全部
employees[0] = new Employee("张三", 20);
employees[0] = new Employee("李四", 21);
employees[0] = new Employee("王五", 22);
employees[0] = new Employee("牛六", 23);
给数组赋值的时候,你只给第0位赋了4个值,后面3个当然是空指针了
employees[0] = new Employee("李四", 21);
employees[0] = new Employee("王五", 22);
employees[0] = new Employee("牛六", 23);
给数组赋值的时候,你只给第0位赋了4个值,后面3个当然是空指针了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
employees[0] = new Employee("张三", 20);
employees[1] = new Employee("李四", 21);
employees[2] = new Employee("王五", 22);
employees[3] = new Employee("牛六", 23);
employees[1] = new Employee("李四", 21);
employees[2] = new Employee("王五", 22);
employees[3] = new Employee("牛六", 23);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-11-19
展开全部
从代码异常处的对象开始往前推啊, 自己找找看,你的格式这么烂,谁能知道你哪行是哪行,,,流汗!!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询