5 Replies Latest reply: Jan 11, 2010 10:33 AM by DarrylBurke RSS

    Popups throwing a NullPointerException

    843807
      I know that I will get jives because I am using awt, and I bet this problem is about Swing being more developed than awt, but anyway:

      I am trying to get a popup to appear when I click on a label. (right-click because I am using Windows). I am using the following code. When I right-click on the labl I get a NullPointerException, because the popup doesn't have a parent. From what I understood, a parent I the Menu from which the popup should appear, but I don't want it that way; I want it to popup from "anywhere" (well, actually, the label).

      May I note that I downloaded the source code from Sun's tutorials and they worked, I added a simple command to check for the popup's parent and discovered it is null, but that was in Swing.

      Can you please advise? Many thanks.

      here's the code:
      import java.awt.*;
      import java.awt.event.*;
      
      public class MyFrame extends Frame implements ActionListener
      {
           //properties
           private Label lbl;
           private MenuItem miClick;
           private PopupMenu pum;
      
           //constructor
           public MyFrame()
           {
                setSize(200, 200);
                lbl = new Label("before menu item");
                lbl.addMouseListener(new MyMouseEvent(pum));
                add(lbl);
      
                pum = new PopupMenu();
                miClick = new MenuItem("Click me");
                miClick.addActionListener(this);
                pum.add(miClick);
      
                setVisible(true);
           }
      
           //methods
           public void actionPerformed(ActionEvent ae)
           {
                lbl.setText("After Click");
           }
      
           //nested class
           public class MyMouseEvent extends MouseAdapter
           {
                //properties
                PopupMenu pum;
      
                //constructor
                public MyMouseEvent(PopupMenu pop)
                {
                     pum = pop;
                }
      
                //methods
                public void mousePressed(MouseEvent me)
                {
                     doPopup(me);
                }
      
                public void mouseReleased(MouseEvent me)
                {
                     doPopup(me);
                }
      
                public void doPopup(MouseEvent me)
                {
                     if(me.isPopupTrigger())
                     {
                          pum.show(me.getComponent(), me.getX(), me.getY());
                     }
                }
           }
      }
      Run it from any where like this:
      MyFrame f = new MyFrame();
      Right-click on the label named click me, and a NullPointerException should be throw.

      Thanks
        • 1. Re: Popups throwing a NullPointerException
          DarrylBurke
          Of course you'll get a NullPointerException. Your code constructs the MyMouseEvent with a null reference pum[v] which is only 2 lines later initialized to a new PopupMenu.

          And MyMouseEvent is a terrible name for a class that is in fact a MouseListener and not in any way a MouseEvent.

          db
          • 2. Re: Popups throwing a NullPointerException
            843807
            Oh my gosh, I am sooo sorry about that.
            I amended the code, and the constructor now read:
            public MyFrame()
            {
                 setSize(200, 200);
                 lbl = new Label("before menu item");
            
                 pum = new PopupMenu();
                 miClick = new MenuItem("Click me");
                 miClick.addActionListener(this);
                 pum.add(miClick);
            
                 lbl.addMouseListener(new MyMouseEvent(pum));
                 add(lbl);
            
                 setVisible(true);
            }
            Unfortunately, it's still throwing the same exception: NullPointerException: parent is null
            DarrylBurke wrote:
            And MyMouseEvent is a terrible name for a class that is in fact a MouseListener and not in any way a MouseEvent.

            db
            I know, but I wrote that in 2 minutes, and didn't really take notice of naming. =D It's just a SSCCE.

            Thanks for the time,
            Two-eyes %

            PS: the TraceStack shows that the exception is being thrown here:
            public void doPopup(MouseEvent me)
            {
                 if(me.isPopupTrigger())
                 {
                      pum.show(me.getComponent(), me.getX(), me.getY()); //<--here
                 }
            }
            Edited by: two-eyes on Jan 11, 2010 7:09 AM
            • 3. Re: Popups throwing a NullPointerException
              DarrylBurke
              Unfortunately, it's still throwing the same exception: NullPointerException: parent is null
              That should have been enough for you to realize that you have neglected to add the PopupMenu to the GUI hierarchy.
              public MyFrame()
              {
                   setSize(200, 200);
                   lbl = new Label("before menu item");
               
                   pum = new PopupMenu();
                   miClick = new MenuItem("Click me");
                   miClick.addActionListener(this);
                   pum.add(miClick);
               
                   lbl.addMouseListener(new MyMouseEvent(pum));
                   add(lbl);
                   add(pum); // <----------------
               
                   setVisible(true);
              }
              db
              • 4. Re: Popups throwing a NullPointerException
                843807
                I see. I didn't know that :-\ It works fine now (obviously)

                Very sorry that I took your time for such a trivial thing
                • 5. Re: Popups throwing a NullPointerException
                  DarrylBurke
                  Very sorry that I took your time for such a trivial thing
                  No, that's ok. So long as you learned something, my time is not wasted.

                  db