I learn myself to understand Java and make as exercise in Netbeans 4.0 a
popup program. A start with Frame and add the popupMenu with items. With the
MousePressed I call "poupMenu.show(this, x,y)" and hope to see the popup
menu. But that doesn't happen. The frame window is on the screen but when I
press the mouse I see the next note;
"Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException:
parent is null". It looks like if "this" can not be found.
Thanks for help.
Arie Tamboer
proGex - 24 Feb 2005 16:07 GMT
would be helpful if you post your code so others can help see where is
wrong
A.Tamboer - 04 Mar 2005 13:15 GMT
The complete code is;
/*
* PopFrame.java
*
public class PopFrame extends java.awt.Frame {
public PopFrame() {
initComponents();
setSize(300,200);
}
private void initComponents() {
popupMenu1 = new java.awt.PopupMenu();
menuBar1 = new java.awt.MenuBar();
menu1 = new java.awt.Menu();
popupMenu1.setLabel("PopupMenu");
setLayout(new java.awt.GridLayout(2, 2));
addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
formMousePressed(evt);
}
});
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
menu1.setLabel("Menu");
menuBar1.add(menu1);
setMenuBar(menuBar1);
pack();
}
private void formMousePressed(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
if(evt.getModifiers()!=0){
popupMenu1.show(this, evt.getX(), evt.getY());
}
}
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PopFrame().setVisible(true);
}
});
}
private java.awt.Menu menu1;
private java.awt.MenuBar menuBar1;
private java.awt.PopupMenu popupMenu1;
}
> would be helpful if you post your code so others can help see where is
> wrong
Bjorn Abelli - 04 Mar 2005 14:44 GMT
>I learn myself to understand Java and make as exercise in Netbeans 4.0 a
>popup program. A start with Frame and add the popupMenu with items. With
[quoted text clipped - 4 lines]
> "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException:
> parent is null". It looks like if "this" can not be found.
It's not always that the Component provided in "popupMenu1.show" is the
parent.
It can also be another item within the parent, but the latter isn't set in
your case.
To set the parent, you have to add the PopupMenu to the Frame. Put this
somewhere in your code:
this.add(popupMenu1);
http://java.sun.com/j2se/1.3/docs/guide/awt/designspec/popupmenu.html
Note.
This behaviour is different in the corresponding Swing-component JPopupMenu,
which works without that "adding".
http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html#popup
// Bjorn A