Geeks Talk

Prepare for your Next Interview


Welcome to the Geeks Talk forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

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 Blogs FAQ Tag Cloud Calendar Mark Forums Read

Java Java related Issues and Problems

Reply

 

LinkBack Thread Tools Display Modes
  #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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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 04:17 AM
static array or dynamic array? rpgubba C and C++ 6 12-22-2007 04:03 AM
WinRunner not identified object , what 2 do other than virtual object wizard method. jayant_gajbhiye WinRunner 6 10-16-2007 07:23 AM
Why should we create a separate detail object Geek_Guest Data Warehousing 0 05-09-2007 02:54 PM
Create Object JobHelper Java 2 11-30-2006 01:45 AM


All times are GMT -4. The time now is 05:11 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved