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