0

My program starts by prompting the user how many text fields they would like to have

public class textEvent1 implements ActionListener {    //action listener for "how many masses?"
            public void actionPerformed (ActionEvent e) {
                n = (int)(Double.parseDouble(massNumField.getText())); 

next I create a for loop to create labels and textfields (which I have created lists for because I dont know how many there will be). There are a couple of lists but I will give an example of just one.

ArrayList masses = new ArrayList();

    for(int i=1; i<=n; i++) {                                  //adds text event 2 text to the screen
                        massLabel = new JLabel("How much mass does Mass " +i+ " have? ");
                        massField = new JTextField(5);
                        masses.add(massField);

Now my problem appears to come when I try to assign an element of the masses list to a variable like so.

for(int i=1; i<=n; i++) {
                        mass =  Double.parseDouble(((JTextComponent) masses.get(i)).getText());

I have tried a couple of things...mass = masses.get(i).....mass = masses.get(i).getText()) and so on and so on. I either keep getting errors such as Null pointer exceptions or things saying I cant parseDouble an Object.

There errors that arrise for this example are as below

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3 >= 3
    at java.util.Vector.elementAt(Unknown Source)
    at acmx.export.java.util.ArrayList.get(ArrayList.java:54)
    at Orbit$textEvent2.actionPerformed(Orbit.java:151)

line 151 is

mass =  Double.parseDouble(((JTextComponent) masses.get(i)).getText());
2
  • Could you edit your question to include how you're creating and setting up the masses variable? I suspect that may be the issue. (Hit the edit button in the lower-left of your question) Commented Sep 17, 2013 at 16:21
  • "Any suggestions would be very useful." I suggest you ask a question.. Also: 1) For better help sooner, post an SSCCE. 2) Always copy/paste error & exception output. Commented Sep 17, 2013 at 17:02

1 Answer 1

2

When creating the JTextFields, you do:

for(int i=1; i<=n; i++) {
    ...

Note that List indices start at 0, so when you retrieve the items with a similar loop, using i as the index, you are trying to access one past the last item. Change the reading loop indices to:

for (int i = 0; i < n; i++) {
    ...

Or you could use an enhance for loop, unless you need to use an ancient java version:

for (Object massField : masses) {
    mass =  Double.parseDouble(((JTextComponent) massField).getText());
    ...

(Then you should really use generics too, if the java version supports them).

Sign up to request clarification or add additional context in comments.

3 Comments

Btw, is there a particular reason to not use ArrayList<JTextField>? That would save the cast and be good for type safety.
ya I tried that and for some strange reason it didn't accept it. it came up with a warning saying "The type ArrayList is not generic; it cannot be parameterized with arguments <JTextField>"
@DavidJarrin looks like you're importing the wrong ArrayList. It should be java.util.ArrayList

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.