0

So I'm having an issue with my array returning with a bunch of nulls even though I have the if statement not allowing the SoftballPlayer obj if it != null....and my array is bounded by 24 so it works for the larger files but still returns nulls if the file input is less than my max of 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++;
         }         
      }



   }
  • Where is ```playerCount``` initialized - and which value? – sics Apr 21 '16 at 04:59
  • What does description of max of 24, or max of 30 have to do with error message of 5 being out-of-bounds? I see no description of what line is causing the error (show full stacktrace), and I see no code declaring and allocating any arrays, so how can we even attempt to help you? – Andreas Apr 21 '16 at 04:59

1 Answers1

0

I suspect that when you don't have enough entries in your file to fill your array, the other elements are remaining uninitialized. Referencing into them will cause a null error.

Consider using a list (http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html) instead - it will let you add any number of items, however large or small.

If you have to use an array, you need to store the number of elements you have put in it. Something like this:

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

And then numItems should have the number of items in the array. To loop through it:

for (int i = 0; i < numItems; i++) {
 //do something to each item in the array
}
nhouser9
  • 6,572
  • 3
  • 18
  • 39
  • This sounds like a comment, not an answer. – Andreas Apr 21 '16 at 04:59
  • we can't use ArrayList....we have to use arrays – JGrasp AllStars Apr 21 '16 at 05:00
  • @JGraspAllStars If you have to use arrays, keep a count of how many elements you have put in your array. Then when you loop through the array, loop until you have reached that count instead of until you reach the end of the array. – nhouser9 Apr 21 '16 at 05:02
  • @nhouser9 playerCount keeps the amount of elements but how would you go about doing that? Been trying to figure that out for the last 20min. – JGrasp AllStars Apr 21 '16 at 05:24
  • @JGraspAllStars please give us a description of the error given by the compiler/debugger. Also, more info on how playerCount, roster, and excludedRecords are being declared/initialized would help since the ArraysIndexOutOfBoundsException is most likely occurring in these arrays. Also, check your text files to see if the commas are the right places, otherwise, this could be causing the null's being printed out. – Chris Gong Apr 21 '16 at 06:08
  • @JGraspAllStars check my edit – nhouser9 Apr 21 '16 at 06:20