Pergunta

I have the following code here:

public class A extends Runnable {
  ArrayList<String> choosenFiles;

  run(){
  /*graphic stuff such as JFrame,JLabel,JButtons*/
  }

  private ButtonListener implements ActionListener {
  /*options*/
  }

  public class cbListener implements ItemListener {
    @Override
    public void itemStateChanged(ItemEvent e) {
        Checkbox cb = (Checkbox)e.getItemSelectable();
        String cbl = cb.getLabel();

        if (e.getStateChange() == ItemEvent.SELECTED) {
            JOptionPane.showMessageDialog(null, "selected:\n" + cbl);
            choosenFiles.add(cbl);
        } else if (e.getStateChange() == ItemEvent.DESELECTED) {
            JOptionPane.showMessageDialog(null, "deselected:\n" + cbl);
            choosenFiles.remove(cbl);
        }
    }
}  

The problem seems to be that CBListener can not reach choosenFile since an AWT-EventQueue-0 java.lang.NullPointerException comes up.

Here is the stacktrace:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at FileDownloader.Client.User$cbListener.itemStateChanged(Unknown Source)
at java.awt.Checkbox.processItemEvent(Checkbox.java:563)
at java.awt.Checkbox.processEvent(Checkbox.java:530)
at java.awt.Component.dispatchEventImpl(Component.java:4660)
at java.awt.Component.dispatchEvent(Component.java:4488)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:674)
at java.awt.EventQueue.access$400(EventQueue.java:81)
at java.awt.EventQueue$2.run(EventQueue.java:633)
at java.awt.EventQueue$2.run(EventQueue.java:631)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege
(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege
(AccessControlContext.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:647)
at java.awt.EventQueue$3.run(EventQueue.java:645)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege
(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:644)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Is there a way to solve this problem?

Foi útil?

Solução

It looks like choosenFiles has not been initialized; this should happen early on, e.g. in the constructor or an initialization method. Also it is good practice to use the most general interface possible, so if there's not a very good reason for choosenFiles do be defined as an ArrayList<String>, make it a List<String>.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top