Geeks Talk

Prepare for your Next Interview




Create an array of object

This is a discussion on Create an array of object within the Java forums, part of the Software Development category; Hi I am MCM Student I want to create an array of object which will accepts the more than one record I tried following code but its giving nullpointer Exception. ...


Go Back   Geeks Talk > Software Development > Java

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 09-10-2007
Junior Member
 
Join Date: Jun 2007
Location: India
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
vasundhara.jagdale is on a distinguished road
Create an array of object

Hi I am MCM Student

I want to create an array of object which will accepts the more than one record I tried following code but its giving nullpointer Exception. Please tell me how should i solve this problem

import java.io.*;


class Account
{
private String name;
private int ano;
private int atype;
private double abal;

public void getdata(String n, int no, int type, double bal)
{
name = n;
ano = no;
atype = type;
abal = bal;
}

public void display()
{


System.out.println("Account No: " + ano);
System.out.println("Customer Name: " + name);
if (atype == 1)
{
System.out.println("Account type: Saving A/C");
System.out.println("Facilty: Withdrawal");
}
else
{
System.out.println("Account type : Current A/C");
System.out.println("Facility : Cheque Book");
}

System.out.println("Account balance : " + abal);
}

}

class Acc_demo
{
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

public static void main(String arg[]) throws IOException
{
Account a[] = new Account[2];

System.out.println("Enter records: ");

for (int i = 0; i < 2; i++)
{
System.out.println("Enter Customer Name: ");
String nm = br.readLine();

System.out.println("Enter A/C No: ");
int no = Integer.parseInt(br.readLine());

System.out.println("Enter Account type:");
System.out.println("1 - Saving Account");
System.out.println("2- current Account");
int at = Integer.parseInt(br.readLine());

System.out.println("Enter your last balance: ");
int bal = Integer.parseInt(br.readLine());

a[i].getdata(nm,no,at,bal);
a[i].display();
}
}
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-11-2007
Banned
 
Join Date: Nov 2005
Posts: 45
Thanks: 1
Thanked 7 Times in 6 Posts
ejazazeem79 is on a distinguished road
Smile Re: Create an array of object

code through null pointer exception because a[i] is not initalized with any Account object.

Add the following line

a[i] = new Account();

before

a[i].getdata(nm, no, at, bal);
a[i].display();



hope this will help.


Regards
Reply With Quote
Reply

  Geeks Talk > Software Development > Java


Thread Tools
Display Modes


Similar Threads

Thread Thread Starter Forum Replies Last Post
convert one dimansional array to two dimansional array Geek_Guest C and C++ 6 12-22-2007 03:17 AM
static array or dynamic array? rpgubba C and C++ 6 12-22-2007 03:03 AM
WinRunner not identified object , what 2 do other than virtual object wizard method. jayant_gajbhiye WinRunner 6 10-16-2007 06:23 AM
Why should we create a separate detail object Geek_Guest Data Warehousing 0 05-09-2007 01:54 PM
Create Object JobHelper Java 2 11-30-2006 12:45 AM


All times are GMT -4. The time now is 09:10 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved