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);
     
    }
}
int g3 = g1 + g2;

What is this statement supposed to do? What are the values of g1 and g2?

that statement is supposed to add g1 and g2, and i want the values of g1 and g2 to be whatever someone puts into a text box thing

The problem is that when the statement(line 11) where the NPE occurs, the values of g1 and g2 are null and can't be added together. You need to add them together AFTER you put values into them.

So how would I do that? I dont know the values because the values are gonna be whatever someone puts in the box, right?

So how would I do that

Can you tell in your program that the user has input the values for summing?
Don't do the summing until you have both values.

g1 = Integer.parseInt("Enter 1st Grade: ");

You need to read the API doc for the parseInt() method. This will not work.
API doc at http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/

Member Avatar for coil

Basically, in Java, order matters. What you're doing is:

<declare integers as null>
<assign null value to third integer>
<get input>

You need to do:

<declare integers as null>
<get input>
<assign input to the integers>
<now assign value to third integer>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.