Beginner Java: Simple grade average code

389
February 04, 2017, at 1:08 PM

So I've been trying to decipher this thing for about 3hrs now. I've googled, and googled and read many threads here at stackoverflow but had no luck. I'm trying to understand the different parts of the code but I need to ask for individual help at this point. So here's my issue: I'm building a simple grade average program for my first java programming class. I want to save 4 grade inputs, then display an average...and eventually I am going to display letter grades based on that average. I think this error is saying I am not initializing finalGrade. But I'm lost...An explanation of what is happening would be great so I can actually learn this...thanks in advance for any help :)

import java.util.Scanner;
import javax.swing.JOptionPane;
public class GradeAverage{
   public static  Double gradeQ1; //gradeQ are grades for the respective quarters
   public static  Double gradeQ2;
   public static  Double gradeQ3;
   public static  Double gradeQ4;
   public static  String studentName;
   public static  Double finalGrade = ((gradeQ1 + gradeQ2 + gradeQ3 + gradeQ4) / 4);
   public static void main(String args[]) {

   Scanner input = new Scanner(System.in);

   studentName = JOptionPane.showInputDialog(null, "Please enter your first and last name.");
   JOptionPane.showMessageDialog(null, "Thanks " + studentName + ", let's get started!");
   gradeQ1 = Double.parseDouble(JOptionPane.showInputDialog(null, "What was your grade in the first quarter?")); // gets grade and saves it as a double gradeQ1
   JOptionPane.showMessageDialog(null, "You entered " + gradeQ1);
   //double gradeQ1 = input.nextDouble();
   gradeQ2 = Double.parseDouble(JOptionPane.showInputDialog(null, "What was your grade in the second quarter?")); 
   JOptionPane.showMessageDialog(null, "You entered " + gradeQ2);
   gradeQ3 = Double.parseDouble(JOptionPane.showInputDialog(null, "What was your grade in the third quarter?")); 
   JOptionPane.showMessageDialog(null, "You entered " + gradeQ3);
   gradeQ4 = Double.parseDouble(JOptionPane.showInputDialog(null, "What was your grade in the fourth quarter?")); 
   JOptionPane.showMessageDialog(null, "You entered " + gradeQ4);
   JOptionPane.showMessageDialog(null, "Thanks " + studentName + ", Your average was " + finalGrade);
   }
}`

JGRASP error:
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
    at GradeAverage.<clinit>(GradeAverage.java:15)
READ ALSO
Programmatic integration of Spring 4 and jersey [duplicate]

Programmatic integration of Spring 4 and jersey [duplicate]

This question already has an answer here:

410
Hadoop how to do CopyMerge in Hadoop 3.0

Hadoop how to do CopyMerge in Hadoop 3.0

I know hadoop version 27's FileUtil has the copyMerge function that merges multiple files into a new 1

666
How can we bundle Java Webserver (Tomcat) and NW.js together?

How can we bundle Java Webserver (Tomcat) and NW.js together?

How can we bundle any Java Server or have Java backend for NWjs for developing desktop applications? Ideally the idea is need to run the webserver from the bundled application an NW

426
Java How to check all possible patterns in ArrayList without repeating cells?

Java How to check all possible patterns in ArrayList without repeating cells?

I am trying to 7 random values from an array greater than 7, and find all possible outcomes without repeating cells

337