Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

-1

I need to open a file txt and store its information in ArrayList and then make a comparison between what was typed and what has in bad ArrayList to see if the login is right or not. Here is my code:

public void actionPerformed(ActionEvent evento)
{
  if( evento.getSource( ) == bENTRAR )
  {
     openFile();
     //Gravando records = new Gravando();
     int i = 0;
     System.out.println(grava.getListaMateria().size());
     while( i <= grava.getListaMateria().size())
     {
        System.out.println(grava.getListaMateria().get(2).getLogin());
        if(grava.getListaMateria().get(i).getLogin().equals(l) && grava.getListaMateria().get(i).getSenha().equals(s))
           {
              JOptionPane.showMessageDialog(null,"Login feito com sucesso","Bem vindo",1);
              dispose();
              MenuGUI oples = new MenuGUI();
           }
           System.out.println(grava2.getListaMateria().get(i).getLogin()+"\n"+grava2.getListaMateria().get(i).getSenha());                 
       i++;
       } 
      JOptionPane.showMessageDialog(null,"Login ou senha incorretos","Desculpe",1);


   }
 }

public void openFile()
{
  l = tLogin.getText();
  s = Senha.getText();
  try
  {
     input = new Scanner(new File( "login.txt" ));
  } // end try
  catch( FileNotFoundException fileNotFoundException )
  {
     System.err.println( "Error opening file." );
     System.exit( 1 );
    } // end catch


  ArrayList<Gravando> lp = new ArrayList<Gravando>();
  try // read records from file using Scanner object
  {
     while(input.hasNext())
     {
        grava.setLogin( input.next() ); // read account number
        grava.setSenha( input.next() ); // read first name

        lp.add(grava); 
     } // end try

  }
  catch ( NoSuchElementException elementException )
  {
     System.err.println( "File improperly formed." );
     input.close();
     System.exit( 1 );
  } // end catch
  catch ( IllegalStateException stateException )
  {
     System.err.println( "Error reading from file." );
     System.exit( 1 );
  } // end catch

  grava.setListaMaterias(lp);
  input.close();
} // end method openFile

But when squeeze gives the error:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.rangeCheck(ArrayList.java:653)
        at java.util.ArrayList.get(ArrayList.java:429)
    at LoginGUI.actionPerformed(LoginGUI.java:309)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6535)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6300)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4891)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
.
    
asked by anonymous 04.09.2016 / 21:29

1 answer

0

Your problem is here:

while( i <= grava.getListaMateria().size()

The comparison of the value of i should be < and not < =. The correct would be:

while( i < grava.getListaMateria().size()

The array indexes start at 0, so they only go up to the size of the array - 1. For example, if the size of an array is 2, accessible indexes would be 0,     

05.09.2016 / 01:56