Fairly new to programming, trouble reading and writing to a binary file. : <p>So I've got an assignment where I'm supposed to read a .csv file and then write it out in a seperate binary file. The csv file gives strangely formatted stats about baseball players. </p>

<p>Here is the code for the GUI where the program is actually run from:</p>

<pre><code>import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;

public class Homework3 extends JFrame
{
   private JButton jbChoose = new JButton("Choose File to Convert");
   private JTextArea jtaDisc = new JTextArea("Welcome to the .csv to binary file converter. Simply click the button below and choose the .csv file you would like to write to binary.", 5, 20);
   private File fobj = null;

   public static void main(String[] args)
   {
      new Homework3();
   }

   public Homework3()
   {
      this.setSize(400, 175);
      this.setTitle("File Converter");
      this.setLocationRelativeTo(null);
      this.setDefaultCloseOperation(EXIT_ON_CLOSE);
      this.setResizable(false);

      jtaDisc.setWrapStyleWord(true);
      jtaDisc.setLineWrap(true);
      jtaDisc.setEditable(false);

      JPanel jpSouth = new JPanel();
         jpSouth.add(jbChoose);
         jpSouth.setAlignmentX(jbChoose.CENTER_ALIGNMENT);
      this.add(jpSouth, BorderLayout.SOUTH);

      JPanel jpNorth = new JPanel();
         jpNorth.add(jtaDisc);
      this.add(jpNorth, BorderLayout.NORTH);

      Homework3Button hw3b = new Homework3Button(this);
         jbChoose.addActionListener(hw3b);

      setVisible(true);
   }
   public File getFobj()
   {
      return fobj;
   }

   public void setFobj(File _fobj)
   {
      fobj = _fobj;
   }
}
</code></pre>

<p>Here is the class where I actually try to do the reading and writing. I've got it set to print out the values of the csv file so I can see where it is going wrong. For some reason it's reading the first line and skipping the 2nd and 3rd instead of skipping the 1st and second like I want. It also doesn't get far enough to actually create the file in the write method.</p>

<pre><code>import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.filechooser.*;
import java.util.*;

public class Homework3Button implements ActionListener 
{
   private Homework3 window;
   private ArrayList&lt;Player&gt; data = new ArrayList&lt;Player&gt;();
   private int pos; 

   public Homework3Button(Homework3 _window)
   {
      this.window = _window;
   }

   public void actionPerformed(ActionEvent ae)
   {
      doChoose();
      doRead();
      doWrite();
   }

   public void doChoose()
   {
      JFileChooser chooser = new JFileChooser(new File("."));
      FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel Documents", "csv");
      chooser.setFileFilter(filter);

      int returnVal = chooser.showDialog(window, "Select");
      if(returnVal == JFileChooser.APPROVE_OPTION)
      {
         File fobj = chooser.getSelectedFile();
         window.setFobj(fobj);
      } 
   }

   public void doRead()
   {
      File fobj = window.getFobj();
      if(fobj == null)
      {
         JOptionPane.showMessageDialog(null, "There was a problem reading your file.", "Opening Error", JOptionPane.ERROR_MESSAGE);
         return;
      }
      BufferedReader br = null;
      try
      {
          br= new BufferedReader(new FileReader(fobj));
      }
      catch(IOException ioe)
      {
         JOptionPane.showMessageDialog(null, "Cannot open file: " + ioe.getMessage(), "Cannot Open", JOptionPane.ERROR_MESSAGE);
         return;
      }
      data = new ArrayList&lt;Player&gt;();
      try
      {
         String line = br.readLine() ;
         while(line != null)
         {
            System.out.println(line);
            for(int i = 0; i &lt;= 1; i++)
            {
                line = br.readLine();
            }
            Player pl = new Player(line);
            data.add(pl);
            line = br.readLine();
         }
      }
      catch(EOFException eofe)
      {
         System.out.println("Exception for ArrayList.");
      } 
      catch(IOException ioe)
      {
         JOptionPane.showMessageDialog(null, "Error reading file: " + ioe.getMessage(), "Bad Read", JOptionPane.ERROR_MESSAGE);
         return;
      }
      try
      {
          br.close();
      }
      catch(IOException ioe)
      {
         JOptionPane.showMessageDialog(null, "Cannot close file: " + ioe.getMessage(), "Cannot Close", JOptionPane.ERROR_MESSAGE);
         return;
      }
   }

   public void doWrite()
   {
      File fobj2 = new File("BaseballNames1.bin");

      if(fobj2 == null)
      {
         JOptionPane.showMessageDialog(null, "There was a problem writing to your file.", "Problem Writing", JOptionPane.ERROR_MESSAGE);
         return;
      }
      DataOutputStream dos = null;
      try
      {
         dos = new DataOutputStream(new FileOutputStream(fobj2));
      }
      catch(IOException ioe)
      {
         JOptionPane.showMessageDialog(null, "Cannot open file: " + ioe.getMessage(), "Cannot Open", JOptionPane.ERROR_MESSAGE);
         return;
      }
      System.out.println("Before For Loop");
      for(Player pl : data)
      {
         System.out.println("Inside For Loop");
         try
         {
            dos.writeUTF(pl.getName());
            dos.writeUTF(pl.getBirth());
            dos.writeInt(pl.getWeight());
            dos.writeUTF(pl.getHeight());
         }
         catch(IOException ioe)
         {
            JOptionPane.showMessageDialog(null, "Cannot write file: " + ioe.getMessage(), "Cannot Write", JOptionPane.ERROR_MESSAGE);
            return;
         }
         try 
         {
            dos.close();
         }
         catch(IOException ioe)
         {
            JOptionPane.showMessageDialog(null, "Cannot close file: " + ioe.getMessage(), "Cannot Close", JOptionPane.ERROR_MESSAGE);
            return;
         }
      }
   }    
}
</code></pre>

<p>Here is the class where I describe the a player object. When I try to run the program, after reading the .csv file I am met with a long list of errors. The top one is a null pointer exception at line 16 of this class. </p>

<pre><code>import java.util.*;
import java.io.*;

public class Player
{
   private String firstName;
   private String lastName;
   private int birthDay;
   private int birthMonth;
   private int birthYear;
   private int weight;
   private double height;

   public Player(String lineRead)
   {
      String[] fields = lineRead.split(",");
      firstName = fields[0];
      lastName = fields[1];
      birthDay = Integer.parseInt(fields[2]);
      birthMonth = Integer.parseInt(fields[3]);
      birthYear = Integer.parseInt(fields[4]);
      weight = Integer.parseInt(fields[5]);
      height = Double.parseDouble(fields[6]);  
   }

   public String getName()
   {
      return firstName + " " + lastName;
   }

   public String getBirth()
   {
      return Integer.toString(birthMonth) + "/" + Integer.toString(birthDay) + "/" + Integer.toString(birthYear);
   }

   public int getWeight()
   {
      return weight;
   }

   public String getHeight()
   {
      String weight2 = String.format("%.2f", weight);
      return weight2;
   }
}
</code></pre>

<p>Here is the .csv file format, each line starts with one space in front of it:</p>

<pre><code>namefirst , namelast  , birthday , birthmonth , birthyear , weight , height 

Starlin   , Castro    ,24,3,1990,190,72.8
Madison   , Bumgarner ,1,8,1989,215,76.0
Jason     , Heyward   ,9,8,1989,240,77.3
Ruben     , Tejada    ,27,10,1989,160,71.2
Jenrry    , Mejia     ,11,10,1989,160,72.5
Mike      , Stanton   ,8,11,1989,235,77.9
Dayan     , Viciedo   ,10,3,1989,240,71.1
Chris     , Sale      ,30,3,1989,170,77.2
Freddie   , Freeman   ,12,9,1989,225,77.7
Clayton   , Kershaw   ,19,3,1988,225,75.4
Travis    , Snider    ,2,2,1988,235,72.0
Elvis     , Andrus    ,26,8,1988,200,72.0
Trevor    , Cahill    ,1,3,1988,220,76.0
Rick      , Porcello  ,27,12,1988,200,77.0
Brett     , Anderson  ,1,2,1988,235,76.5
Fernando  , Martinez  ,10,10,1988,200,73.0
Jhoulys   , Chacin    ,7,1,1988,215,75.2
Chris     , Tillman   ,15,4,1988,200,77.8
Neftali   , Feliz     ,2,5,1988,215,75.5
Craig     , Kimbrel   ,28,5,1988,205,71.6
</code></pre>

<p>I know this is a lot but none of the questions I look at online pertain to my specific problem and the javadocs are really confusing to me. This is my first big thing with Java IO so I'd really appreciate some help here... I'm not sure if it's a problem with the way I've declared the array in the Player class or what... Any advice on how to fix this mess is greatly appreciated.</p>
