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();
	
	}
}

Recommended Answers

All 3 Replies

paste us the error message. the line that the error occurred on would be good information to know.

Exception in thread "main" java.lang.NullPointerException
at FileMatch.matchRecords(FileMatch.java:46)
at FileMatchTest.main(FileMatchTest.java:8)

AccountRecord record[] = new AccountRecord[4];

You created an empty array with 4 elements. Each of those elements is by default null. Then you said:

currentAccount.setAccount(old.nextInt() );

Which attempts to call a method on 'currentAccount', which is an AccountRecord from the array record[] (that you declared earlier). Therefore currentAccount is 'null' and you cannot call a method on something which is null. The correct way to populate the record[] array would be to use a for loop and in the for loop create a

new AccountRecord();

and then set record = new AccountRecord(). Basically I'm saying you need to use a constructor to make a new object, then put that object into the array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.