Average and standard deviation methods returning as NaN

I'm making a program that gets the averages and standard deviation of the numbers that are imputed in an array. I'm stuck on only one thing though: whenever the average and standard deviation are returned, it shows up as Not a Number. What is wrong with the code?

Here's the methods:

    public mainFrame() {

private double avg;
private double stdDeviation;
private double[] gradeArray;

     * @param gradeArray
     * @param numElem
     * @return
    public double getAverage (double[] gradeArray, int numElem) {
    double sum = 0;

    for(int i=0;i<numElem;i++){
        sum = sum + gradeArray[i];
    return (sum/numElem);        

public double getStdDev (double[] gradeArray, int numElem, double avg){
    double sum = 0;

    for(int i = 0;i<numElem;i++){
        sum = sum + Math.pow((gradeArray[i]-avg),2);
    return Math.sqrt(sum/numElem);

And here is the event code when the "Enter Grades" button is clicked on:

final int SIZE = 25;
double[] gradeArray = new double[SIZE];
String s;
int numElem = 0;
double average = 0;

for(int i=0;i<=SIZE;i++){

    s = (String)JOptionPane.showInputDialog("Enter Grade","");
    if (s == null || s == ("")){

            gradeArray[i] = Double.parseDouble (s);
            average = getAverage(gradeArray,numElem);
            stdDeviation = getStdDev(gradeArray,numElem,avg);
            txtGradeEntered.setText(1 + i + "");
            txtMean.setText(average +"");

        catch (NumberFormatException e){
            JOptionPane.showMessageDialog(null, "Your input must be numeric!","Bad  Data!",0);
        catch (ArrayIndexOutOfBoundsException e){
            JOptionPane.showMessageDialog (null, "You can only input 25 values!","Too much data!",0);



The problem is with division with such statement: return (sum/numElem); where numElm is initialized as0 and sum is also 0 because the following never gets executed:

for(int i=0;i<numElem;i++){ //<---- numElem = 0
        sum = sum + gradeArray[i];

A NaN value is used to represent the result of certain invalid operations such as dividing zero by zero. NaN constants of both float and double type are predefined as Float.NaN and Double.NaN.

So initialize or set this numElem to some other value.

Need Your Help

Active Directory (LDAP) - Check account locked out / Password expired

c# active-directory ldap

Currently I authenticate users against some AD using the following code:

Flying Saucer does not render PNG files in PDF

java pdf-generation flying-saucer xhtmlrenderer

I'm trying to generate a PDF file from XHTML by using the Flying Saucer (9.0.7) library. Texts and tables are rendered as expected but images (PNGs) are not shown in the final PDF. The images are