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);
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);
}
}
输出:
Size of Array 3
Enter Customer Name :
Nirav
Exception in thread "main" java.lang.NullPointerException
at StoreSell.main(StoreSell.java:38)
说明:
- 的所述节目d不会抛出任何编译错误。
- 在运行程序时,如果在控制台上为Name输入数据,它可以从控制台获取数据,但无法存储到数组对象中。
- 我试图将从控制台检索到的数据存储到一个临时变量(不是数组元素)中,并且它存储正确。
- 因此,我可以断定只有当它试图将数据存储到数组对象时才会出现问题。
- 但是,数组已成功创建。我试图打印数组的长度。它给出了正确的长度.. 3.
请帮我对此,我试图谷歌很多,但无法找到任何相同的修复。
雅的工作..谢谢.. :) – 2011-04-24 11:21:49