Hey folks,
I am working on a getBalance method that retrieves a balance from a CSV file. I keep getting:

java.lang.NullPointerException
	at java.util.StringTokenizer.<init>(StringTokenizer.java:182)
	at java.util.StringTokenizer.<init>(StringTokenizer.java:204)
	at atmEngine.getBalance(atmEngine.java:100)
	at atmEngine.<init>(atmEngine.java:81)
	at Launcher.main(Launcher.java:12)

I can't figure this one out! I threw in some console print outs to make sure it was getting the data correctly and it was outputting the following:

1111,25145.25
1111
2222,1200.12
2222
3333,99.52
3333
4444,1000000000
4444
null

which is all 100% correct (minus the null). In the CVS file there are Account Numbers (1111, 2222, etc) and then right after each account is the balance of that account.

Here is my code for the method.

public double getBalance(int accountNumber) throws IOException
    {
        String accountFile="accounts/accounts.csv";
        double balance=-1.0;
        int accountTranslation=0;
        boolean accountVerified=false;
        
        FileReader freader = new FileReader(accountFile);
        BufferedReader outputFile = new BufferedReader(freader);
        
        String str="404";
        
        while((str!=null) || (accountTranslation==accountNumber))
        {
            str = outputFile.readLine();
            System.out.println(str);

            StringTokenizer strTokenizer = new StringTokenizer(str, ",");
            accountTranslation = Integer.parseInt(strTokenizer.nextToken());
            System.out.println(accountTranslation);
            if(accountTranslation==accountNumber)
            {
                balance=Double.parseDouble(strTokenizer.nextToken());
                accountVerified=true;
            }
            else
            {
                accountVerified=false;
            }
        }
        if(!accountVerified)
            JOptionPane.showMessageDialog(null, "Account does not exist");
        return balance;
    }

I'd give you all my code but theres about 1500 lines of it in a bunch of classes.

Anyway, all ideas are appreciated!

Thanks PO

After reading a line from the file can you check whether it is null, before passing it to the stringtokenizer.
That can prevent this "Null pointer exception."

Hope it helps!!!

Also another suggestion as far as using StringTokenizer is considered, to quote the javadocs :-

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

So always make it a habit to go through the Javadocs if you are using Any class for the first time.

WOOT Got it not just oneway but BOTH ways.

@legilimen
I saw what you said and I put an if statement around the everything in the while and it worked (after a few modifications)

@stephen84s
I saw your post about how tokenizer is considered legacy so i figured for the sake of me being update to date I should learn it and use it, if it's legacy now when i graduate it will be ancient! haha!

Thanks Guys!!!

here is the finished product...with troubleshooting outputs :P

public double getBalance(int accountNumber) throws IOException
    {
        String accountFile="accounts/accounts.csv";
        double balance=-1.0;
        int accountTranslation=0;
        boolean accountVerified=false;
        
        FileReader freader = new FileReader(accountFile);
        BufferedReader outputFile = new BufferedReader(freader);
        
        String str="404";
        
        while((str!=null)&&(accountVerified!=true))
        {
            str = outputFile.readLine();
            System.out.println(str);
            if ((str!=null) || (accountTranslation==accountNumber))
            {
                String[] tokens = str.split(",");
                accountTranslation = Integer.parseInt(tokens[0]);
                
                System.out.println("Account# from file"+accountTranslation+" Account# from user"+accountNumber);
                if(accountTranslation==accountNumber)
                {
                    balance=Double.parseDouble(tokens[1]);
                    accountVerified=true;
                }
                else
                {
                    accountVerified=false;
                }
                System.out.println(balance+" "+accountVerified);
            }
        }
        if(accountVerified==false)
            JOptionPane.showMessageDialog(null, "Account does not exist");
        return balance;
    }
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.