java.lang.NullPointerException Error
Hello All,
I know this is a common error with a simple fix but I can't figure out why that Null Pointer Exception error is happening in my code.
Sometimes a second eye helps so if anyone could just look over the following code it would be greatly appreciated :(happy):
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Lab9Init extends JApplet implements ActionListener
{
private JButton btnAdd, btnClear;
private JTextField tfTotal, tfQuantity;
private JLabel lblQuantity, lblTotal;
private JList lstDisplay;
private DefaultListModel dlmDisplay;
private JScrollPane scrollPane;
private ButtonGroup grpGames;
private JRadioButton radBattlefield3, radSkyrim, radMassEffect;
public void init() {
do_gui();
}//end main
private void do_gui()
{
Container c = this.getContentPane();
c.setLayout(null);
this.setSize(290, 250);
btnAdd = new JButton("Add");
btnAdd.setBounds(120,190,70,30);
c.add(btnAdd);
btnAdd.addActionListener(this);
this.getRootPane().setDefaultButton(btnAdd);
btnClear = new JButton("Clear");
btnClear.setBounds(200,190,70,30);
c.add(btnClear);
btnClear.addActionListener(this);
tfTotal = new JTextField();
tfTotal.setEditable(false);
tfTotal.setBounds(180,140,60,30);
c.add(tfTotal);
tfTotal.setHorizontalAlignment(SwingConstants.CENTER);
tfQuantity = new JTextField();
tfQuantity.setBounds(70,190,40,30);
c.add(tfQuantity);
lblTotal = new JLabel();
lblTotal.setBounds(110,140,60,30);
c.add(lblTotal);
lblTotal.setHorizontalAlignment(SwingConstants.RIGHT);
lblQuantity = new JLabel();
lblQuantity.setBounds(10, 190, 50, 30);
c.add(lblQuantity);
lblQuantity.setHorizontalAlignment(SwingConstants.RIGHT);
grpGames.add(radBattlefield3);
grpGames.add(radSkyrim);
grpGames.add(radMassEffect);
radBattlefield3 = new JRadioButton(String.format("Battlefield 3 $.2f", COrder.PBATTLEFIELD3), true);
radBattlefield3.setBounds(10,20,110,30);
c.add(radBattlefield3);
radMassEffect = new JRadioButton(String.format("Mass Effect $.2f", COrder.PMASSEFFECT));
radMassEffect.setBounds(10, 60,110,30);
c.add(radMassEffect);
radSkyrim = new JRadioButton(String.format("Skyrim $.2f",COrder.PSKYRIM));
radSkyrim.setBounds(10, 100, 100, 30);
c.add(radSkyrim);
dlmDisplay = new DefaultListModel();
lstDisplay = new JList(dlmDisplay);
scrollPane = new JScrollPane(lstDisplay,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBounds(120, 10, 150, 120);
c.add(scrollPane);
}//end doGui
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==btnAdd)do_Add();
else if (e.getSource()==btnClear)do_Clear();
else
System.exit(0);
}//end actionPerformed
private void do_Add(){
dlmDisplay.addElement("Hello World");
}
private void do_Clear(){
dlmDisplay.clear();
}
}//end class
Re: java.lang.NullPointerException Error
Critical information: which line throws the NPE?
Re: java.lang.NullPointerException Error
Ooops thought I included that, sorry.
It's 64 and 22.
Re: java.lang.NullPointerException Error
Quote:
Originally Posted by
rockintyler
Ooops thought I included that, sorry.
It's 64 and 22.
OK, on line 64 -- check the variables that could possibly be null (there's only one possibility) . Now check to see if you initialized it before using it.
Re: java.lang.NullPointerException Error
Thanks for the response.
I forgot to initialize the button group so thanks for giving me an idea what to think about. I'm in a beginning Java course and this is the assignment I'm working on for it.
Re: java.lang.NullPointerException Error
Quote:
Originally Posted by
rockintyler
Thanks for the response.
I forgot to initialize the button group so thanks for giving me an idea what to think about. I'm in a beginning Java course and this is the assignment I'm working on for it.
You're welcome. There's more null problems though. Again, look at when you're initializing your variables with regards to when you're adding them to the button group.