Null Pointer exception (Again !!)
Hi everyone I have a null pointer exception. I'm still not exactly sure how these work but this one is only on a string comparison so I'm really confused. Here is the code would appreciate being pointed in the right direction.
Code:
package GarageSystem;
import java.io.*;
public class GarageUpdate {
public static void main (String [ ] args ) {
// note file names coded into program these can also be passed in as parameters
// Initialise variables
// Opening Master File Data
String omReg = " ";
String omName = " ";
String omAddress = " ";
char omType = ' ';
double omBalance = 0.0;
// Closing Master File Data
// used as an output buffer
String cmReg = " ";
String cmName = " ";
String cmAddress = " ";
char cmType = ' ';
double cmBalance = 0.0;
// Transaction Reg and Balance
String transReg = " ";
double transValue = 0.0D;
try{ // Try block
// Set up Opening Master File for input
FileReader inputFile1 = new FileReader( "om.txt" );
BufferedReader br1 = new BufferedReader( inputFile1);
StreamTokenizer om = new StreamTokenizer( br1);
// Set up Closing Master File for output
FileWriter outputFile = new FileWriter( "closingMaster.txt" );
BufferedWriter cm = new BufferedWriter( outputFile);
// Set up Transaction File for input
FileReader inputFile2 = new FileReader( "trans.txt" );
BufferedReader br2 = new BufferedReader( inputFile2);
StreamTokenizer tr = new StreamTokenizer( br2);
// Read the first Opening Master record
om.nextToken();
omReg = om.sval;
om.nextToken ();
omName = om.sval;
om.nextToken ();
omAddress = om.sval;
om.nextToken ();
omType = (char)om.nval;
om.nextToken ();
omBalance = om.nval;
// Copy to output buffer
cmReg = omReg;
cmName = omName;
cmAddress = omAddress;
cmType = omType;
cmBalance = omBalance;
// Loop thru and process all records in the Transaction File
while (tr.nextToken () != StreamTokenizer.TT_EOF){
// Read a record from the Transaction File
tr.nextToken();
transReg = tr.sval;
tr.nextToken();
transValue = tr.nval;
// If the (transReg != omReg) the current customer
// has no transactions today
// Write the output buffer to the Closing Master File
// and read the next record from the Opening Master File
//***************************************************************************************
//***************************************************************************************
// Eclipse says the exception is in line below
//***************************************************************************************
//***************************************************************************************
while (! transReg.equals(omReg)){
cm.write( cmReg + "\t" + cmName + "\t" + cmAddress + "\t" + cmType + "\t" + cmBalance);
cm.newLine();
// Read a record from the Opening Master File
om.nextToken();
omReg = om.sval;
om.nextToken ();
omName = om.sval;
om.nextToken ();
omAddress = om.sval;
om.nextToken ();
omType = (char)om.nval;
om.nextToken ();
omBalance = om.nval;
// Copy to output buffer
cmReg = omReg;
cmName = omName;
cmAddress = omAddress;
cmType = omType;
cmBalance = omBalance;
}
// Customer has had a transaction today
// Update the customers balance
cmBalance = cmBalance + transValue;
// System.out.println(cmBalance);
cm.write( cmReg + "\t" + cmName + "\t" + cmAddress + "\t" + cmType + "\t" + cmBalance);
cm.newLine();
} // End of Transaction File
// Write last customer to have a transaction today
// to the Closing Master File
// cm.write( cmReg + "\t" + cmName + "\t" + cmAddress + "\t" + cmType + "\t" + cmBalance);
// cm.newLine();
// Process any records left in the Opening Master File
while (om.nextToken () != StreamTokenizer.TT_EOF){
// Read a record from the Opening Master File
om.nextToken();
omReg = om.sval;
om.nextToken ();
omName = om.sval;
om.nextToken ();
omAddress = om.sval;
om.nextToken ();
omType = (char)om.nval;
om.nextToken ();
omBalance = om.nval;
// Copy to output buffer
cmReg = omReg;
cmName = omName;
cmAddress = omAddress;
cmType = omType;
cmBalance = omBalance;
// Write output buffer to the Closing Master File
cm.write( cmReg + "\t" + cmName + "\t" + cmAddress + "\t" + cmType + "\t" + cmBalance);
cm.newLine();
}
// Close all files
br1.close();
cm.close();
br2.close();
} // End of try
// Catch any exceptions thrown during processing
catch (IOException ioe){ // Catch block
System.err.println( ioe);
return;
} // End of catch
} // End of main()
}