File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes java.lang.Nullpointer Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java.lang.Nullpointer Exception" Watch "java.lang.Nullpointer Exception" New topic
Author

java.lang.Nullpointer Exception

Nick Verma
Greenhorn

Joined: Oct 27, 2004
Posts: 9
I have a bank class, one customer class, one accounts class having two derived classes: Savingaccount and Checking account in a package banking. and then there is a TestBanking class outside this package. Everything works fine until the insertion of this code:

Account accounts[];
int numberOfAccounts = 0;

public Customer(){
accounts = new Account[100];
}

public void addAccount(Account a){
//account = a;
//accounts[numberOfAccounts++] = account;
//accounts[numberOfAccounts++] = new Account(a);
accounts[numberOfAccounts++] = a;
}

public int getNumOfAccounts(){
return numberOfAccounts;
}

public Account getAccount(int ac){
return accounts[ac];
}

After this segment of code is inserted compilation is fine but at run time the following error is thrown.

G:\Java\SCJP\mod06\exercise2>java TestBanking
Exception in thread "main" java.lang.NullPointerException
at banking.Customer.addAccount(Customer.java:21)
at TestBanking.main(TestBanking.java:20)

Please advise. The whole code is too big to be pasted here......but in case it is needed I can paste that.
Thanks in advance
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hi, Nick -

Can you post the code at TestBanking.main(TestBanking.java:20)? (This is the invocation of the method in your test program.) Perhaps you're passing a null pointer? Make sure the argument is initialized before passing it.


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Mahesh Bhatt
Ranch Hand

Joined: Sep 15, 2004
Posts: 88
Hi there,

G:\Java\SCJP\mod06\exercise2>java TestBanking
Exception in thread "main" java.lang.NullPointerException
at banking.Customer.addAccount(Customer.java:21)
at TestBanking.main(TestBanking.java:20)


I am not giving you the complete answer because I need you to trace the problem (learning from mistakes is best way of learning in java). Please go through the exception, It contains lot of information,

if you look at :
Exception in thread "main" java.lang.NullPointerException,
It tells you what kind of exception has occoured.

if you look at :
at banking.Customer.addAccount(Customer.java:21)
at TestBanking.main(TestBanking.java:20)
It tells you wher the exception has occoured. It tells you the location with the line number.

One clue is, NullPointerException occours when you try to use an object (can be an array) without initializing it. Here may be that either you have not initialized the array before using (that means there are all default null values in the object/array and you are referring to that) or may be that you have a reference variable which points to a null value.
try to find out the problem and if that doesnt help. Please feel free to post again and i will give you the detailed answer. However the key is that "you are using something that points to a null".

Hope it helps.
Regards
Prashant
[ October 28, 2004: Message edited by: prashant bhogvan ]

Impossible is I M Possible
Nick Verma
Greenhorn

Joined: Oct 27, 2004
Posts: 9
I tried to print the value of "ac" being received by addaccount and found out that the following set of code gives the error

bank.addCustomer("Maria", "Soley");
customer = bank.getCustomer(3);
// Maria and Tim have a shared checking account
customer.addAccount(bank.getCustomer(2).getAccount(1));
customer.addAccount(new SavingsAccount(150.00, 0.05));.............in TestBanking.java
and

public Account getAccount(int ac){
return accounts[ac];
}

and there is another overloaded method

public Account getAccount(){
return account;
} in the same class.
......in Customer.java as the new error being thrown is
G:\Java\SCJP\mod06\exercise2>java TestBanking
banking.SavingsAccount@3fbdb0
banking.CheckingAccount@3e86d0
banking.CheckingAccount@50169
banking.SavingsAccount@1fcc69
banking.CheckingAccount@253498
Exception in thread "main" java.lang.NullPointerException
at banking.Customer.getAccount(Customer.java:30)
at TestBanking.main(TestBanking.java:35)
Nick Verma
Greenhorn

Joined: Oct 27, 2004
Posts: 9
Perhaps this will provide a clear picture

package banking;

public class Customer{

private String firstName;
private String lastName;
private Account account;

Account accounts[];
int numberOfAccounts = 0;

public Customer(){
accounts = new Account[100];
}

public void addAccount(Account a){
System.out.println(a);
//account = a;
//accounts[numberOfAccounts++] = account;
//accounts[numberOfAccounts++] = new Account(a);
//accounts[numberOfAccounts++] = a;
}

public int getNumOfAccounts(){
return numberOfAccounts;
}

public Account getAccount(int ac){
return accounts[ac];
}


public Customer(String f,String l){
firstName = f;
lastName = l;
}

public String getFirstName(){
return firstName;
}

public String getLastName(){
return lastName;
}

public Account getAccount(){
return account;
}

public void setAccount(Account acct){
account = acct;
}

};

and

/*
* This class creates the program to test the banking classes.
* It creates a set of customers, with a few accounts each,
* and generates a report of current account balances.
*/

import banking.*;
import java.text.NumberFormat;

public class TestBanking {

public static void main(String[] args) {
NumberFormat currency_format = NumberFormat.getCurrencyInstance();
Bank bank = new Bank();
Customer customer;

// Create several customers and their accounts
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomer(0);
customer.addAccount(new SavingsAccount(500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00, 400.00));

bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomer(1);
customer.addAccount(new CheckingAccount(200.00));

bank.addCustomer("Tim", "Soley");
customer = bank.getCustomer(2);
customer.addAccount(new SavingsAccount(1500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00));

bank.addCustomer("Maria", "Soley");
customer = bank.getCustomer(3);
// Maria and Tim have a shared checking account
customer.addAccount(bank.getCustomer(2).getAccount(1));
customer.addAccount(new SavingsAccount(150.00, 0.05));

// Generate a report
 
 
subject: java.lang.Nullpointer Exception
 
Similar Threads
Constructor problem?
Frustrating- logic error, but where?
Unmarshalling Error while registering an object in RMIRegistery
Threads
Aggregation/Composition code example