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. |
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. ...
|
|||||||
|
|||
|
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(); } } |
| Sponsored Links |
|
|||
|
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 |
![]() |
|
| 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 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 |