title: How to fill a 2d array, given the values
body: <p>I am trying to input the values inside of one class into the 2d array declared into another class.</p>
  
  <p>I keep getting a null pointer exception when I run it, so I feel the way I am trying is not working well.</p>
  
  <p>The way it's supposed to work is in the main the nested loop is supposed to supply the values to the other method which is then supposed to input them into the 2d array.</p>
  
  <p>Thanks</p>
  
  <p>Here is main</p>
  
  <pre><code>public class Lab1 {
  
  
  static final int NBPLAYERS = 11;
  static final int NBMONTHS = 6;
  
  public static void main(String[] args) {
  
      String[] month = {"April", "May","June", "July", "August","September"};
      String[] players = {"Colabello","Donaldson","Smoak","Martin","Goins","Encarnacion","Carrera","Tulowitzki","Pillar","Bautista","Travis"};
      double[][] battingAvg = {
                      {0,.368,.300,.224,.386,.268},
              {.319,.306,.269,.287,.324,.296},
              {.229,.310,.213,.191,.203,.262},
              {.197,.327,.239,.256,.138,.213},
              {.276,.236,.172,.240,.314,.279},
              {.205,.225,.303,.241,.407,.279},
              {0,.302,.282,.244,.333,.231},
              {0,0,0,.357,.214,.237},
              {.273,.181,365,.283,.240,.323},
              {.164,.295,.226,.219,.286,.293},
              {.325,.189,.313,.368,0,0}};
  
      double [][] onBase = {
                      {.417,.330,.286,.413,.362,.429},        
              {.370,.373,.322,.370,.408,.403},
              {.372,.333,.275,.283,.243,.324},
              {.367,.362,.329,.322,.263,.300},
              {.323,.278,.221,.286,.442,.347},
              {.258,.333,.382,.384,.460,.411},
              {0,.357,.333,.277,.333,.313},
              {0,0,0,.400,.325,.250},
              {.297,.237,.380,.323,.283,.363},
              {.325,.418,.388,.300,.370,.436},
              {.393,.246,.313,.421,0,0}};
  
      PlayerStats player;
  
      BlueJays team = new BlueJays(NBPLAYERS, NBMONTHS);
  
      for(int iPlayer=0; iPlayer&lt;NBPLAYERS; iPlayer++) {
          for(int iMonth=0; iMonth&lt;NBMONTHS; iMonth++) {
  
               player = new PlayerStats(players[iPlayer], iMonth, battingAvg[iPlayer][iMonth],onBase[iPlayer][iMonth]);
  
              team.setPlayerStatsCell(player,iPlayer,iMonth);
          }
      }
  
      team.getHighestSingleMonthBattingAvg();
  
      team.getHighestOnBase(5);
  
      team.getLowestBattingAvg(5);
  
      team.getBestMonth("Bautista");
  
      team.getBestOverallRecord();
  
      team.getLowestOnBase();
  </code></pre>
  
  <p>Here is the method I am trying to put it into, I'll write the whole class though.</p>
  
  <pre><code>PlayerStats setPlayerStatsCell(PlayerStats p, int nbPlayers, int nmMonths) {
  
      for(int i=0; i&lt;nbPlayers; i++) {
          for(int j=0; j&lt;nmMonths; j++) {
  
              stats[i][j]= p;
          }
      }
          return p;
  
  }
  </code></pre>
  
  <p>Below is the whole class.</p>
  
  <pre><code>class BlueJays {
  
  int nbPlayers;
  int nbMonths;
  int j = 0;
  int highestBattingAvg;
  int highestBattingMonth;
  String highestBattingPlayer;
  int highestOnBase;
  int lowestAvg;
  String lowestAvgPlayer;
  int highestOverall;
  String highestOverallPlayer;
  int lowestOnBase;
  int lowestOnBaseMonth;
  String highestOnBasePlayer;
  double bestOverAllMonth;
  
  PlayerStats[][] stats;
  
  public BlueJays(int nbplayers2, int nbmonths2) {
      this.nbPlayers = nbplayers2;
      this.nbMonths = nbmonths2;
  
      this.stats = new PlayerStats[nbPlayers][nbMonths];
  
  }
  
  
  
  PlayerStats setPlayerStatsCell(PlayerStats p, int nbPlayers, int nmMonths) {
  
      for(int i=0; i&lt;nbPlayers; i++) {
          for(int j=0; j&lt;nmMonths; j++) {
  
              stats[i][j]= p;
          }
      }
          return p;
  
  }
  
  
   PlayerStats getHighestSingleMonthBattingAvg() {
  
  
  
      for(int i = 0; i &lt; nbPlayers; i++)   {
          for(int j = 0; j&lt;nbMonths; j++) {
  
              stats[i][j].getAvg();
  
                  if(highestBattingAvg &lt; stats[i][j].getAvg()) {
                      highestBattingMonth = stats[i][j].getMonth();
                      highestBattingPlayer = stats[i][j].getName();
  
                  }
  
                  System.out.println("Highest average batting player for the month " + highestBattingMonth + " is " + highestBattingPlayer);
  
              }
          }
  
          return null;
  
  
      }
  
   PlayerStats getHighestOnBase(int month) {
  
      while(j &lt; nbMonths) {
  
          for(int i = 0; i &lt; nbPlayers; i++)  {
              stats[i][month].getOnBase();
  
              if(highestOnBase &lt; stats[i][month].getOnBase()) {
  
              highestOnBasePlayer =  stats[i][month].getName();
          }
  
          if (i == nbMonths) {
              j++;
              i = 0;
  
          }
          System.out.println("Highest average onBase player for the month " + month + highestOnBasePlayer);
  
      }
  }
  
  
  return null;
  
  }
  
  
  public PlayerStats getLowestBattingAvg(int month) {
  
      while(j &lt; nbMonths) {
  
          for(int i = 0; i &lt; nbPlayers; i++)  {
              stats[i][month].getOnBase();
  
          if(lowestAvg &gt; stats[i][month].getAvg()) {
  
              lowestAvgPlayer =  stats[i][month].getName();
          }
  
          if (i == nbMonths) {
              j++;
              i = 0;
  
          }
  
  
  
      }
      System.out.println("Lowest average batting player for the month " + month + " is " + lowestAvgPlayer);
  }
  return null;
  
  }
  
     PlayerStats getBestMonth(String player) {
  
    while(j &lt; nbMonths) {
  
      for(int i = 0; i &lt; nbPlayers; i++)  {
          stats[i][j].getBoth();
  
          if(bestOverAllMonth &gt; stats[i][j].getAvg() &amp;&amp; stats[i][j].getName().contains(player)) {
  
  
               bestOverAllMonth =  stats[i][j].getBoth();
  
  
          }
  
          if (i == nbMonths) {
              j++;
              i = 0;
  
          }
  
  
  
      }
      System.out.println("Best month for the player " + player + " is " + bestOverAllMonth);
  }
  return null;
  
  
  
  
  
  
    }
  
    public String getBestOverallRecord() {
  
     while(j &lt; nbMonths) {
  
         for(int i = 0; i &lt; nbPlayers; i++)  {
             stats[i][j].getBoth();
  
          if(highestOverall &lt; stats[i][j].getBoth()) {
  
              highestOverallPlayer =  stats[i][j].getName();
          }
  
          if (i == nbMonths) {
              j++;
              i = 0;
  
          }
  
  
  
      }
      System.out.println("Highest overall record is " + highestOverallPlayer);
      }
     return null;
  
  
  
  }
  
  
  
     public PlayerStats getLowestOnBase() {
  
     while(j &lt; nbMonths) {
  
         for(int i = 0; i &lt; nbPlayers; i++)  {
          stats[i][j].getOnBase();
  
          if(lowestOnBase &gt; stats[i][j].getOnBase()) {
  
              double lowestOnBase =  stats[i][j].getOnBase();
  
              if(lowestOnBase &gt; 0) {
  
                  lowestAvgPlayer = stats[i][j].getName();
  
              } else {
  
                  i++;
              }
          if (i == nbMonths) {
              j++;
              i = 0;
  
          } 
  
      }
  
  
  
  }
      System.out.println("Lowest On Base is  " + lowestOnBase);
  
  
  
     }
  return null;
  
   }
  }
  </code></pre>
  
  <p>EDIT: The exception</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
  at BlueJays.getHighestSingleMonthBattingAvg(BlueJays.java:52)
  at Lab1.main(JonathanRoy_Lab1.java:52)
  </code></pre>