So here is the code for my program FileMatch and FileMatchTest. The purpose of this program to read file oldmast.txt which contains account numbers with first and last name and their balance. The program will also read trans.txt which contains transaction information. If the account number from trans.txt matches with oldmast.txt, the balance and the account information will be added in a new file: newmast.txt or else it will be logged in log.txt.
The two programs will compile with no error; however, whenever I run FileMatchTest, there is a null pointer exception error occuring in the method matchRecords in FileMatch. I'm really not sure what's happening. Could someone explain or tell me what I should do?
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.IllegalStateException;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.Formatter;
import java.util.FormatterClosedException;
import com.deitel.ch17.AccountRecord;
public class FileMatch
{
private Scanner old;
private Scanner tran;
private Formatter newm;
private Formatter log;
public void openFile()
{
try
{
old = new Scanner( new File("oldmast.txt") );
tran = new Scanner( new File("trans.txt") );
newm = new Formatter("newmast.txt");
log = new Formatter("log");
}
catch( FileNotFoundException fileNotFoundException)
{
System.err.println("Error opening file.");
System.exit(1);
}
}
public void matchRecords()
{
AccountRecord record[] = new AccountRecord[4];
AccountRecord transaction[] = new AccountRecord[4];
try
{
while(old.hasNext() )
{
for(AccountRecord currentAccount : record)
{
currentAccount.setAccount(old.nextInt() );
currentAccount.setFirstName(old.next() );
currentAccount.setLastName(old.next() );
currentAccount.setBalance(old.nextDouble());
}
}
}
catch(NoSuchElementException elementException)
{
System.err.println("oldmast.txt file improperly formed." );
old.close();
System.exit(1);
}
catch(IllegalStateException stateException)
{
System.err.println("Error reading from file oldmast.txt." );
System.exit(1);
}
try
{
while(tran.hasNext() )
{
for(AccountRecord currentTrans : transaction)
{
currentTrans.setAccount(old.nextInt() );
currentTrans.setBalance(old.nextDouble() );
}
}
}
catch(NoSuchElementException elementException)
{
System.err.println("trans.txt file improperly formed." );
tran.close();
System.exit(1);
}
catch(IllegalStateException stateException)
{
System.err.println("Error reading from file trans.txt." );
System.exit(1);
}
for(AccountRecord currentAccount : record)
{
for(AccountRecord currentTrans : transaction)
{
if(currentAccount.getAccount() == currentTrans.getAccount() )
{
currentAccount.setBalance(currentTrans.getBalance() + currentAccount.getBalance() );
newm.format( "%d %s %s %.2f\n", currentAccount.getAccount(),
currentAccount.getFirstName(), currentAccount.getLastName(), currentAccount.getBalance() );
}
else
{
log.format( "Unmatched transaction record for account number: %.2f\n",
currentTrans.getAccount() );
}
}
}
}
public void closeFile()
{
if (old != null)
old.close();
if (tran != null)
tran.close();
if (newm != null)
newm.close();
if (log != null)
log.close();
}
}
public class FileMatchTest
{
public static void main(String[] args)
{
FileMatch app = new FileMatch();
app.openFile();
app.matchRecords();
app.closeFile();
}
}