1

I have little problem with inner methods in Java. In line:

dReservation[i].dispose();

I have an error:

Cannot refer to a non-final variable dReservation inside an inner class defined in a different method

I have read many threads in forum, but there are two solutions of that problem which didn't works:

Cannot refer to a non-final variable inside an inner class defined in a different method

Cannot refer to a non-final variable i inside an inner class defined in a different method

Why a non-final "local" variable cannot be used inside an inner class, and instead a non-final field of the enclosing class can?

I have tried to set JDialog[] dReservation as global field for my class (GUIShowReservations). Then my error disappears, but in the inner method (actionPerformed) instead of dReservation[i] is null.

Just the same history is when I set JDialog[] dReservation as final field. It is null.

bShowReservations = new JButton("Show Reservations");
bShowReservations.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
  JDialog[] dReservation = new JDialog[10000];

  for(Object o: reservations)
  {
    rez = (Reservations)o;
    reservation.append(rez.getGroup());

    dReservation[i] = new JDialog();
    dReservation[i].setSize(400, 300);
    dReservation[i].setLocationRelativeTo(null);
    dReservation[i].setVisible(false);
    dReservation[i].setLayout( null );
    dReservation[i].setTitle("Edition");

    bEditAccept = new JButton("Edit");
    bEditAccept.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent arg0) {
            rez.setTeacher(cEditTeacher.getSelectedItem().toString());
            dao.update(rez);
            dReservation[i].dispose();
        }
    });
    bEditAccept.setSize(160, 24);
    bEditAccept.setLocation(10, 200);
    dReservation[i].add(bEditAccept);
  }
}
});

Could you help me? I want to see a proper JDialog in my inner method instead of null.

Community
  • 1
  • 1
chrison
  • 11
  • 1
  • 3
    Where is `i` declared? Can you show a short but *complete* program demonstrating the problem? – Jon Skeet Dec 24 '12 at 15:25
  • i is declared as global variable. This variable is initialized (i=0) before declaration of dReservation table. After loop i is iterated. I have dropped some unnecessary lines of code in my post. What is interesting I see proper value of variable i in inner method. – chrison Dec 24 '12 at 15:39

1 Answers1

0

It's hard to answer, because the code is incomplete, and the above code doesn't make much sense. I'll explain what it actually does, because I don't think that it does what you want it to do.

The code creates a single button labeled "Show Reservations". When this button is clicked, an array of ten thousand (!) JDialog instances is created (why?). All these dialogs are null.

Then, a loop is done, iterating on the reservations. For each reservation, a new JDialog is created, containing a new button named "Edit", which closes the dialog when clicked. And the last one of these dialogs is stored in the giant array, at the index i. All the other ones are created for nothing, since they're replaced by the next one, at the same index in the array.

So at the end of the loop, you have created one dialog for each reservation, but stored only one of them in a giant array. And since this giant array is a local variable, nobody can use this dialog anyway, because the array and its unique dialog goes out of scope. So your action listener does the equivalent of the following one, but in a very slow and inefficient way:

bShowReservations.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent arg0) {
        for (Object o: reservations) {
            rez = (Reservations) o;
            reservation.append(rez.getGroup());
        }
    }
}

Also not that your code is full of bad practices. You're not using generic collections, you're setting sizes on buttons, you're not using a layout manager and instead set arbitrary locations...

If you tell us what the code should do, we could help you make it do that.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255