Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: "java.lang.NoSuchMethodError: main" and "fatal exception occured."

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default "java.lang.NoSuchMethodError: main" and "fatal exception occured."

    This code gives me those error messages when I run it.


    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JLabel;
     
    public class TempConverter extends JFrame{
     
    	private JButton button1;
    	private JButton button2;
    	private JTextField field1;
    	private JLabel label1;
    	String input = field1.getText();
    	String string = "";
    	public TempConverter(){
    		super("Temperature converter");
    		setLayout(new FlowLayout());
     
     
     
    		button1 = new JButton("°C to °F");
    		add(button1);
    		button2 = new JButton("°F to °C");
    		add(button2);
    		field1 = new JTextField(5);
    		add(field1);
    		String input = field1.getText();
     
    		label1 = new JLabel(string);
    		add(label1);
     
    		EventHandler handler = new EventHandler();
    		button1.addActionListener(handler);
    		button2.addActionListener(handler);
     
    	}
     
    	public class EventHandler implements ActionListener{
     
    		public void actionPerformed(ActionEvent event){
     
    			try{
    			if(event.getSource() == button1)
    				string =  Double.toString((Double.parseDouble(input) - 32) * 5 / 9);
     
    			if(event.getSource() == button2)
    				string = Double.toString(Double.parseDouble(input) * 9 / 5 + 32);
    			}
    			catch(NumberFormatException exception){
    				string = "ERROR";
    			}
    		}
     
    	public void main(String [] args){
     
    		TempConverter bucky = new TempConverter();
    		bucky.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		bucky.setSize(400,80);
    		bucky.setVisible(true);
    	}
    }
    }


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: "java.lang.NoSuchMethodError: main" and "fatal exception occured."

    your main method doesnt have static modifier, and also your main method is inside the inner class.

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: "java.lang.NoSuchMethodError: main" and "fatal exception occured."

    I've put the static modifier in front of the main method now, and I think it's outside the inner class..
    Still I get: "Exception in thread "main" java.lang.NullPointerException
    at TempConverter.<init>(TempConverter.java:15)
    at TempConverter.main(TempConverter.java:40)"
    When I run it.

    I skipped all the import statements this time.

    import java.awt.FlowLayout;
     
    public class TempConverter extends JFrame{
     
    	private JButton button1;
    	private JButton button2;
    	private JTextField field1;
    	private JLabel label1;
    	String input = field1.getText();
    	String string = "";
     
    	public TempConverter(){
    		super("Temperature converter");
     
    		setLayout(new FlowLayout());
    		button1 = new JButton("°C to °F");
    		add(button1);
    		button2 = new JButton("°F to °C");
    		add(button2);
    		field1 = new JTextField(5);
    		add(field1);
    		String input = field1.getText();
     
    		label1 = new JLabel(string);
    		add(label1);
     
    		EventHandler handler = new EventHandler();
    		button1.addActionListener(handler);
    		button2.addActionListener(handler);
     
    	}
     
    	public static void main(String [] args){
    		TempConverter bucky = new TempConverter();
    		bucky.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		bucky.setSize(400,80);
    		bucky.setVisible(true);
    	}
     
     
     
     
     
     
    	public class EventHandler implements ActionListener{
     
    		public void actionPerformed(ActionEvent event){
     
    			try{
    			if(event.getSource() == button1)
    				string =  Double.toString((Double.parseDouble(input) - 32) * 5 / 9);
     
    			if(event.getSource() == button2)
    				string = Double.toString(Double.parseDouble(input) * 9 / 5 + 32);
    			}
    			catch(NumberFormatException exception){
    				string = "ERROR";
    			}
    		}
     
     
    	}}

Similar Threads

  1. Replies: 16
    Last Post: August 27th, 2010, 03:30 PM
  2. Replies: 2
    Last Post: March 23rd, 2010, 01:38 AM
  3. Please help! Exception in thread "main" java.lang.NullPointerException
    By Arutha2321 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 18th, 2009, 02:25 AM
  4. Replies: 1
    Last Post: October 25th, 2009, 11:54 AM
  5. Replies: 4
    Last Post: June 18th, 2009, 09:23 AM