Results 1 to 6 of 6
  1. #1
    rockintyler is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default 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

    Java 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    rockintyler is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default Re: java.lang.NullPointerException Error

    Ooops thought I included that, sorry.

    It's 64 and 22.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: java.lang.NullPointerException Error

    Quote Originally Posted by rockintyler View Post
    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.

  5. #5
    rockintyler is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default 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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: java.lang.NullPointerException Error

    Quote Originally Posted by rockintyler View Post
    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.

Similar Threads

  1. error occured:-java.lang.NullPointerException
    By niksipandit in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-17-2011, 02:54 PM
  2. java.lang.NullPointerException Error
    By Manfizy in forum NetBeans
    Replies: 3
    Last Post: 02-24-2011, 06:27 AM
  3. Please solve my java.lang.NullPointerException error.?
    By Viruthagiri in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-23-2010, 09:30 PM
  4. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  5. Replies: 2
    Last Post: 07-02-2007, 02:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •