• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException with JButton

 
bobby, morkos
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone tell me what I did wrong. I have a button within another panel and I want to set that button to disable. I did something like below and I get a NullPointerException. Does anyone know what I did wrong. Thanks.
I have a getter method in my button panel that returns a reference to the button.
in BtnPanel:
JButton theButton = new JButton("load");
JButton getButton(){ return theButton; }
And call getButton() from another panel (mainPanel), like:
BtnPanel bt = new BtnPanel();
bt.getButton().setEnable(false);
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
make sure that u dont try to call this line:
bt.getButton().setEnable(false);
before u create the button in the first panel with this line:
JButton theButton = new JButton("load");
what i think happens is that u try to get the button BEFORE u create it.
 
bobby, morkos
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this code:
public class ButtonPanelView extends JPanel {
private static String _TreeName=null;
private JMXApplicationView _app;
private ObjectMBeanInfo _mBeanInfo;
JButton btnMbeanInfo = new javax.swing.JButton("MBean Info");

protected JButton getInfoBeanButton(){
return btnMbeanInfo;
}

public ButtonPanelView(JMXApplicationView app) {
_app = app;
initComponents();
}

public ButtonPanelView(ObjectMBeanInfo mBeanInfo) {
_mBeanInfo = mBeanInfo;
initComponents();
}

public ButtonPanelView(String treeSelectionName) {
_TreeName = treeSelectionName;
initComponents();
}
public ButtonPanelView() {
initComponents();
}
private void initComponents() {
pnlButton = new javax.swing.JPanel();
//btnMbeanInfo = new javax.swing.JButton();
btnCancel = new javax.swing.JButton();
//btnMbeanInfo.setText("MBean Info");
btnMbeanInfo.setForeground(Color.blue);
btnCancel.setText("Cancel");
btnCancel.setForeground(Color.blue);
btnCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCancelActionPerformed(evt);
}
});
btnMbeanInfo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnMBeanInfoActionPerformed(evt);
}
});
pnlButton.add(btnMbeanInfo); //btnMbeanInfo
pnlButton.add(btnCancel);
add(pnlButton);
// the line below works, but when I call it from another panel, then I get a NullPointerException
this.getInfoBeanButton().setEnabled(false);
}
// Variables declaration
private javax.swing.JPanel pnlButton;
private javax.swing.JButton btnReload;
private javax.swing.JButton btnCancel;
// end Variables declaration
}

I call it within another panel, something like this:
public class TreePanelView extends JPanel{
private ButtonPanelView _bn;
public TreePanelView(ButtonPanelView bn) {
_bn = bn;
initComponents();
}
// here is where I'm getting NullPointerEx ception
// event trigerred here like button click
_bn.getInfoBeanButton().setEnabled(true);
}
Originally posted by Roy Ben Ami:
make sure that u dont try to call this line:
bt.getButton().setEnable(false);
before u create the button in the first panel with this line:
JButton theButton = new JButton("load");
what i think happens is that u try to get the button BEFORE u create it.

[ February 06, 2002: Message edited by: bobby, morkos ]
[ February 06, 2002: Message edited by: bobby, morkos ]
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2 things:
first are these lines commented out as // in your code also?
//btnMbeanInfo = new javax.swing.JButton();
//btnMbeanInfo.setText("MBean Info");
is so, uncomment them.
next, r u making an object of ButtonPanelView somewhere in your code?
if u do then make sure u create that panel (and therefore create the button) before calling any methods on it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!