返回Nulls:获取java.lang.ArrayIndexOutOfBoundsException:5

时间:2016-04-21 04:54:44

标签: java arrays null

所以我有一个问题,我的数组返回了一堆空值,即使我有if语句不允许SoftballPlayer obj,如果它!= null ....并且我的数组以24为界它适用于较大的文件,但如果文件输入小于我的最大值24,仍会返回空值? [MAX_PLAYERS = 24 MAX_EXCLUDED = 30]

public int getPlayerCount() {
      return playerCount;
   }

   public void setPlayerCount(int playerCountIn) {
      playerCount = playerCountIn;
  }

public void playerReadFile(String fileNameIn) throws IOException {
  Scanner scanFile = new Scanner(new File(fileNameIn));
  teamName = "";
  String number = "";
  String name = "";
  String position = "";
  double specializationFactor = 0.0;
  double battingAvg = 0.0;
  double outfielderFieldingAvg = 0.0;
  double infielderFieldingAvg = 0.0;
  int wins = 0;
  int losses = 0;
  double era = 0.0;
  int saves = 0;

  teamName = scanFile.nextLine();      
  while (scanFile.hasNext()) {
     String playerLine = scanFile.nextLine();
     Scanner scan = new Scanner(playerLine);
     scan.useDelimiter(",");
     char i = scan.next().charAt(0);
     number = scan.next();
     name = scan.next();
     position = scan.next();
     specializationFactor = Double.parseDouble(scan.next());
     battingAvg = Double.parseDouble(scan.next());

     if (playerCount < MAX_PLAYERS) {
        switch (i) {

           case 'O': 
              outfielderFieldingAvg = Double.parseDouble(scan.next());
              Outfielder out = new Outfielder(number, name, position,
                 specializationFactor, battingAvg, outfielderFieldingAvg);
              if (out != null) {
                 roster[playerCount] = out;
                 playerCount++;
              }
              break;

           case 'I':
              infielderFieldingAvg = Double.parseDouble(scan.next());
              Infielder inf = new Infielder(number, name, position,
                 specializationFactor, battingAvg, infielderFieldingAvg);
              if (inf != null) {
                 roster[playerCount] = inf;
                 playerCount++;
              }
              break;

           case 'P':
              wins = Integer.parseInt(scan.next());
              losses = Integer.parseInt(scan.next());
              era = Double.parseDouble(scan.next());
              Pitcher pit = new Pitcher(number, name, position,
                 specializationFactor, battingAvg, wins,
                 losses, era);
              if (pit != null) {
                 roster[playerCount] = pit;
                 playerCount++;
              }
              break;

           case 'R':
              wins = Integer.parseInt(scan.next());
              losses = Integer.parseInt(scan.next());
              era = Double.parseDouble(scan.next());
                  saves = Integer.parseInt(scan.next());
                  ReliefPitcher rpit = new ReliefPitcher(number, name, position,
                    specializationFactor, battingAvg, wins,
                     losses, era, saves);
                  if (rpit != null) {
                     roster[playerCount] = rpit;
                     playerCount++;
                  }
                  break;

               default:
                  if (excludedCount < MAX_EXCLUDED) {
                     excludedRecords[excludedCount] = playerLine;
                     excludedCount++;
                  }
                  break;
            }
         }
         else if (excludedCount < MAX_EXCLUDED) {
            excludedRecords[excludedCount] = playerLine;
            excludedCount++;
         }         
      }



   }

1 个答案:

答案 0 :(得分:0)

我怀疑当你的文件中没有足够的条目来填充你的数组时,其他元素仍未被初始化。引用它们将导致空错误。

请考虑使用列表(http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html) - 它可以让您添加任意数量的项目,无论大小,都可以。

如果必须使用数组,则需要存储放入其中的元素数。像这样:

int numItems = 0;
while (scanFile.hasNext()) {
 numItems++;
 //use your existing code to insert items into the array
}

然后numItems应该具有数组中的项目数。循环遍历:

for (int i = 0; i < numItems; i++) {
 //do something to each item in the array
}