Results 1 to 2 of 2
  1. #1
    Sathelan is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default Volume Calculator Problem

    Hello all! I am having a problem with a calculator that calculates the volume of a pool based on user input. Our program runs (its a group program) but our calculate button always gives us this error:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at FinalProject$calcBtnHandler.actionPerformed(FinalP roject.java:136)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    this is our program so far (We are going to make another tab with a different calculator but we want to figure the problem with this one first.)
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.*;
    
    public class FinalProject extends JFrame 
    {
    	private JTabbedPane tab;
    	private JPanel p1;
    	private JButton calcBtn;
    	private JButton extBtn;
    	private JTextField t1;
    	private JTextField t2;
    	private JTextField t3;
    	private JTextField sumField;
    	private JLabel l1;
    	private JLabel l2;
    	private JLabel l3;
    	private JLabel sumLabel;
    	
    	public FinalProject()
    	    {
    	    	super("Final Project"); //call super JFrame constructor
    	    	    	
    	    			    
    	    JTabbedPane tab = new JTabbedPane();
    	   
    	    //define components for tab 1
    	    JLabel l1 = new JLabel("Enter the length of the hot tub:");
    		JLabel l2 = new JLabel("Enter the height of the hot tub:");
    		JLabel l3 = new JLabel("Enter the depth of the hot tub:");
    		JTextField t1 = new JTextField(5);
    		JTextField t2 = new JTextField(5);
    		JTextField t3 = new JTextField(5);
    		JButton calcBtn = new JButton("Calculate Volume");
    		JLabel sumLabel = new JLabel("The hot tub volume is:");
    		JTextField sumField = new JTextField(13);
    		JButton extBtn = new JButton("Exit");
    		
    		JPanel p1 = new JPanel();
    		
    //		add components to the tab
    		p1.setBackground(Color.WHITE);
    		p1.setLayout(new FlowLayout());
    		p1.add(l1);
    		p1.add(t1);
    		p1.add(l2);
    		p1.add(t2);
    		p1.add(l3);
    		p1.add(t3);
    		p1.add(calcBtn);
    		p1.add(sumLabel);
    		p1.add(sumField);
    		p1.add(extBtn);
    		tab.addTab("Hot tubs",null, p1, "Panel #1");
    		getContentPane().add(tab);
    		
    		setSize(200,350);
    		setVisible(true);
    		
    		/*define and register window event handler
    		p1.addWindowListener(new Window Adapter()
    		{
    			public void windowClosing(Window Event e){System.exit(0);}
    		});
    		*/
    			   		
    		//register listeners
    		calcBtnHandler chandler = new calcBtnHandler();
    		calcBtn.addActionListener(chandler);
    		extBtnHandler ehandler = new extBtnHandler();
    		extBtn.addActionListener(ehandler);
    		
    		//create and register the new focus handler
    		FocusHandler fhandler = new FocusHandler();
    		t1.addFocusListener(fhandler);
    		t2.addFocusListener(fhandler);
    		t3.addFocusListener(fhandler);
    			sumField.addFocusListener(fhandler);
    			
    		tab.show();
    	    }
    	
    	class FocusHandler implements FocusListener
    	{
    		public void focusGained(FocusEvent e)
    
    		{
    			if(e.getSource()== t1)
    			{
    				t1.setText(""); //blank input textfield
    				sumField.setText("");//blank text field
    			}
    			else if(e.getSource() == t2)
    			{
    				t2.setText(""); //blank input textfield
    				sumField.setText("");//blank textfield
    			}
    			else if(e.getSource() == t3)
    			{
    				t3.setText("");//blank input textfield
    				sumField.setText("");//blank field
    			}
    			else if(e.getSource() == sumField)
    			{
    				sumField.setNextFocusableComponent(t1);
    				t1.grabFocus();
    			}
    		}
    		
    		public void focusLost(FocusEvent e)
    		{
    			if(e.getSource()== t1)
    			{
    				t1.setNextFocusableComponent(t2);
    			}
    			if(e.getSource()==t2)
    			{
    				t2.setNextFocusableComponent(t3);
    			}
    			if(e.getSource()==t3)
    			{
    				t3.setNextFocusableComponent(calcBtn);
    			}
    		}
    	}
    	    	
    	    class calcBtnHandler implements ActionListener
    	    {
    	    	public void actionPerformed(ActionEvent e)
    	    	{
    	    		DecimalFormat num =new DecimalFormat(",###.##");
    	    		double  invalue1, invalue2, invalue3 = 0, outvalue;
    	    		String instring;
    	    		//sumField.setEnabled(true); //enable area text field
    	    		instring =t1.getText(); //read input value
    	    		//prevent an entry from an empty string
    	    		if(instring.equals(""))
    	    		{
    	    			instring = "0";
    	    			t1.setText("0");
    	    		}
    	    		invalue1 = Double.parseDouble(instring); //convert to double
    	    		instring =t2.getText(); //read second input
    	    		//prevent an entry from an empty string
    	    		if(instring.equals(""))
    	    		{
    	    			instring = "0";
    	    			t2.setText("0");
    	    		}
    	    		invalue2 = Double.parseDouble(instring); //convert to double
    	    		instring =t3.getText();	//read third input
    	    		//prevent an entry from an empty string
    	    		if(instring.equals(""))
    	    		{
    	    			instring = "0";
    	    			t3.setText("0");
    	    		}
    	    		if(e.getSource() == calcBtn) 
    	    		invalue3 = Double.parseDouble(instring); //convert to double
    	    		outvalue = invalue1 * invalue2 * invalue3;
    	    		sumField.setText(num.format(outvalue));
    	 	    	}
    	    	}
    	  
    	
    	    class extBtnHandler implements ActionListener
    	    {
    	    	public void actionPerformed(ActionEvent e)
    	    	{
    	    		System.exit(0);
    	      	}
    	    }
    	    
    	    public static void main(String args[])
    	    {
    	    	new FinalProject(); //instanciate a GUI object
    	    }
    }
    *Also while it says final project it is not. It is purely extra credit and for practice.

  2. #2
    mr_guy is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    Java Code:
                            DecimalFormat num =new DecimalFormat(",###.##");
    	    		double  invalue1, invalue2, invalue3 = 0, outvalue;
    	    		String instring;
    	    		//sumField.setEnabled(true); //enable area text field
    	    		[COLOR="Red"]instring =t1.getText(); //read input value[/COLOR]
    	    		//prevent an entry from an empty string
    	    		if(instring.equals(""))
    	    		{
    	    			instring = "0";
    	    			t1.setText("0");
    	    		}
    The error is occuring here. The problem is you have created two text fields with the name 't1'. One of them has been initialized and the other hasn't.
    the one that is refered to here has not been initialized.

    You need to fix up the starting peice of code:
    Java Code:
    public class FinalProject extends JFrame 
    {
    	private JTabbedPane tab;
    	private JPanel p1;
    	private JButton calcBtn;
    	private JButton extBtn;
    	[COLOR="Red"]private JTextField t1;
    	private JTextField t2;
    	private JTextField t3;[/COLOR]
    	private JTextField sumField;
    	private JLabel l1;
    	private JLabel l2;
    	private JLabel l3;
    	private JLabel sumLabel;
    	
    	public FinalProject()
    	    {
    	    	super("Final Project"); //call super JFrame constructor
    	    	    	
    	    			    
    	    JTabbedPane tab = new JTabbedPane();
    	   
    	    //define components for tab 1
    	    JLabel l1 = new JLabel("Enter the length of the hot tub:");
    		JLabel l2 = new JLabel("Enter the height of the hot tub:");
    		JLabel l3 = new JLabel("Enter the depth of the hot tub:");
    		[COLOR="Red"]JTextField t1 = new JTextField(5); //Remove the "JTextField" bit at the start of the line
    		JTextField t2 = new JTextField(5);// same here
    		JTextField t3 = new JTextField(5);// and here[/COLOR]
    		JButton calcBtn = new JButton("Calculate Volume");
    		JLabel sumLabel = new JLabel("The hot tub volume is:");
    		JTextField sumField = new JTextField(13);
    		JButton extBtn = new JButton("Exit");
    When you put JTextField in front of those lines you are creating new text fields instead of initializing the old ones

    EDIT: Same goes for all of the labels and buttons that you have made two of
    Last edited by mr_guy; 04-19-2011 at 05:25 AM.

Similar Threads

  1. Volume of a sphere problem
    By dj_ee3 in forum New To Java
    Replies: 8
    Last Post: 02-14-2011, 03:10 AM
  2. Problem with NumberFormatException in calculator
    By kumarv75 in forum CLDC and MIDP
    Replies: 17
    Last Post: 06-28-2010, 01:47 PM
  3. Java calculator problem ???
    By danielmessick in forum Advanced Java
    Replies: 2
    Last Post: 03-13-2010, 07:53 PM
  4. Time Calculator Problem
    By ktisallred in forum New To Java
    Replies: 3
    Last Post: 09-22-2009, 08:15 PM
  5. Calculator Problem. Thanks for helping! ^^
    By clark_sandy in forum New To Java
    Replies: 3
    Last Post: 07-06-2008, 05:01 PM

Posting Permissions

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