Storing data into Object Array Elements returns NullPointerException
Code :
import java.io.*;
class Customer
{
String name;
int ID;
int purchasequantity;
double purchaseprice;
Customer()
{
name = "";
ID = 0;
purchasequantity = 0;
purchaseprice = 0.0;
}
}
class StoreSell
{
public static void main(String args[]) throws IOException
{
Customer[] cst = new Customer[3];
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
double totalAmount = 0;
System.out.println("Size of Array " + cst.length);
for (int i=0;i<cst.length;i++)
{
System.out.println("Enter Customer Name : ");
cst[i].name = br.readLine();
cst[i].ID = 100 + (i+1);
System.out.println("Customer ID Generated is : "+cst[i].ID);
System.out.println("Enter Purchase Price per Piece : ");
//String price = br.readLine();
//System.out.println("Entered Price is " +price);
开发者_JAVA技巧 cst[i].purchaseprice = Double.parseDouble(br.readLine());
System.out.println("Enter Purchase Quantity : ");
cst[i].purchasequantity = Integer.parseInt(br.readLine());
}
System.out.println(" Customer ID " + "Customer Name " + "Price Per Piece " + "Quntity " + "Bill Amount ");
for(int i=0;i<cst.length;i++)
{
System.out.print(cst[i].ID + " " +cst[i].name+" "+ cst[i].purchaseprice + " " + cst[i].purchasequantity);
double tempAmount = StaticMethod.calculateStatic(cst[i].purchaseprice, cst[i].purchasequantity);
totalAmount = totalAmount + tempAmount;
System.out.println(" " + tempAmount);
}
System.out.println("Total Sell of the day : Amount : " + totalAmount);
}
}
Output :
Size of Array 3
Enter Customer Name :
Nirav
Exception in thread "main" java.lang.NullPointerException
at StoreSell.main(StoreSell.java:38)
Explanation :
- The said program doesn't throw any compilation error.
- While running the program, when data is entered on console for Name, it is able to fetch the data from console, but not able to store into array object.
- I have tried to store the data retrieved from Console into a temp variable (Not an array element), and it stores correctly.
- Hence, I can conclude that the problem occurs only when it tries to store data into an array object.
- However, array is created successfully. I have tried to print the array length. and it gives correct length.. 3.
Please help me on this, I have tried to google lot on it, but not able to find any fix for the same.
Customer[] cst = new Customer[3];
This creates the array, not the individual elements, you need to create those yourself, e.g. in the loop:
for (int i=0;i<cst.length;i++)
{
cst[i] = new Customer();
Initialising an array populates all positions within the array with the default value, and for an array of objects the default value is null. So the following code:
Customer[] cst = new Customer[3];
creates the following array:
{null, null, null}
There are numerous ways of initialising arrays, but if you're definately only going to be using a 3 element array just go for this one:
Customer cst[] = {new Customer(), new Customer(), new Customer()};
This is a guess* Call me crazy but does this, cst[i].ID = 100 + (i+1); actually increment i?
精彩评论