Llevo rato trasteando y tratando de arreglarlo pero no hay manera. La excepción que salta es
java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
package phonemanagergui.views;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import phonemanagergui.controllers.MainController;
import phonemanagergui.model.Contact;
public class RemoveView extends JPanel implements ActionListener{
List<Contact> myContacts = new ArrayList<Contact>();
MainController controller;
JTextField phoneText, nameText, surnameText;
Contact contact;
boolean remove;
int position;
public RemoveView(MainController controller){
this.controller = controller;
this.myContacts = this.controller.getAllContacts();
contact = new Contact();
remove = false;
position = 0;
initComponents();
}
private void initComponents(){
this.setLayout(new GridLayout(5,2));
JLabel phone = new JLabel("Teléfono");
this.add(phone);
if(myContacts!=null)phoneText=new JTextField(myContacts.get(0).getPhone());
else phoneText=new JTextField();
phoneText.setEditable(false);
this.add(phoneText);
JLabel name = new JLabel("Nombre");
this.add(name);
if(myContacts!=null) nameText=new JTextField(myContacts.get(0).getName());
else nameText=new JTextField();
nameText.setEditable(false);
this.add(nameText);
JLabel surname = new JLabel("Apellido");
this.add(surname);
if(myContacts!=null) surnameText=new JTextField(myContacts.get(0).getSurname());
else surnameText=new JTextField();
surnameText.setEditable(false);
this.add(surnameText);
JButton nextButton = new JButton("Siguiente");
nextButton.addActionListener(this);
nextButton.setActionCommand("next");
this.add(nextButton);
JButton previousButton = new JButton("Anterior");
previousButton.addActionListener(this);
previousButton.setActionCommand("previous");
this.add(previousButton);
JButton removeButton = new JButton("Borrar");
removeButton.addActionListener(this);
removeButton.setActionCommand("delete");
this.add(removeButton);
}
public void actionPerformed(ActionEvent e){
switch(e.getActionCommand()){
case "next":
if(myContacts!=null) remove=true;
else remove=false;
if(remove){
if(position<myContacts.size()-1)position++;
contact = myContacts.get(position);
phoneText.setText(contact.getPhone());
nameText.setText(contact.getName());
surnameText.setText(contact.getSurname());}
else {
controlEmptyList();
}
break;
case "previous":
if(myContacts!=null) remove=true;
else remove=false;
if(remove){
if(position>0)position--;
contact = myContacts.get(position);
phoneText.setText(contact.getPhone());
nameText.setText(contact.getName());
surnameText.setText(contact.getSurname());}
else {
controlEmptyList();
}
break;
case "delete":
if(myContacts!=null) remove=true;
else remove=false;
if(remove)
{
controller.deleteContact(position);
myContacts.remove(myContacts.get(position));
if(position>0)position--;
else position++;
if(myContacts.size()==1 && position>0){
position=0;
contact = myContacts.get(position);
phoneText.setText(contact.getPhone());
nameText.setText(contact.getName());
surnameText.setText(contact.getSurname());
}
else{
if(/*myContacts.size()>0*/myContacts!=null){
contact = myContacts.get(position);
phoneText.setText(contact.getPhone());
nameText.setText(contact.getName());
surnameText.setText(contact.getSurname());}
}
}
else {
controlEmptyList();
}
break;
default:
break;
}
}
private void controlEmptyList(){
JOptionPane.showMessageDialog(null, "No hay contactos");
phoneText.setText("");
nameText.setText("");
surnameText.setText("");
}
}
Ese es el código.
Supuestamente falla aquí:
else{
if(/*myContacts.size()>0*/myContacts!=null){
===> contact = myContacts.get(position);
phoneText.setText(contact.getPhone());
nameText.setText(contact.getName());
surnameText.setText(contact.getSurname());}
}
Si dejaba
myContacts.size()>0
me saltaba un NullPointerException.
Gracias de antemano. Saludos.