"java.lang.NoSuchMethodError: main" and "fatal exception occured."
This code gives me those error messages when I run it.
Code :
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);
}
}
}
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.
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.
Code :
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";
}
}
}}