Results 1 to 4 of 4
  1. #1
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Exclamation help with program

    i am trying to create a converter for measurements. like when i input a number it will convert it to inches,feet,yards ect. but when i run the program and put a number into inches or feet textbox it shows a bunch of errors.

    this is what the error show:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Handler.actionPerformed(Handler.java:11)
    at javax.swing.JTextField.fireActionPerformed(Unknown Source)
    at javax.swing.JTextField.postActionEvent(Unknown Source)
    at javax.swing.JTextField$NotifyAction.actionPerforme d(Unknown Source)
    at javax.swing.SwingUtilities.notifyAction(Unknown Source)
    at javax.swing.JComponent.processKeyBinding(Unknown Source)
    at javax.swing.JComponent.processKeyBindings(Unknown Source)
    at javax.swing.JComponent.processKeyEvent(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.KeyboardFocusManager.redispatchEvent(Unkn own Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEv ent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKe yEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAsse rtions(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent (Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown 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)

    here is my Converter class.
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    
    public class Converter extends JFrame {
    	// US measurements 
    	JTextField inch2feet;
    	JTextField feet2inch;
    	JTextField yard2feet;
    	JTextField feet2yard;
    	JTextField mile2yard;
    	JTextField yard2mile;
    	// metric measurements
    	JTextField cm2mm;
    	JTextField mm2cm;
    	JTextField m2cm;
    	JTextField cm2m;
    	JTextField km2m;
    	JTextField m2km;
    	//both us and metric;
    	JTextField inch2meter;
    	JTextField meter2inch;
    	JTextField feet2meter;
    	JTextField meter2feet;
    	JTextField yard2meter;
    	JTextField meter2yard;
    	//other
    	JTextField fahrenheit;
    	JTextField celcius;
    	
    	public Converter() {
    		super("Converter");
    		setSize(375,700);
    		GridLayout flow = new GridLayout(16,4);
    		setLayout(flow);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    		JLabel US = new JLabel("US Measurements");
    		add(US);
    
    		
    		//row 1
    		JPanel row1 = new JPanel();
    		JLabel in = new JLabel("Inches:");
    		row1.add(in);
    		inch2feet = new JTextField(10);
    		row1.add(inch2feet);
    		JLabel ft = new JLabel("Feet:");
    		row1.add(ft);
    		feet2inch = new JTextField(10);
    		row1.add(feet2inch);
    		add(row1);
    		
    		//row 2
    		JPanel row2 = new JPanel();
    		JLabel ft2 = new JLabel("Feet:");
    		row2.add(ft2);
    		feet2yard = new JTextField(10);
    		row2.add(feet2yard);
    		JLabel yd = new JLabel("Yard:");
    		row2.add(yd);
    		yard2feet = new JTextField(10);
    		row2.add(yard2feet);
    		add(row2);
    		
    		//row 3
    		JPanel row3 = new JPanel();
    		JLabel yd2 = new JLabel("Yard:");
    		row3.add(yd2);
    		yard2mile = new JTextField(10);
    		row3.add(yard2mile);
    		JLabel mile = new JLabel("Mile:");
    		row3.add(mile);
    		mile2yard = new JTextField(10);
    		row3.add(mile2yard);
    		add(row3);
    		
    		JLabel metric = new JLabel("Metric Measurements");
    		add(metric);
    		
    		//row 4
    		JPanel row4 = new JPanel();
    		JLabel cm = new JLabel("mm:");
    		row4.add(cm);
    		mm2cm = new JTextField(10);
    		row4.add(mm2cm);
    		JLabel mm = new JLabel("cm:");
    		row4.add(mm);
    		cm2mm = new JTextField(10);
    		row4.add(cm2mm);
    		add(row4);
    		
    		//row 5
    		JPanel row5 = new JPanel();
    		JLabel cm2 = new JLabel("cm:");
    		row5.add(cm2);
    		cm2m = new JTextField(10);
    		row5.add(cm2m);
    		JLabel m = new JLabel("Meter:");
    		row5.add(m);
    		m2cm = new JTextField(10);
    		row5.add(m2cm);
    		add(row5);
    		
    		//row 6
    		JPanel row6 = new JPanel();
    		JLabel m2 = new JLabel("Meter:");
    		row6.add(m2);
    		m2km = new JTextField(10);
    		row6.add(m2km);
    		JLabel km = new JLabel("km:");
    		row6.add(km);
    		km2m = new JTextField(10);
    		row6.add(km2m);
    		add(row6);
    		
    		JLabel both = new JLabel("US and Metric conversion");
    		add(both);
    		
    		//row 7
    		JPanel row7 = new JPanel();
    		JLabel in3 = new JLabel("Inches:");
    		row7.add(in3);
    		inch2meter = new JTextField(10);
    		row7.add(inch2meter);
    		JLabel m3 = new JLabel("Meter:");
    		row7.add(m3);
    		meter2inch = new JTextField(10);
    		row7.add(meter2inch);
    		add(row7);
    		
    		//row 8
    		JPanel row8 = new JPanel();
    		JLabel ft3 = new JLabel("Feet:");
    		row8.add(ft3);
    		feet2meter = new JTextField(10);
    		row8.add(feet2meter);
    		JLabel m4 = new JLabel("Meter:");
    		row8.add(m4);
    		meter2feet = new JTextField(10);
    		row8.add(meter2feet);
    		add(row8);
    		
    		//row 9
    		JPanel row9 = new JPanel();
    		JLabel yd3 = new JLabel("Yard:");
    		row9.add(yd3);
    		yard2meter = new JTextField(10);
    		row9.add(yard2meter);
    		JLabel m5 = new JLabel("Meter:");
    		row9.add(m5);
    		meter2yard = new JTextField(10);
    		row9.add(meter2yard);
    		add(row9);
    		
    		JLabel other = new JLabel("Other");
    		add(other);
    		
    		//row 10
    		JPanel row10 = new JPanel();
    		JLabel far = new JLabel("Fahrenheit");
    		row10.add(far);
    		fahrenheit = new JTextField(10);
    		row10.add(fahrenheit);
    		JLabel cel = new JLabel("Celcius");
    		row10.add(cel);
    		celcius = new JTextField(10);
    		row10.add(celcius);
    		add(row10);
    		
    		Handler ch = new Handler();
    		 inch2feet.addActionListener(ch);
    		 feet2inch.addActionListener(ch);
    		 yard2feet.addActionListener(ch);
    		 feet2yard.addActionListener(ch);
    		 mile2yard.addActionListener(ch);
    		 yard2mile.addActionListener(ch);
    		 cm2mm.addActionListener(ch);
    		 mm2cm.addActionListener(ch);
    		 m2cm.addActionListener(ch);
    		 cm2m.addActionListener(ch);
    		 km2m.addActionListener(ch);
    		 m2km.addActionListener(ch);
    		 inch2meter.addActionListener(ch);
    		 meter2inch.addActionListener(ch);
    		 feet2meter.addActionListener(ch);
    		 meter2feet.addActionListener(ch);
    		 yard2meter.addActionListener(ch);
    		 meter2yard.addActionListener(ch);
    		 fahrenheit.addActionListener(ch);
    		 celcius.addActionListener(ch);
    		
    	}
    	
    	public static void main(String[] args) {
    		Converter ct = new Converter();
    	}
    	
    
    }
    here is my Handler class:
    Java Code:
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
     class Handler implements ActionListener {
    	 Converter con;
    	 String convert;
    	 double conum;
    	 
    	 
    	public void actionPerformed(ActionEvent e) {
    		if(e.getSource()==con.inch2feet){
    			convert = con.inch2feet.getText();
    			conum = Double.parseDouble(convert);
    			conum = conum /12;
    			convert = Double.toString(conum);
    			con.feet2inch.setText(convert);
    			
    		}else if(e.getSource()==con.feet2inch) {
    			convert = con.feet2inch.getText();
    			conum = Double.parseDouble(convert);
    			conum = conum *12;
    			convert = Double.toString(conum);
    			con.inch2feet.setText(convert);
    		}
    		
    		
    	}
     }

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    in Handler, your "Converter con" is never assigned and thus is null :(

    do this:

    Java Code:
    class Handler {
    ...
      public Handler(Converter con) {
        this.con = con;
      }
      ...
    }
    
    class Converter {
    ...
      handler = new Handler(this);
    ...
    }

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

    Default

    The error message is telling you what the problem is and where it's occurring: you're trying to use an object that is null at a specific line of your program.

    The general solution is to look at that line and then figure out why the object there is null. So here's the line:

    Java Code:
    if(e.getSource()==con.inch2feet){

    And as to why it's null -- look to see where you assign an object to this variable. If you check carefully, you'll see that you don't ever do this. So the solution is obvious: assign a valid object to this variable. I suggest you give Handler a constructor which takes a Converter object as its parameter, and then when you're creating a new Hander object, pass the current Converter object (this) to the Handler's constructor. Then in the constructor, assign con the parameter's object. Much luck!

    edit: as per iluxa's demo code above.

  4. #4
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

Similar Threads

  1. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 07:53 PM
  2. Execute A program from a Program!
    By Moncleared in forum Advanced Java
    Replies: 2
    Last Post: 02-22-2009, 05:17 PM
  3. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 09:24 AM
  4. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 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
  •