Exception in thread "AWT-Eventqueue-0" java.lang.Indexoutofboundsexception: Index: 0, Size: 0

Asked

Viewed 436 times

-1

I need to open a file txt and store his information in a ArrayList and then make a comparison between what was typed and what is in bad ArrayList to see if the login is right or not. Just follow 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 I spin it makes the mistake:

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)
.
  • Can you add the full stack of the error? This error is related to accessing non-existent indices in an array or array.

  • Of course, I edited my question and added the full error @diegofm

  • Where is the line 309?

  • The small 309 is: System.out.println(grava2.getListaMateria(). get(i). getLogin()+" n"+grava2.getListaMateria(). get(i). getSelf());

  • 1

    As I said before, you must be accessing a non-existent input from the arrayList. Maybe adding a [mcve] will be possible to locate the error.

  • Another detail, the array is empty, check your method that fills it with TXT, probably nothing is being copied.

  • I managed to resolve the situation,@diegofm

Show 2 more comments

1 answer

0


Your problem is here:

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

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

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

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

  • thank you so much for your help, I did what you said and it worked

  • Show! Mark as answered to help other people :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.