Search code examples
javanullhashmapjform

My hashmap is only putting null values


To help myself learn java, i am creating a blackjack program using a JForm GUI that include accounts that you can create and keep a running balance you use to bet on each game. I have a BlackJackApp.JForm class which is the main class. The accounts are stored in a .txt file and are read using an Account class containing readFile and writeFile methods. I created a sample txt file, named Accounts.txt, with these values:

John Doe>>1000000

Jack Black>>1

Bob Dole>>987654321

(there are no empty spaces between lines in the actual txt file)

I have a method that reads the text file and attaches these values to a HashMap. This is the code i have for the method.

public void readFile()
{
   accountsMap.clear();
   BufferedReader br;

   try
   {    
     br = new BufferedReader(new FileReader("Accounts.txt"));
     String nextLine = br.readLine();

        while(nextLine != null)
        {
           String lineString[] = nextLine.split(">>");
           Integer accountBalance = Integer.parseInt(lineString[1]);
           System.out.println(lineString[0] + " " + lineString[1] + " " + 
           accountBalance);
           accountsMap.put(lineString[0], accountBalance); 
           nextLine = br.readLine();  
        }
        br.close();
   }
   catch(IOException frex)
   {System.out.println("An error has occurred while reading the file");}
}

This is the relevant code i have for the JForm class, with only the top portion included for reference

public class BlackjackApp extends javax.swing.JFrame {

Account a = new Account();
String account;
Integer accountBalance;
HashMap accountsMap = a.getAccountsMap();


public void fillAccountNameBox()
{

   for (int i = 0; i < accountsMap.size(); i++)
   {
       accountNameBox.addItem((String) accountsMap.get(i));
   }


}

public BlackjackApp() {
    initComponents();
    a.readFile();
    fillAccountNameBox(); //fills comboBox component w/ list of hashMap keys
    System.out.println(accountsMap.keySet());
    for(int i = 0; i < accountsMap.size(); i++)
        System.out.println(accountsMap.get(i));

}

The system.out.println code is for debugging. This is the output:

John Doe 1000000 1000000
Jack Black 1 1
Bob Dole 987654321 987654321
[Bob Dole, John Doe, Jack Black]
null
null
null

My question is this: why is my hashmap putting in the correct keys, but leaving their values null? The lineString array is populating correctly, and so is the Integer accountBalance, but when it comes to actually putting the key/value pairs into the hashmap, it only puts the keys in and it leaves their values null even though accountBalance is not null. Why is this? I have tried searching numerous threads for advice relating to this issue but none of their advice worked for me. There must be something that im overlooking, but as a beginner its hard for me to recognize where the problem lies.


Solution

  • The problem relys on the way you are printing the information. The Map.get method expects you pass a parameter with a key, whose value you want. In the for loop, you are asking for the values attached to the keys 0, 1 and 2 - hence the null values. Change your code to this:

    for(String key : accountsMap.keySet())
        System.out.println(accountsMap.get(key));
    

    As you can see, I also changed the for loop structure to use a for-each.

    Hope this helps you.