0

I'm getting a nullpointerexception in my program and can't figure it out. These are the portions of code it is highlighting.

public int size()

         throws IOException
   {
      return (int) (file.length() / RECORD_SIZE);
   }

public int find(int accountNumber)
         throws IOException
   {
      for (int i = 0; i < size(); i++)
      {
         file.seek(i * RECORD_SIZE);
         int a = file.readInt();         
         if (a == accountNumber) { return i; }
            // Found a match            
      } 
      return -1; // No match in the entire file
   }

I believe this line is the main problem but don't really know what to do.

 int position = data.find(accountNumber);

Exception in thread "main" java.lang.NullPointerException
    at BankData.size(BankData.java:43)
    at BankData.find(BankData.java:79)
    at BankSimulator.main(BankSimulator.java:35)

If you need any other parts of my code to look at just let me know. The purpose of the program is to create a file, create bank accounts and/or delete them. It will then calculate the file size before and after a transaction. Thanks!

Dustin
  • 185
  • 3
  • 12
  • 3
    Looks like the file object is not set before calling find. It might be better to pass in the object and not rely on a member variable. Depends on implementation. – JustinKSU Nov 25 '14 at 15:10
  • 1
    There is only one thing that can be null in the line mentioned in the stack trace. Read http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – JB Nizet Nov 25 '14 at 15:11
  • 1
    Do you have access to a debugger? If so, put in a breakpoint on line 43 for your BankData class and see where the null pointer is occurring. Learning to debug your code will prove invaluable in the future. As for your problem, I imagine your "file" member variable is null when you ask for its length() inside the size() method. – kha Nov 25 '14 at 15:13

0 Answers0