Quantcast

Jump to content

» «
Photo

ArrayList error, returns -1

1 reply to this topic
Chris Fromage
  • Chris Fromage

    Homie

  • Members
  • Joined: 22 Sep 2012
  • Japan

#1

Posted 10 April 2013 - 03:00 PM Edited by Chris Fromage, 10 April 2013 - 03:16 PM.

Hi, It's me again smile.gif

I have a small problem with an arraylist.

When I got 4 textfields in my program. When fill something in (in all 4 textFields) and press on a 'save'-button, every textfield should be saved in 4 different arraylists.

This is the code I used for that:

public class knopHandler7 implements ActionListener {
public void actionPerformed(ActionEvent e) {

al1.add(veldNaam);
al2.add(veldPk);
al3.add(veldCilinder);
al4.add(veldVelgen);


veldnaam, veldPk, veldCilinder and veldVelgen are the names of my textfields.

private JTextField veldNaam = new JTextField();
private JTextField veldPk = new JTextField();
private JTextField veldCilinder = new JTextField();
private JTextField veldVelgen = new JTextField();




When I press on a 'load'-button, I get a ShowInputDialog, where I write something that I have written before in the textfield veldNaam.

Then I used a 'IndexOf(veldNaam) to get the Index of that object. I should use that Index to find text that I wrote on the other textfields (by using 'al2.get(index)' , 'al3.get(index)' and 'al4.get(index)' ).

My code:

public class knopHandler8 implements ActionListener {
public void actionPerformed(ActionEvent arg0) {

String merkNaam = JOptionPane.showInputDialog("Geef merk/model");

int index = al1.indexOf(merkNaam);
System.out.println(index);
System.out.println(al2.get(index));
System.out.println(al3.get(index));
System.out.println(al4.get(index));
JOptionPane.showMessageDialog(null, "De "+ merkNaam +" heeft een vermogen van "+ al2.get(index)
+" pk, "+ al3.get(index) +" cilinders en velgen met een diameter van "+ al4.get(index) +" inch");

}


And the Error I get:
-1 --> the output of System.out.println(index);
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at J01Ontwerp$knopHandler8.actionPerformed(J01Ontwerp.java:165)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)


(J01Ontwerp.java:164) is where I ask a sysout for al2.get(Index)

I googled a bit and I red somewhere that the Index from al1.indexOf(merkNaam) is not an Integer, but I have no idea how to fix it... cryani.gif

Chris Fromage
  • Chris Fromage

    Homie

  • Members
  • Joined: 22 Sep 2012
  • Japan

#2

Posted 10 April 2013 - 04:44 PM

Allright people, I found the solution biggrin.gif

For those who might have a similar problem later:

I used
String Array1 = al2.get(index);
System.out.println(Array1);
String Array2 = al3.get(index);
System.out.println(Array2);
String Array3 = al4.get(index);
System.out.println(Array3);
JOptionPane.showMessageDialog(null, "De "+ merkNaam +" heeft een vermogen van "+ Array1
+" pk, "+ Array2 +" cilinders en velgen met een diameter van "+ Array3 +" inch");


instead of

System.out.println(index);
System.out.println(al2.get(index));
System.out.println(al3.get(index));
System.out.println(al4.get(index));
JOptionPane.showMessageDialog(null, "De "+ merkNaam +" heeft een vermogen van "+ al2.get(index)
+" pk, "+ al3.get(index) +" cilinders en velgen met een diameter van "+ al4.get(index) +" inch");



System.out.println(al.get(index)); gives information about the Textfield on a certain position in the array, and not about what is written in the textfield tounge.gif
So I first made a String that gets the text out of that field.
and then I made a sysout od that. smile.gif




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users