Results 1 to 4 of 4

Thread: JMenu help

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

    Question JMenu help

    the program was working fine until i added some menus. it showed no errors in the program itself but in the console these errors showed.

    Exception in thread "main" java.lang.NullPointerException
    at Converter.<init>(Converter.java:54)
    at Converter.main(Converter.java:224)

    here is the code
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.*;
    import java.awt.*;
    
    
    public class Converter extends JFrame {
    	// menu
    	JMenuBar bar;
    	JMenu menu;
    	JMenuItem resetapp;
    	JMenuItem exit;
    	JMenu help;
    	// buttons
    	JButton clear;
    	// 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 fahrenheit2c;
    	JTextField celcius2h;
    	 
    
    	
    	public Converter() {
    		super("Converter");
    		setSize(375,700);
    		GridLayout flow = new GridLayout(16,4);
    		setLayout(flow);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		menu = new JMenu("File");
    		help = new JMenu("Help");
    		resetapp = new JMenuItem("Reset");
    		exit = new JMenuItem("Exit");
    		menu.add(resetapp);
    		menu.add(exit);
    		bar.add(menu);
    		bar.add(help);
    		add(bar);
    		setVisible(true);
    		
    		//row 0
    		JPanel row0 = new JPanel();
    		clear = new JButton("reset");
    		row0.add(clear);
    		add(row0);
    		
    		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);
    		inch2meter = 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);
    		fahrenheit2c = new JTextField(10);
    		row10.add(fahrenheit2c);
    		JLabel cel = new JLabel("Celcius");
    		row10.add(cel);
    		celcius2h = new JTextField(10);
    		row10.add(celcius2h);
    		add(row10);
    		
    		Handler ch = new Handler(this);
    		 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);
    		 fahrenheit2c.addActionListener(ch);
    		 celcius2h.addActionListener(ch);
    		 clear.addActionListener(ch);
    		
    	}
    	
    	public static void main(String[] args) {
    		Converter ct = new Converter();
    	}
    	
    
    }
    one of the errors is in line 54
    Java Code:
    bar.add(menu);
    the other is in line 224
    Java Code:
    Converter ct = new Converter();
    what did i do wrong.

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Java Code:
    bar.add(menu);
    So bar must be null. Look back at where you declare and initialize it.

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

    Default

    thanks a lot

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

Similar Threads

  1. link between two JFrame using JMenu
    By JavaEbony in forum NetBeans
    Replies: 8
    Last Post: 03-09-2011, 12:19 PM
  2. unwanted bug. JMenu
    By ocean in forum New To Java
    Replies: 7
    Last Post: 11-20-2009, 10:29 PM
  3. Using a JMenu as a real option
    By nfh in forum AWT / Swing
    Replies: 3
    Last Post: 11-09-2009, 06:04 PM
  4. JMenu to JPopupMenu
    By carderne in forum New To Java
    Replies: 0
    Last Post: 02-28-2009, 07:07 PM
  5. JMenu and JRadioButtonMenuItem
    By doron70 in forum AWT / Swing
    Replies: 3
    Last Post: 07-18-2007, 07:13 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
  •