سؤال

I have a simple interface that is designed to select one or more seats in a theater and reserve them for yourself.

I have three columns: zone / row / seat. You select the first to populate the second, and the second to populate the third.

When I select them the first time no problem.

Selected an item in all three columns for the first time

If I change the second row, the third row re-populates (in this case all rows have the same number of seats) it does not break!

Selected a new item in column two

However if I change the first row everything breaks!

Selected a new item in column one

Now the reason for this is kinda clear, but I don't understand exactly why this is.

This is the first list event trigger:

List_Zona.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent arg0) {
                if (! arg0.getValueIsAdjusting()){

                    RowListModel.clear(); // second list
                    SeatListModel.clear(); // third list
                    List_Rand.clearSelection(); // second list
                    List_Scaun.clearSelection(); // third list

                    int[] rows = self.repository.getRowsForZone(
                              List_Zona.getSelectedValue().toString()
                    );
                    int index = 0;
                    while (rows[index]!=0) {
                          RowListModel.addElement(String.valueOf(rows[index]));
                          index++;
                    }
                }
            }
        });

It should clear the other column selections so it should not interfere with this next trigger (second column), but apparently it does, or something happens which I don't get:

List_Rand.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent arg0) {
                if (! arg0.getValueIsAdjusting()){

                    SeatListModel.clear();

                    int[] seats = self.repository.getSeatByZoneAndRow(
                          List_Zona.getSelectedValue().toString(),
                          Integer.parseInt(List_Rand.getSelectedValue().toString())//HERE
                    );
                    int index = 0;
                    while (seats[index]!=0) {
                        SeatListModel.addElement(String.valueOf(seats[index]));
                        index++;
                    }
                }
            }
        });

It can't parse the integer because the second column should be cleared, but it's not ? But even though it's not ... it is ?

What I'm trying to say: The second column should be clear (it's not) but if it's not then the error should not occur, but it does.

I hope this makes some sense!

Can anyone spot the problem ? Should I provide more code ?

The error is a NullPointerException at the second column, because something fishy is happening there (again: at the integer parsing).

By my mind the second column's valueChanged should not trigger at all when I click an item in the first column. It should just clear the other two and that's that. Why am I wrong ?

P.S. First Code snippet is responsible for the second one breaking the program.

Maybe I should also rephrase the question How can I safely clear everything when I re-select a new "Zone" (Zona) - Column one ?

هل كانت مفيدة؟

المحلول

The first listener clears the selection of List_Rand. That makes the selection change: it goes from "the index i is selected" to "no index is selected", so the second listener is invoked. And in the second listener, you're trying to call a method on the selected value of List_Rand. Since you just cleared the selection, there's no selected value anymore, hence the NullPointerException.

Side note: your code is very hard to read, because it doesn't respect the Java naming conventions. Variables start with a lowercase letter, and are camelCased (no underscore in their name).

Other side note : what's the point in calling parseInt(selectedValue.toString())? If the list contains Integer instances, the cast the value to Integer directly. If it contains String, then why not store Integers instead, since this is what you want the list to contain?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top