• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.lang.Nullpointer Exception

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Nick Verma
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic