1 Attachment(s)
Error Message: Exception in thread "main" java.lang.NullPointerException etc.
Hello, I'm quite obviously new the the forum but joined in hopes of figuring out various problems with my coding. I don't quite understand the error message I'm given (I'm very very new to Java programming) so bear with me, and thank you in advance for any help you are willing to offer.
I get this output:
Code :
Exception in thread "main" java.lang.NullPointerException
at WhileTokenizer.retreiveData(WhileTokenizer.java:55)
at WhileTokenizer.main(WhileTokenizer.java:33)
Here is my complete code:
Code :
import java.io.*;
import java.util.StringTokenizer;
public class WhileTokenizer {
// File input variables
private static FileInputStream inputFile;
private static InputStreamReader inputReader;
private static BufferedReader reader;
// Tokenizer variable
private static StringTokenizer token;
// Variables to define data
private static String line, firstName, lastName;
private static double hourlyWage, overWage, totalPay, totalT,
MonReg, MonPay, MonOver,
TuesReg, TuesOver, TuesPay,
WedsReg, WedsOver, WedsPay,
ThursReg, ThursPay, ThursOver,
FriReg, FriOver, FriPay;
public static void main(String[] args) throws IOException
{
//Methods
initiateFile();
retreiveData();
inputFile.close();
}
public static void initiateFile() throws IOException
{
//Import file to parse
inputFile = new FileInputStream //Change this text file to the one on your computer!
("D:\\...data.txt"); \\I figured you didn't need to see my file location when looking for the error.. The txt file is three lines of data that needs to be parsed. (I will attach it so you can see for yourself).
inputReader = new InputStreamReader(inputFile);
reader = new BufferedReader(inputReader);
}
public static void retreiveData() throws IOException
{
//Data retreival
line = reader.readLine();
System.out.println("Gathered data line = " + line);
while (line != null)
{
//Define variables
firstName = token.nextToken();
lastName = token.nextToken();
hourlyWage = Double.parseDouble(token.nextToken());
MonReg = Double.parseDouble(token.nextToken());
MonOver = Double.parseDouble(token.nextToken());
TuesReg = Double.parseDouble(token.nextToken());
TuesOver = Double.parseDouble(token.nextToken());
WedsReg = Double.parseDouble(token.nextToken());
WedsOver = Double.parseDouble(token.nextToken());
ThursReg = Double.parseDouble(token.nextToken());
ThursOver = Double.parseDouble(token.nextToken());
FriReg = Double.parseDouble(token.nextToken());
FriOver = Double.parseDouble(token.nextToken());
calculateTotal();
printResults();
line = reader.readLine();
}
}
public static void calculateTotal() throws IOException
{
//Data processing
overWage = (hourlyWage * 1.5);
MonPay = (MonReg * hourlyWage);
TuesPay = ((TuesReg * hourlyWage) + (overWage * TuesOver));
WedsPay = ((WedsReg * hourlyWage) + (overWage * WedsOver));
ThursPay = (ThursReg * hourlyWage);
FriPay = ((FriReg * hourlyWage) + (overWage * FriOver));
totalPay = (MonPay + TuesPay + WedsPay + ThursPay + FriPay);
totalPay = Math.round(totalPay*100.000)/100.000;
}
public static void printResults() throws IOException
{
//Show Results
System.out.println("Hello "+ firstName +" "+ lastName +".");
System.out.println("You have made $" + totalPay + " this period.");
}
}
Here is the content of the input text file.
Code :
Kermit D.Frogge 17.25 5.25 0.0 8.0 1.5 8.0 2.25 7.75 0.0 8.0 2.0
Allyson Wonderland 10.18 6.75 0.0 8.0 1.5 8.0 2.25 8.0 3.0 6.5 0.0
Rocky Balboa 14.95 8.0 3.0 8.0 2.25 8.0 2.0 8.0 3.25 8.0 1.25
Juan Valdez 18.65 8.0 4.25 7.75 0.0 8.0 1.5 6.25 0.0 6.0 0.0
Re: Error Message: Exception in thread "main" java.lang.NullPointerException etc.
you r missing one line like this
token = new StringTokenizer(line);
Re: Error Message: Exception in thread "main" java.lang.NullPointerException etc.
Quote:
Exception in thread "main" java.lang.NullPointerException
at WhileTokenizer.retreiveData(WhileTokenizer.java:55 )
At line 55 in WhileTokenizer one of the variables you are using has a null value. Look at that line of code and see if you can determine which variable has a null value. If you can not see which one, add a println statement just before line 55 and print out the values of all the variables used on line 55. Then you can see what variable has a null value. Now backtrack in your code and see why that variable does NOT have a valid value.
Re: Error Message: Exception in thread "main" java.lang.NullPointerException etc.
Thank you both! I was indeed missing my tokenizer. (I miss all the silly things...)
Thanks again :D
Re: Error Message: Exception in thread "main" java.lang.NullPointerException etc.
StringTokenizer is not silly. Camelot is though!
Re: Error Message: Exception in thread "main" java.lang.NullPointerException etc.
Haha no, I mean I myself make silly errors :P