Ruben
Ruben

Reputation: 21

JTextfield array help

I have been trying to create a JFormattedTextField array which populates in a pane. Once a click event occurs, the actions get processed and the textfields are populated using setText(). The code for the array and initializing is in the constructor method here:

JFormattedTextField[] balance=new JFormattedTextField[360];
JFormattedTextField[] paidInt=new JFormattedTextField[360];

Then a for loop is used to instantiate the textfields in the panel in here:

for(count=0;count<360;++count)
{
    counter[count]=Integer.toString(count + 1);
    labPayment[count]=new JLabel(counter[count]);
    scheduler.add(labPayment[count]);
    balance[count]=new JFormattedTextField(amountFormat);
    scheduler.add(balance[count]);
    balance[count].addActionListener(this);
    paidInt[count]=new JFormattedTextField(amountFormat);
    scheduler.add(paidInt[count]);
    paidInt[count].addActionListener(this);
}

And finally, in the Action Performmed method I have the following to send back the text to those fields here:

if(e.getSource()==butSchedule)
{
        for(calc2count=0;calc2count<360; ++calc2count)
        {
            paidint[calc2count]=dmtgAmount * (dmtgRate / 12);
            payout[calc2count]=dmtgPayment- paidint[calc2count];
            dmtgAmount=dmtgAmount - payout[calc2count];
            stBalance[calc2count]=Double.toString(dmtgAmount);
            stpaidInt[calc2count]=Double.toString(paidint[calc2count]);

        //  balance[calc2count].setText(stBalance[calc2count]);
        //  paidInt[calc2count].setText(dollarsandcents.format(stpaidInt[calc2count]));

            System.out.println((stBalance[calc2count]);
            System.out.println("   ");
            System.out.println((stpaidInt[calc2count]);
            System.out.println("   ");
            //if structure used to default to $0 if the loan balance becomes $0 or less
            if(dmtgAmount<0)
                break;

            //if structure used to default to $0 if the interest becomes $0 or less
            if(paidint[calc2count]<0)
                break;
        }     
}

I used the println to confirm that the numbers are being generate, which they are. I continually get a nullpointer exception at these lines:

balance[calc2count].setText(stBalance[calc2count]);
paidInt[calc2count].setText(stpaidInt[calc2count]);

I have tried to initially setText in the constructor but it made no difference. Should I not be using a textfield array to do this?

Upvotes: 1

Views: 1895

Answers (1)

Ruben
Ruben

Reputation: 21

I had initiated the JFormattedTextField twice with these lines:

    private JFormattedTextField[] balance;
private JFormattedTextField[] paidInt;

and then this:

JFormattedTextField[] balance=new JFormattedTextField[360];
JFormattedTextField[] paidInt=new JFormattedTextField[360];

by removing JFormattedTextField[] in the second set of lines, I got the code to work perfectly.

Upvotes: 1

Related Questions