public void readfile()
     {
         
         String[] record = new String[50];
         String str_relation=new String();
         int i=0;         try
         {
             FileReader read=new FileReader("Family Info.txt");
             Scanner scanner=new Scanner(read);
             while(scanner.hasNextLine())
             {
                 i++;
                 record[i]=scanner.nextLine();
                 System.out.println(record[i]);
             }
             System.out.println(i);
             for(int a=0;a<i;a++)
             {
                 if(record[a].equals("Relation:Key Person"))
                 {
                     System.out.println("ok");
                 }
                 else
                 {
                     System.out.println("not ok");
                 }             }                       read.close();
             }catch(Exception e){
                 System.out.println(e);         }     }
 if(record[a].equals("Relation:Key Person"))出现java.lang.NullPointerException的问题
把a换成数字就没问题了,怎样解决?

解决方案 »

  1.   

    数组里面有null呗。没别的可能了。
      

  2.   

    debug,看当a运行到几的时候抛异常
      

  3.   

    LZ,你i初始化是0,然后while马上就++,那么我问,数组下标为0的不是null吗??
      

  4.   

    可能是数组里有空指针你把
    while(scanner.hasNextLine())
                 {
                     i++;
                     record[i]=scanner.nextLine();
                     System.out.println(record[i]);
                 }改成
    while(scanner.hasNextLine())
                 {
                   record[i]=scanner.nextLine();
                   i++;                 System.out.println(record[i]);
                 }再试试
      

  5.   

    record[0]是空的把。。楼主你的数据是从1开始的