THIS IS A PROGRAM THAT WILL OPEN A WINDOW, THEN WHEN YOU CLICK CALCULATE GPA, A JOPTIONPANE THING WILL OPEN AND YOU WILL PUT YOUR GRADE IN, THAT WILL HAPPEN 2 MORE TIMES THEN THE LAST ONE WILL SHOW YOU YOUR GRADE AVERAGE.
WHEN I COMPILE THIS PROGRAM, EVERYTHING IS FINE, BUT WHEN I PRESS EXECUTE, IT DOES NOT RUN MY PROGRAM IT JUST SAYS:
Exception in thread "main" java.lang.NullPointerException
at Menus2.<init>(Menus2.java:12)
at Menus2.main(Menus2.java:67)
Process completed.
PLEASE HELP ME, THIS PROBLEM IS PROBABLY SOMETHING STUPID, BUT I CANT SEEM TO FIGURE IT OUT
THANK YOU,
A. DAVIS
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Menus2 extends JFrame implements ActionListener{
JButton k1, k2;
Integer g1 =(null);
Integer g2 =(null);
int g3 = g1 + g2;
public Menus2(){
this.setSize(400,300);
this.setTitle("GPA Calculator");
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setLayout(new FlowLayout());
Container content = getContentPane();
k1 = new JButton("Close Program");
k1.setActionCommand("Exit");
k2 = new JButton("Calculate GPA");
k2.setActionCommand("Name");
k1.addActionListener(this);
k2.addActionListener(this);
content.add(k1, BorderLayout.NORTH);
content.add(k2, BorderLayout.WEST);
}
public void actionPerformed(ActionEvent e){
if((e.getActionCommand()).equals("Exit")) {
System.exit(0);
}
if((e.getActionCommand()).equals("Name")) {
g1 = Integer.parseInt("Enter 1st Grade: ");
g2 = Integer.parseInt("Enter 2st Grade: ");
g3 = Integer.parseInt("Enter 3st Grade: ");
JOptionPane.showMessageDialog(null, "Your GPA is " + g3);
}
}
public static void main(String[] args) {
Menus2 project = new Menus2();
project.setVisible(true);
}
}