2011-02-02 63 views
1

我會讓這個過程簡單快捷。線程中的異常「AWT-EventQueue-1」

我得到一個錯誤NPE ...

Row: 0 
You pressed on Item 1 
Removed cancel for Item 1 
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException 
     at inv$MousePopupListener.checkPopup(inv.java:118) 
     at inv$MousePopupListener.mouseReleased(inv.java:104) 
     at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:2 
73) 
     at java.awt.Component.processMouseEvent(Component.java:6267) 
     at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) 
     at java.awt.Component.processEvent(Component.java:6032) 
     at java.awt.Container.processEvent(Container.java:2041) 
     at java.awt.Component.dispatchEventImpl(Component.java:4630) 
     at java.awt.Container.dispatchEventImpl(Container.java:2099) 
     at java.awt.Component.dispatchEvent(Component.java:4460) 
     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577 
) 
     at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238) 

     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) 
     at java.awt.Container.dispatchEventImpl(Container.java:2085) 
     at java.awt.Component.dispatchEvent(Component.java:4460) 
     at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) 
     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre 
ad.java:269) 
     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread. 
java:184) 
     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre 
ad.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) 

我已經搜查高和低,我不知道如何解決它:

import java.awt.*; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.event.MouseAdapter; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 
import javax.swing.border.*; 
import javax.swing.event.*; 

public class inv extends JApplet implements MouseListener 
{ 
    public JList listbox; 
    public JPopupMenu popup; 
    public JMenuItem item; 

    public JPopupMenu useDropPopup; 
    public JPopupMenu useCancelPopup; 



public void init() { 
    ActionListener menuListener = new ActionListener() 
     { 
      public void actionPerformed(ActionEvent event) 
      { 
       String invAction = event.getActionCommand(); 


       int itemSelect = listbox.getSelectedIndex(); 
       Object actItem = listbox.getModel().getElementAt(itemSelect); 

       System.out.println("Popup menu item [" + invAction + "] [ " + actItem + " ] was pressed."); 
      } 
     }; 

    useDropPopup = new JPopupMenu(); 
    useCancelPopup = new JPopupMenu(); 

    JMenuItem useMenuItem = new JMenuItem("Use"); 
    useMenuItem.addActionListener(menuListener); 
    JMenuItem dropMenuItem = new JMenuItem("Drop"); 
    dropMenuItem.addActionListener(menuListener); 
    JMenuItem cancelMenuItem = new JMenuItem("Cancel"); 
    cancelMenuItem.addActionListener(menuListener); 

    useDropPopup.add(useMenuItem); 
    useDropPopup.add(dropMenuItem); 

    useCancelPopup.add(useMenuItem); 
    useCancelPopup.add(cancelMenuItem); 


     String listData[] = 
     { 
      "Item 1","Item 2","Item 3","Item 4" 
     }; 

     listbox = new JList(listData); 
     listbox.addMouseListener(new MouseAdapter() 
     { 
      public void mousePressed(MouseEvent e) 
      { 
       if (SwingUtilities.isRightMouseButton(e)) 
       { 
        System.out.println("Row: " + getRow(e.getPoint())); 
        listbox.setSelectedIndex(getRow(e.getPoint())); 
       } 
      } 
     } 
     ); 

     listbox.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
     add(listbox); 
     listbox.setVisible(true); 
     listbox.setFocusable(false); 


     listbox.addMouseListener(new MousePopupListener()); 

     useMenuItem.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent event) { 
       useMenuAction(event); 
      } 
     }; 

     // ... after the init method 

     public void useMenuAction(ActionEvent evt) { 
      // Add specific use menu code here. 
} 
    } 



    class MousePopupListener extends MouseAdapter 
    { 
     public void mousePressed(MouseEvent e) 
     { 
      checkPopup(e); 
     } 

     public void mouseClicked(MouseEvent e) 
     { 
      checkPopup(e); 
     } 

     public void mouseReleased(MouseEvent e) 
     { 
      checkPopup(e); 
     } 

     private void checkPopup(MouseEvent e) 
     { 
      if (e.isPopupTrigger()) 
      { 

       int itemSelectx = listbox.getSelectedIndex(); 
       Object actItemx = listbox.getModel().getElementAt(itemSelectx); 
       System.out.println("You pressed on " + actItemx); 

      if (actItemx == "Item 1") { 
       System.out.println("Removed cancel for " + actItemx); 
       popup.remove(itemSelectx); // So upon right-click on Item 1, you won't see "Cancel" menu. 
      } 

       popup.show(inv.this, e.getX(), e.getY()); 
       popup.revalidate(); 
      } 
     } 
    } 

    private int getRow(Point point) 
    { 
     return listbox.locationToIndex(point); 
    } 

    public void mouseEntered(MouseEvent e) 
    { 
    } 

    public void mouseReleased(MouseEvent e) 
    { 
    } 

    public void mousePressed(MouseEvent e) 
    { 
    } 

    public void mouseClicked(MouseEvent e) 
    { 
    } 

    public void mouseExited(MouseEvent e) 
    { 
    } 
} 

回答

1

我沒有看到在此代碼中的任何位置分配給popup,所以它將始終具有其默認值null

你忘了說

popup = useDropPopup; 

popup = useCancelPopup; 

也許

popup = new JPopupMenu(); 

地方?否則那個例程就不知道你在說什麼popup

此外,由於您要通過移除某個項目來突變popup,請注意它是新創建/複製的JPopupMenu,或者您將已移除的項目放回到希望它位於其中的情況。

1

我覺得JPopupMenu的彈出窗口null,必須使用初始化:

​​

或指定給您定義的其他彈出窗口的一個......的例外是明確不夠,甚至告訴你哪一行包含有問題的代碼!

4

學習閱讀您​​的堆棧跟蹤:

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException 
     at inv$MousePopupListener.checkPopup(inv.java:118) 
     at inv$MousePopupListener.mouseReleased(inv.java:104) 
     at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:2 
73) 

它說,有一個NullPointerExceptionin線118

...checkPopup(inv.java:118).... 

而這又被稱爲通過線104:

... useReleased(inv.java:104).. 

如果您看到這些行,您將擁有:

this

正如你可以看到,在該行援引your're:

popup.remove(itemSelectx); 

該行可能會拋出一個NullPointerException異常的唯一方法,就是popup是空的,因爲null.remove..引發NullPointerException異常。

所以,初始化你的彈出窗口是有道理的。

我希望這可以幫助你在這個在將來的例外。

+0

+1優秀捕魚的教訓! – trashgod 2011-02-02 22:38:37