Why am I getting the Null pointer exception? This code scans through several files and takes out information on the popularity and the meanings of the names the user types in. The graph in the changingGraph method is yet to be finished. I marked where the exceptions are specifically (two places).
Exception in thread "main" java.lang.NullPointerException
at BabyNames.findingStatistics(BabyNames.java:63)
at BabyNames.main(BabyNames.java:23)
import java.util.*;
import java.io.*;
import java.awt.*;
public class BabyNames{
public static final int STARTINGYEAR = 1890;
public static final int WIDTH = 60;
public static final int HEIGHT = 30;
private static String nameFinal;
public static void main(String[] args) throws FileNotFoundException{
Scanner console = new Scanner(System.in);
DrawingPanel panel = new DrawingPanel(780,560);
Graphics g = panel.getGraphics();
Scanner nameFile = new Scanner(new File("names.txt"));
Scanner meaningsFile = new Scanner(new File("meanings.txt"));
Scanner nameFile2 = new Scanner(new File("names2.txt"));
intro();
fixedGraph(g);
String nameFinal = nameToLowerCase(console);
String meanings = "";
String popularity = "";
if(STARTINGYEAR == 1890){
popularity = findingStatistics(console,nameFile); //EXCEPTION HERE
}
else{
popularity = findingStatistics(console, nameFile2);
}
meanings = findingStatistics(console, meaningsFile);
changingGraph(meanings,g,popularity);
}
//prints introduction to what the program does
public static void intro(){
System.out.println("This program allows you to search through the");
System.out.println("data from the Social Security Administration");
System.out.println("to see how popular a particular name has been");
System.out.println("since" + STARTINGYEAR );
System.out.println();
System.out.print("Name: ");
}
//Converts what the user types in so the first letter is
//capitalized and the rest is lower case
public static String nameToLowerCase(Scanner console){
String originalName = console.next();
String name = "" ;
int lengthOfName = originalName.length();
String beginingOfName = originalName.substring(0,1).toUpperCase();
String endOfName = originalName.substring(1,lengthOfName).toLowerCase();
name = beginingOfName + endOfName;
return name;
}
public static String findingStatistics(Scanner console, Scanner data){
boolean goesThroughOnce = false; //stops thhe while statement when the word has been found.
String statistics = "";
String currWord = "";
String currLine = "";
while (data.hasNext() && goesThroughOnce == false){
currLine = data.nextLine();
Scanner lineBeingRead = new Scanner(currLine); //make other scanners?? for each file
currWord = lineBeingRead.next();
if (currWord.equals(nameFinal) || currWord.equals(nameFinal.toUpperCase())){ //EXCEPTION HERE
statistics = currLine;
goesThroughOnce = true;
System.out.println(statistics);
}
else{
}
}
if(goesThroughOnce == false){
System.out.print(nameFinal + " not found"); //make it so it's the actual original
}
return statistics;
}
public static void fixedGraph(Graphics g){ //Draws fixed things such as gray blocks and black lines
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0,0,780,HEIGHT);
g.fillRect(0,560-HEIGHT,780,HEIGHT);
g.setColor(Color.BLACK);
g.drawLine(0,HEIGHT,780,HEIGHT);
g.drawLine(0,560-HEIGHT,780,560-HEIGHT);
}
public static void changingGraph(String meanings, Graphics g, String popularity){
// String meanings = findingStatistics(console,meaningsFile);
//String popularity = findingStatistics(console,nameFile);
g.drawString("" + meanings,0,16); //draws meaning text
int startingYear = STARTINGYEAR;
int amountOfDecades = 0;
if(startingYear == 1890){
amountOfDecades = 13;
}
else{
amountOfDecades = 8;
}
g.drawString("" + startingYear,0,552); //fencepost
for(int i=0; i<=amountOfDecades;i++){
int year = startingYear + (10 * i);
g.drawString("" + year,(WIDTH*i),552); //draws decade numbers
}
Scanner popularityData = new Scanner(popularity);
String currChar = popularityData.next();
boolean gender = false; //if it is a boys name
if(currChar.equals("f")){
gender = true;
}
if(gender == true){
g.setColor(Color.PINK);
} //determines which color the bars will be
else{
g.setColor(Color.BLUE);
}
}
}