Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Getting NullPointerException in program

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Getting NullPointerException in program

    What I am trying to do is create a bank class (which uses an array to store the accounts) that has a create method which is used to create a new account in the bank. The method requires the user to input data (account number, name, phone number, and balance) and will create a new account in the bank, but the method has to check to see if the account number is already in use and if it is print an
    error message and return (this is where I am having problems).

    I have three files an Account class, a Bank class, and a BankDriver class, posted below are the three classes

     
    public class Account
    {
    	private final double RATE = 0.03; // interest rate of 3.0%
     
    	private long acctNumber;
    	private double balance;
    	private String name;
    	private String phoneNumber;
     
    	public Account ()
    	{
    		name = "";
    		acctNumber = 0000;
    		phoneNumber = "555-1111";
    		balance = 0.0;
    	}
     
    	//---------------------------------------------------------------------------------------------
    	// Creates account by defining the owner, account number, phone number, and starting balance
    	//---------------------------------------------------------------------------------------------
    	public Account (String owner, long account, String phone, double initial)
    	{
    		name = owner;
    		acctNumber = account;
    		phoneNumber = phone;
    		balance = initial;
    	}
     
    	//-----------------------------------------------------------------------
    	// Returns balance
    	//-----------------------------------------------------------------------
    	public double getBalance()
    	{
    		return balance;
    	}
     
    	//----------------------------------------------------------------------
    	// Returns account number
    	//----------------------------------------------------------------------
    	public long getAcctNumber()
    	{
    		return acctNumber;
    	}
     
    	public String toString()
    	{
    		return acctNumber + "\t" + name + "\t" + phoneNumber + "\t" + balance;
    	}
    }

     
    public class Bank
    {
    	private Account[] accounts;
    	private int count = 0;
     
    	public Bank()
    	{
    		accounts = new Account[30];
    	}
     
    	public void create (long accountNum,  String owner, String phNumber, double initial)
    	{
    		if(accountNum != accounts[count].getAcctNumber())
     
    		{
    			accounts[count] = new Account(owner, accountNum, phNumber, initial);
    			count++;
    		}
     
    		else if(accountNum == accounts[count].getAcctNumber())
    			System.out.println("Account Number already exsists.");
     
    	}
     
     
     
    	public String toString()
    	{
    		String report = "";
    		for (int i = 0; i<count; i++)
    			report += accounts[i] + "\n";
     
    		return report;
    	}
    }

     
    public class BankDriver
    {
    	public static void main (String[] args)
    	{
    		Bank bank1 = new Bank();
     
    			bank1.create (1111, "Bob", "555-1234", 100.00);
     
    			System.out.println(bank1);
     
    	}
    }


    The problem I believe is my create method but I am not sure of the best way to fix it. Any help would be greatly appreciated.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,332
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default Re: Getting NullPointerException in program

    You mention a NullPointerException in the title of your post, but no mention of it in your post. Is this the problem? Where is this exception being thrown (recommended you post the full stack trace)?

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Getting NullPointerException in program

    Yes it does seem to be the problem as all three programs have no trouble compiling, below is the error I get when I try to run the program

    Exception in thread "main" java.lang.NullPointerException
    at Bank.create(Bank.java:20)
    at BankDriver.main(BankDriver.java:7)

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,332
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default Re: Getting NullPointerException in program

    if(accountNum != accounts[count].getAcctNumber())

    Step through the code and consider the operations one by one. When the above line is first executed, has accounts[count] yet been instantiated? You should validate this in the code (you can do so simply by either checking for null or checking the index is > 0.)

  5. The Following User Says Thank You to copeg For This Useful Post:

    jmack (January 22nd, 2011)

  6. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Getting NullPointerException in program

    if(accountNum != accounts[count].getAcctNumber())

    For your first time, the array will be created but every slot, including index 0, will be empty. That's causing your Null Pointer Exception I think.

  7. The Following User Says Thank You to javapenguin For This Useful Post:

    jmack (January 22nd, 2011)

Similar Threads

  1. Problem with NullPointerException?
    By scooty199 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: November 6th, 2010, 05:13 PM
  2. [SOLVED] Nullpointerexception
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 14th, 2010, 10:33 PM
  3. [SOLVED] NullPointerException
    By javapenguin in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 1st, 2010, 12:10 AM
  4. NullPointerException
    By bbr201 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 29th, 2010, 07:06 PM
  5. [SOLVED] NullPointerException.CLARIFICATION
    By chronoz13 in forum Exceptions
    Replies: 8
    Last Post: August 28th, 2009, 03:24 PM