title: java.util.Scanner object throws NullPointerException while parsing text file
body: <p>I created this JAVA code for <a href="https://code.google.com/codejam/contest/544101/dashboard#s=p0" rel="nofollow">this Google Code Jam question</a>. 
  When i execute the code and select <a href="https://code.google.com/codejam/contest/544101/dashboard/do/A-large-practice.in?cmd=GetInputFile&amp;problem=567116&amp;input_id=1&amp;filename=A-large-practice.in&amp;redownload_last=1&amp;agent=website&amp;csrfmiddlewaretoken=NGRhMjVlNjE3N2IwODc4Yzk5YTBkYzNmODYzNTVlYjV8fDE0MjIyNzc3MjE1NDQ5Njk%3D" rel="nofollow">this text file</a>, it throws NullPointerException. Please help.  I guess it is because nextLine() positions the scanner to the nextLine and returns the rest of the current line which is empty.</p>
  
  <pre><code>    line = ab.nextLine();
  </code></pre>
  
  <p>How do I fix this error so that it reads the next line of text?  Provide code along with your suggestions. I will be grateful to you for the rest of my life :)</p>
  
  <pre><code>            /* JEditorPaneFileChooser.java
       * Copyright (c) 2014, HerongYang.com, All Rights Reserved.
       */
      import java.io.*;
      import java.nio.*;
      import java.nio.charset.*;
      import java.awt.event.*;
      import javax.swing.*;
      import javax.swing.filechooser.*;
      import java.util.Scanner;
      public class JEditorPaneFileChooser implements ActionListener {
          JFrame myFrame = null;
          JEditorPane myPane = null;
          JScrollPane mySPane = null;
          JMenuItem cmdOpen = null;
          JMenuItem cmdSave = null;
          String dirName = "\\herong\\swing\\";
          String fileName = "";
  
          public static void main(String[] a) {
              (new JEditorPaneFileChooser()).test();
          }
  
          private void test() {
              myFrame = new JFrame("JEditorPane JFileChooser Test");
              myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              myFrame.setSize(300,400);
  
              myPane = new JEditorPane();
              myPane.setContentType("text/plain");
              myPane.setText(
                  "CLICK ON \'File\' AND SELECT \'Open\'\nTHEN SELECT \'A-large-practice.in\'");
              mySPane = new JScrollPane(myPane);
              myFrame.setContentPane(mySPane);
  
              JMenuBar myBar = new JMenuBar();
              JMenu myMenu = getFileMenu();
              myBar.add(myMenu); 
              myFrame.setJMenuBar(myBar);
  
              myFrame.setVisible(true);
          }
  
          private JMenu getFileMenu() {
              JMenu myMenu = new JMenu("File");
              cmdOpen = new JMenuItem("Open");
              cmdOpen.addActionListener(this);
              myMenu.add(cmdOpen);
  
              return myMenu;
          }
  
          public void actionPerformed(ActionEvent e) {
              JFileChooser chooser = new JFileChooser();
              chooser.setCurrentDirectory(new File(dirName));
  
              chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  
              FileNameExtensionFilter filter = new FileNameExtensionFilter(
                      ".in file", "in");
              chooser.setFileFilter(filter);
  
              Object cmd = e.getSource();
              try {
                  if (cmd == cmdOpen) {
                      int code = chooser.showOpenDialog(myPane);
                      if (code == JFileChooser.APPROVE_OPTION) {
                          File selectedFile = chooser.getSelectedFile();
                          long ag= selectedFile.length();
                          fileName = selectedFile.getName();
                          FileInputStream fis = 
                              new FileInputStream(selectedFile);
                          InputStreamReader in=
                              new InputStreamReader(fis, Charset.forName("UTF-8")); 
                          char[] buffer=new char[(int)ag];
                          int n = in.read(buffer);
                          String text = new String(buffer, 0, n);
                          String ans =bag(text);
  
                          myPane.setText(ans);
                          in.close();
                      }
                  } 
              } catch (Exception f) {
                  f.printStackTrace();
              }
  
          }
  
          public static String bag(String input){
  
              Scanner ab=new Scanner(input);
              int case1,lp=0;
              case1 = ab.nextInt();
              int total, k;
              char[][] chari,m=null,n=null;
              int R1[][]=null,B1[][]=null;
              String line="",line1="",ghost="",answ="";
              int axr=0,minr=0,maxr=0,axrb=0,minrb=0,maxrb=0,xr=0,lr=0,nex=0,xrb=0,lrb=0,nexb=0,yr=0,lr1=0,lr2=0,lr3=0,yrb=0,lr1b=0,lr2b=0,lr3b=0,man=0,min=0,
              manb=0,minb=0,rax=0,ray=0,cra=0,raxb=0,rayb=0,crab=0;
  
              int t,i,j,x,p,q,p1,g,h,y,w,sumit,sum,RX=0,BX=0;
              for (i=1;i&lt;=case1;i++)
              {
                  total = ab.nextInt();
                  k=ab.nextInt();
                  chari=new char[total][total];
  
                  for(x=0;x&lt;total;x++)
                  {                             
                      line = ab.nextLine();
                      for (y=0;y&lt;total;y++)
                      { chari[x][y] =line.charAt(y);}        
  
                  }
  
  
  
                  for (g=0;g&lt;total;g++)
                  {
  
                      w=0;
  
                      for(h=total-1;h&gt;=0;h--)
                      {
                          m[g][w]=chari[h][g];
                          w++;
                      }
                  }
  
                  for (q=0;q&lt;total;q++)
                  {
                      p1=total-1;
                      for (p=total-1;p&gt;=0;p--)
                      {
                          if( (m[p][q]=='R')  ||  (m[p][q]=='B'))
                          {
                              n[p1][q]=m[p][q];
                              p--;}
                      }
                  }
  
                  for (sumit=total-1;sumit&gt;=0;sumit--)
                  {
  
                      for (sum=0;sum&lt;total;sum++)
                      {
  
                          if (n[sumit][sum] == 'R')
                          {
                              R1[RX][0]=sumit;
                              R1[RX][3]=sum;
                              RX++;
                          }
  
                          else if (n[sumit][sum]=='B')
                          {    B1[BX][0] = sumit;
                              B1[BX][4]= sum;
                              BX++;
                          }
                      }
                  }
  
                  axr=R1.length;
  
                  for (minr=0;minr&lt;axr;minr++)
  
                  {
                      xr = R1 [minr][0];
  
                      for (maxr=minr+1;maxr&lt;axr;maxr++)
  
                      {
  
                          if (R1 [maxr][0] == xr)
                              lr++;}
  
                      if (lr&gt;=k) nex=1;
                      if (lr&lt;=k) break;
                      else lr=0;
                  }
  
                  if (nex!=1)
                  {
                      for (minr=0;minr&lt;axr;minr++)
                      {
  
                          yr=R1[minr][5];
  
                          for (maxr=minr+1;maxr&lt;axr;maxr++)
                          {
                              if (R1[maxr][6]==yr)
                                  lr1++;}
  
                          if (lr1&gt;=k)nex=1;
                          if (lr1&gt;=k) break;
                          else lr1=0;
                      }
  
                      if (nex!=1)
                      {
  
                          for (man=0;man&lt;axr;man++)
  
                          {
  
                              rax=R1[man][0];
                              ray = R1 [man][7];
                              cra=1;
                              for (min=man+1;min&lt;axr;min++)
  
                              {
  
                                  if (    (R1[min][0]==rax-cra)   &amp;&amp;   (R1[min][8] == ray +cra)    )
  
                                  {lr2++;
                                      cra++;}
  
                                  else break;
  
                              }
                              cra=1;
                              if (lr2&gt;=k) nex=1;
                              if (lr2&gt;=k) break;
                              else lr2=0;
                          }
  
                          if (nex!=1)
                          {
                              for (man=0;man&lt;axr;man++)
  
                              {
  
                                  rax=R1[man][0];
                                  ray = R1 [man][9];
                                  cra=1;
                                  for (min=man+1;min&lt;axr;min++)
  
                                  {
  
                                      if (    (R1[min][0]==rax-cra)   &amp;&amp;   (R1[min][10] == ray -cra)    )
  
                                      {lr3++;
                                          cra++;}
  
                                      else break;
  
                                  }
                                  cra=1;
                                  if (lr3&gt;=k) nex=1;
                                  if (lr3&gt;=k) break;
                                  else lr3=0;
                              }
                          }
                      }
                  }
  
                  axrb=B1.length;
                  for (minrb=0;minrb&lt;axrb;minrb++)
                  {
                      xrb = B1 [minrb][0];
  
                      for (maxrb=minrb+1;maxrb&lt;axrb;maxrb++)
  
                      {
  
                          if (B1 [maxrb][0] == xrb)
                              lr++;}
  
                      if (lrb&gt;=k) nexb=1;
                      if (lrb&lt;=k) break;
                      else lr=0;
                  }
  
                  if (nexb!=1)
                  {
                      for (minrb=0;minrb&lt;axrb;minrb++)
                      {
  
                          yr=B1[minrb][11];
  
                          for (maxrb=minrb+1;maxrb&lt;axrb;maxrb++)
                          {
                              if (B1[maxrb][12]==yrb)
                                  lr1b++;}
  
                          if (lr1b&gt;=k)nexb=1;
                          if (lr1b&gt;=k) break;
                          else lr1b=0;
                      }
  
                      if (nexb!=1)
                      {
  
                          for (manb=0;manb&lt;axrb;manb++)
  
                          {
  
                              raxb=B1[manb][0];
                              rayb = B1 [manb][13];
                              crab=1;
                              for (minb=manb+1;minb&lt;axrb;minb++)
  
                              {
  
                                  if (    (B1[minb][0]==raxb-crab)   &amp;&amp;   (B1[minb][14] == rayb +crab)    )
  
                                  {lr2b++;
                                      crab++;}
  
                                  else break;
  
                              }
                              crab=1;
                              if (lr2b&gt;=k) nexb=1;
                              if (lr2b&gt;=k) break;
                              else lr2b=0;
                          }
  
                          if (nexb!=1)
                          {
                              for (manb=0;manb&lt;axrb;manb++)
  
                              {
  
                                  raxb=B1[manb][0];
                                  rayb = B1 [manb][15];
                                  crab=1;
                                  for (minb=manb+1;minb&lt;axrb;minb++)
  
                                  {
  
                                      if (    (B1[minb][0]==raxb-crab)   &amp;&amp;   (B1[minb][16] == rayb -crab)    )
  
                                      {lr3b++;
                                          crab++;}
  
                                      else break;
  
                                  }
                                  crab=1;
                                  if (lr3b&gt;=k) nexb=1;
                                  if (lr3b&gt;=k) break;
                                  else lr3b=0;
                              }
                          }
                      }
                  }
  
                  if (nex==1)
                  {ghost=(nexb==1)?"Both": "Red";}
                  else
                  {ghost =(nexb==1)? "Blue":"Neither";}
  
                  answ= "case1 #" + i+ ": " +ghost+"\n";
  
              }
  
  
              return answ;
          }
  
      }
  </code></pre>
  
  <p>stacktrace --</p>
  
  <pre><code>java.lang.NullPointerException
  at JEditorPaneFileChooser.bag(JEditorPaneFileChooser.java:112)
  at JEditorPaneFileChooser.actionPerformed(JEditorPaneFileChooser.java:78)
  </code></pre>