In the following code the getProperty() method always returns null, even if I add a default for
loadNumberOfAccounts();
so its not null, the next method which uses getProperty() will return null. storing the properties works flawlessly.
prop.get
also returns null.
public class AccountList {
private static Properties prop = new Properties();
private static final File DIRECTORY = Constants.DIRECTORY;
private static final File BANK_ACCOUNTS_FILE = new
File(DIRECTORY.getName() + File.separator + "Accounts.txt");
private static final FileOutputStream WRITER = assignWriter();
private static final FileInputStream READER = assignReader();
private static final List<Bank> ACCOUNT_LIST = new ArrayList<>();
private static final String NUMBER_OF_ACCOUNTS = "NumberOfAccounts";
private static final String BANK_ACCOUNT_NAME = "BankAccount";
private static final String SEPERATOR = "-";
private static int accounts = 0;
static {
BANK_ACCOUNTS_FILE.setReadOnly();
}
private static FileInputStream assignReader() {
FileInputStream tmp = null;
try {
tmp = new FileInputStream(BANK_ACCOUNTS_FILE);
} catch (FileNotFoundException e) {
EvaluateErrors.eveluateException(e);
}
if (tmp == null) {
EvaluateErrors.eveluateException(new NullPointerException());
}
return tmp;
}
private static FileOutputStream assignWriter() {
BANK_ACCOUNTS_FILE.setWritable(true);
FileOutputStream tmp = null;
try {
tmp = new FileOutputStream(BANK_ACCOUNTS_FILE);
} catch (IOException e) {
EvaluateErrors.eveluateException(e);
}
if (tmp == null) {
EvaluateErrors.eveluateException(new NullPointerException());
}
BANK_ACCOUNTS_FILE.setReadOnly();
return tmp;
}
public static void loadAccounts() {
try {
prop.load(READER);
} catch (IOException e) {
EvaluateErrors.eveluateException(e);
}
loadNumberOfAccounts();
loadAccountsList();
for (Bank object : ACCOUNT_LIST) { //to try and debug
System.out.println(object.getName());
System.out.println(object.getId());
System.out.println(object.getBankAccountFunds());
System.out.println(object.getOverdrawFee());
}
}
public static void saveAccounts() {
BANK_ACCOUNTS_FILE.setWritable(true);
saveAccountsList();
saveNumberOfAccounts();
try {
prop.store(WRITER, null);
} catch (IOException e) {
EvaluateErrors.eveluateException(e);
} finally {
CloseLogs();
BANK_ACCOUNTS_FILE.setReadOnly();
}
private static void loadNumberOfAccounts() {
String string = prop.getProperty(NUMBER_OF_ACCOUNTS);
System.out.println(string); // always null
try {
accounts = Integer.parseInt(string);
} catch (NumberFormatException e) { // this exception is thrown
// because string is always null.
EvaluateErrors.eveluateException(e);
}
// more methods ...
i'm sure the answer is quite simple and I have just missed it, but I have exhausted all options.
Accounts.txt looks like this
NumberOfAccounts=1
OtherStoredProperties=etc
etc...
UPDATE
I have found the bug but do not know why it's happening, nor how to fix it.
Turns out that if I change
private static final WRITER = assignWriter();
to
WRITER = null
the getProperty()
method works flawlessly. I obviously get a NullPointerException
when attempting to store the property file because WRITER = null
, however, that's an easy fix. What I don't understand is, why does assigning the WRITER
cause the getProperty()
method to only return null?