title: ArrayList becomes null while writing to file
body: <p>My arraylist&lt;"obj"> becomes null after trying to write to a file.</p>
  
  <p>In WriteToFile class arraylist info becomes null after executing the last line </p>
  
  <pre><code>    writer.write(info.get(i).getIpadd().toString()+"\n");
  </code></pre>
  
  <p>It works on the first instance when i am writing another list to file but does not when i run it the 2nd instance. I dun understand why its happening. Below is the whole code and the stack trace.</p>
  
  <p>WriteToFile Class:</p>
  
  <pre><code>public class WriteToFile {
  
  public WriteToFile(ArrayList&lt;Information&gt; info,String location) 
  {
      FileWriter writer=null;
      try
      {
          writer = new FileWriter(location); 
          System.out.println(info.size());
          for(int i=0;i&lt;info.size()-1;i++) 
          {
            writer.write(info.get(i).getDate().toString()+",");
            writer.write(info.get(i).getAccount().toString()+",");
            writer.write(info.get(i).getStatus().toString()+",");
            writer.write(info.get(i).getIpadd().toString()+"\n");
            System.out.println(info.get(i).getAccount());
          }
      }
      catch(Exception e)
      {
          e.printStackTrace();
          System.out.println(e.getMessage());
      }
      finally
      {
          try {
              writer.close();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  }
  
  }
  </code></pre>
  
  <p>StackTrace:</p>
  
  <pre><code>    java.lang.NullPointerException
  at WriteToFile.&lt;init&gt;(WriteToFile.java:17)
  at Gen_Report.&lt;init&gt;(Gen_Report.java:45)
  at Gen_Report.main(Gen_Report.java:57)
  </code></pre>
  
  <p>main:</p>
  
  <pre><code>public class Gen_Report {
  
  public Gen_Report() 
  {
      // TODO Auto-generated constructor stub
      //locate file and read all text from .log file to .csv
          //file is found.so read text from it and extract all date/time, email add ,accepts and rejects,ip add, delete
      Date date=new Date();
      String[] dateTokens=date.toString().split(" ");
      String dateString=dateTokens[2]+dateTokens[1]+dateTokens[5]+"_"+dateTokens[3].substring(0, 2)+dateTokens[3].substring(3,5)+dateTokens[3].substring(6, 8);
  
      String logFileLocation = "/Users/gundu_87/Documents/workspace/GenFLRReport/";
  
      ReaderFromLog rfl = new ReaderFromLog(logFileLocation+"radsecproxy.log");
  
      //include duplicates
      WriteToFile wtf = new WriteToFile(rfl.log,logFileLocation+dateString+"_FLRlogduplicates.txt");
  
      //exclude duplicates
      RemoveDuplicatesInList rdil = new RemoveDuplicatesInList(logFileLocation+dateString+"_FLRlogduplicates.txt");
      for(int i=0;i&lt;rdil.log.size();i++)
      {
          System.out.println(rdil.log.get(i).getAccount());
      }
      wtf = new WriteToFile(rdil.log,logFileLocation+dateString+"_FLRlog.txt");
  
  }
  public static void main(String[] args) {
      // TODO Auto-generated method stub
  
      Gen_Report gr= new Gen_Report();
  }
  
  }
  </code></pre>
  
  <p>Information class:</p>
  
  <pre><code>public class Information {
  
  private String ipadd;
  private String status;
  private String account;
  private String date;
  
  
  public String getIpadd() {
      return ipadd;
  }
  
  public void setIpadd(String ipadd) {
      this.ipadd = ipadd;
  }
  
  public String getStatus() {
      return status;
  }
  
  public void setStatus(String status) {
      this.status = status;
  }
  
  public String getAccount() {
      return account;
  }
  
  public void setAccount(String account) {
      this.account = account;
  }
  
  public String getDate() {
      return date;
  }
  
  public void setDate(String date) {
      this.date = date;
  }
  }
  </code></pre>
  
  <p>ReaderFromLog class</p>
  
  <pre><code>public class ReaderFromLog {
  
  
  Scanner s1 = null;
  String line=null;
  ArrayList&lt;Information&gt; log;
  
  public ReaderFromLog(String logFileLocation) {
      // TODO Auto-generated constructor stub
      File logFile = new File(logFileLocation);
  
      if(!logFile.exists())
      {
          System.err.println("File not found");
          System.exit(1);
      }
      else
      {
          try 
          {
              s1 = new Scanner(new FileReader(logFile));
          } catch (FileNotFoundException e) 
          {
              System.err.print("File not found");
          }
      }
  
      log=new ArrayList&lt;Information&gt;();
      //store into a array
      //exclude any repeats
  
  
      do{
          line=s1.nextLine();
          Information newUser = new Information();
          if(line.contains("Access-Accept for user"))
          {
              newUser.setStatus("Accept");
              String[] sb=line.split(" ");
              newUser.setAccount(sb[7]);
              int idx_Ipadd = 0;
              for(int i=0;i&lt;sb.length;i++)
                  if (sb[i].contentEquals("to"))
                      idx_Ipadd=i;
              newUser.setIpadd(sb[idx_Ipadd+1]+ " " + sb[idx_Ipadd+2]);
              newUser.setDate(sb[0]+ " "+sb[1] + " " +sb[2]+" " + sb[3].substring(0, 4));
              log.add(newUser);
          }
          else if(line.contains("Access-Reject for user"))
          {
              newUser.setStatus("Reject");
              String[] sb=line.split(" ");
              newUser.setAccount(sb[7]);
              int idx_Ipadd = 0;
              for(int i=0;i&lt;sb.length;i++)
                  if (sb[i].contentEquals("to"))
                      idx_Ipadd=i;
              newUser.setIpadd(sb[idx_Ipadd+1]+ " " + sb[idx_Ipadd+2]);
              newUser.setDate(sb[0]+ " "+sb[1] + " " +sb[2]+" " + sb[3].substring(0, 4));
              log.add(newUser);
          }
      }while(s1.hasNextLine());
  }
  
  }
  </code></pre>
  
  <p>RemoveDuplicate class:</p>
  
  <pre><code>public class RemoveDuplicatesInList {
  
  Scanner s1 = null;
  String line=null;
  ArrayList&lt;Information&gt; log;
  
  public RemoveDuplicatesInList(String duplicateFileLocation) 
  {
      // TODO Auto-generated constructor stub
      File logFile = new File(duplicateFileLocation);
  
      if(!logFile.exists())
      {
          System.err.println("File not found");
          System.exit(1);
      }
      else
      {
          try 
          {
              s1 = new Scanner(new FileReader(logFile));
          } catch (FileNotFoundException e) 
          {
              System.err.print("File not found");
          }
      }
  
      log=new ArrayList&lt;Information&gt;();
      //store into a array
      //exclude any repeats
      do{
          boolean sameAccount=false;
          line=s1.nextLine();
          Information newUser = new Information();
          if(line.contains("Accept"))
          {
              newUser.setStatus("Accept");
              String[] sb=line.split(",");
              sameAccount=false;
              for(int i=0;i&lt;log.size();i++)
                  if(log.get(i).getAccount().contentEquals(sb[1]))
                  {
                      sameAccount=true;
                      break;
                  }
              if(!sameAccount)
              {
                  newUser.setAccount(sb[1]);
                  newUser.setIpadd(sb[3]);
                  newUser.setDate(sb[0]);
                  log.add(newUser);
              }
  
          }
          else if(line.contains("Reject"))
          {
              newUser.setStatus("Reject");
              String[] sb=line.split(",");
              for(int i=0;i&lt;log.size();i++)
                  if(log.get(i).getAccount().contentEquals(sb[1]))
                  {
                      sameAccount=true;
                      break;
                  }
              if(!sameAccount)
              {
                  newUser.setAccount(sb[1]);
                  newUser.setIpadd(sb[3]);
                  newUser.setDate(sb[0]);
                  log.add(newUser);
              }
          }
      }while(s1.hasNextLine());
  }
  
  }
  </code></pre>