Have error with variable being null when it shouldn’t Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Have error with variable being null when it shouldn’t without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am getting an error

Exception in thread "main" java.lang.NullPointerException
at Proj5.main(Proj5.java:61)

When run the program in lines 61 (char z = placeHolder.charAt(counter) 😉 I cannot figure out what is wrong and is causing the null.

String answers = new String("112211324135412") ;
    int k = 0 ;                                     
    int[] correct = new int[15] ;                   
    String placeHolder = ("112211324135412") ;
    for ( int i = 1 ; i < tokens.length ; i+=2)     
    {
        int counter = 0 ;                           


        int amountCorrect = 0 ;                     
        placeHolder = tokens[i] ;

    while (counter < 15)                            

    {   



        char z = placeHolder.charAt(counter) ; 
        char c = answers.charAt(counter) ;
        if( z == c) 
        {
            amountCorrect++ ;
        } // end if
        counter++ ;                                 

    }   // end while    

    correct[k] = amountCorrect ;                    
    k++ ;


    }   

This is the Full Code.

/** * * * * */

import java.util.* ;
import java.io.* ;
public class Proj5 
{
public static void main(String[] args) throws IOException
{
Scanner s = new Scanner(System.in) ;
/* This piece asks for the name of the file with the ids and answers and if it is not
entered correctly it says invalid and makes the user try again. */  
String fileCheck ;
File file = null ;
do{
System.out.print("Enter the name of the File: ") ;
fileCheck = s.nextLine() ;
file = new File(fileCheck) ;
if (!file.exists())
System.out.println("ERROR, INVALID FILE") ;
}while (!file.exists()) ;
/* This piece opens connections with the file, splits up each line, and then puts the 
pieces into an array (tokens). */
String[] tokens = new String[100] ; 
tokens= information(fileCheck) ;
/* This piece contains the answers string and compares each character of answers to the 
odd numbered elements(answers from file) from the tokens array and keeps track of the 
number correct which is then placed into an array. */   
String answers = new String("112211324135412") ;
int k = 0 ;                                     // used to help store into correct array.
int[] correct = new int[15] ;                   // stores amount correct can be used to compare to WID
String placeHolder = ("112211324135412") ;
for ( int i = 1 ; i < tokens.length ; i+=2)     // adds two so it skips WID
{
int counter = 0 ;                           // used to pull characters 
//in the strings
int amountCorrect = 0 ;                     // keeps track of amount correct
placeHolder = tokens[i] ;
while (counter < 15)                            // goes through all characters in 
//the string of answers
{   
//puts answers into a string so 
//individual answers can be compared
char z = placeHolder.charAt(counter) ; 
char c = answers.charAt(counter) ;
if( z == c) 
{
amountCorrect++ ;
} // end if
counter++ ;                                 // moves to next char
}   // end while    
correct[k] = amountCorrect ;                    // stores amount correct in array for later use.
k++ ;
}   // end for
/* This piece takes "correct" array and loops it through to determine what percentage and 
what letter grade each elements would get and plugs each of those into their own array 
(percentage and letterGrade). */    
int halfToken = (tokens.length/2) ;                 // only need half the amount in tokens array since we dont need to include WID.
double[] percentage = new double[halfToken] ;
int a = 0 ;                                     //used to step through the 3 arrays
char[] letterGrade = new char[halfToken] ;
while(a < halfToken)
{   
if(correct[a] == 15)
{
percentage[a] = 100.0 ;
letterGrade[a] = 'A' ;
a++ ;
}   
else if(correct[a] == 14)
{
percentage[a] = 93.3 ;
letterGrade[a] = 'A' ;
a++ ;
}
else if(correct[a] == 13)
{
percentage[a] = 86.7 ;
letterGrade[a] = 'B' ;
a++ ;
}
else if(correct[a] == 12)
{
percentage[a] = 80.0 ;
letterGrade[a] = 'B' ;
a++ ;
}
else if(correct[a] == 11)
{
percentage[a] = 73.3 ;
letterGrade[a] = 'C' ;
a++ ;
}
else if(correct[a] == 10)
{
percentage[a] = 66.7 ;
letterGrade[a] = 'D' ;
a++ ;
}
else if(correct[a] == 9)
{
percentage[a] = 60.0 ;
letterGrade[a] = 'D' ;
a++ ;
}
else if(correct[a] == 8)
{
percentage[a] = 53.3 ;
letterGrade[a] = 'F' ;
a++ ;
}
else if(correct[a] == 7)
{
percentage[a] = 46.7 ;
letterGrade[a] = 'F' ;
a++ ;
}
else if(correct[a] == 6)
{
percentage[a] = 40.0 ;
letterGrade[a] = 'F' ;
a++ ;
}
else if(correct[a] == 5)
{
percentage[a] = 33.3 ;
letterGrade[a] = 'F' ;
a++ ;
}
else if(correct[a] == 4)
{
percentage[a] = 26.7 ;
letterGrade[a] = 'F' ;
a++ ;
}
else if(correct[a] == 3)
{
percentage[a] = 20.0 ;
letterGrade[a] = 'F' ;
a++ ;
}
else if(correct[a] == 2)
{
percentage[a] = 13.3 ;
letterGrade[a] = 'F' ;
a++ ;
}
else if(correct[a] == 1)
{
percentage[a] = 6.7 ;
letterGrade[a] = 'F' ;
a++ ;
}
else if(correct[a] == 0)
{
percentage[a] = 0.0 ;
letterGrade[a] = 'F' ;
a++ ;
}
} // end while
/* This piece prints the header for id/#correct/%correct/grade and then cycles in a loop 
prints out each students info. */   
int f = 0 ;
int g = 0 ;
System.out.println("Student ID t" + "# Correct t" + "% Correct t" + "Grade") ;
while(f < tokens.length)
{
System.out.println(tokens[f] + "t" + correct[g] + "t" + percentage[g] + "t" + letterGrade[g]) ;
f+=2 ;
g++ ;
} // end while
/* This piece finds adds up the percentage array and then divides by its length which is 
the class avg grade. It then uses this percentage to determine the letter grade. It 
lastly prints out the total average and letter grade. */    
double totalAVG = 0.0 ;
char totalGrade = 'A' ;
while(f<percentage.length) // calculates average percent
{
totalAVG = (totalAVG+percentage[f]) ;
} // end while
totalAVG = totalAVG/percentage.length ;
if(totalAVG >= 90.0)
{
totalGrade = 'A' ;
}
else if (totalAVG >= 80.0 && totalAVG <=89.9)
{
totalGrade = 'B' ;
}
else if (totalAVG >= 70.0 && totalAVG <=79.9)
{
totalGrade = 'C' ;
}
else if (totalAVG >= 60.0 && totalAVG <=69.9)
{
totalGrade = 'D' ;
}       
else 
totalGrade = 'F' ;
System.out.println() ;
System.out.println("Average: " + totalAVG + "% (" + totalGrade + ")" );
/* This next piece cycles through the correct array to find the max and then takes that 
times 2 to get high score, then it prints it out. */    
int highScore = 0 ;
for(int h = 0; h<correct.length ; h++) 
{
if(correct[h] >= highScore)
{
highScore = correct[h] ;
}
} // end for
System.out.println("High Score: " + highScore*2) ;
/* This piece cycles through the correct array to find the min and then takes that times 
2 to get the low Score, then it prints it out. */       
int lowScore = 0 ;
for(int h = 0; h<correct.length ; h++) 
{
if(correct[h] <= lowScore)
{
lowScore = correct[h] ;
}       
} // end for
System.out.println("Low Score: " + lowScore*2) ;
PrintWriter pw = new PrintWriter(new FileWriter("Result.txt")) ;
for (int t=0 ; t< tokens.length ; t+=2 )
{   
g = 0 ;
pw.println(tokens[t] + "," + correct[g] + percentage[g] + letterGrade[g]) ;
g++ ;
}
pw.println("Average: " + totalAVG + "% (" + totalGrade + ")") ;
pw.println("High Score: " + highScore*2) ;  
pw.println("Low Score: " + lowScore*2) ;
pw.close() ;    
} // end main
/*
*
* Opens connection to file with ids and answers and returns them split up. 
* @param (String a) pulls in the filename for use in method
* @return Returns an array containing the split-up file. */
public static String[] information(String a) throws IOException
{   
Scanner inFile = new Scanner (new File(a)) ; // opens connection with file
String[] quarters = new String[100] ;
int index = 0 ;
while (inFile.hasNext())                        // loops while more lines in file
{
String line = inFile.nextLine() ;           // brings in next line to be broken up
String[] array = line.split(",") ;
quarters[index] = array[0]  ;           //stores lines into array tokens
index++ ;
quarters[index] = array[1] ;
index++ ;
}       
inFile.close() ;                            // close connection to file
return quarters ;
} // end information
} // end class  

Answer

My previous answer was incorrect. I misread your code.

The problem is that you are getting null values out of the tokens array, I just didn’t isolate the cause properly.

You code starts out like this:

    String[] tokens = new String[100] ; 
tokens= information(fileCheck) ;

The first line allocates a 100 element string array … and the second line throws away the array you just allocated (huh?) and replaces it with a new one created by the information method.

In the information method, you allocate another 100 element array of Strings, and fill it with stuff read from the file. This is where the problem starts. You see, if you only read (say) 30 tokens, the remaining 70 elements will be null.

Then in the main method you do this:

for ( int i = 1 ; i < tokens.length ; i+=2)     // adds two so it skips WID
{
...
placeHolder = tokens[i];
...
placeHolder.charAt(...)

So the problem is that you are assuming that all tokens in the array are not null. But in fact, the method that fills in the tokens, DOES NOT guarantee to fill all slots.

I’ll leave it to you to figure out the solution. (It is YOUR homework!) But here are a couple of ideas:

  • Use a list instead of an array
  • Figure out (somehow) where the tokens in the array stop.
We are here to answer your question about Have error with variable being null when it shouldn’t - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji