Note that there are some explanatory texts on larger screens.

plurals
  1. POStoring data into Object Array Elements returns NullPointerException
    primarykey
    data
    text
    <p>Code : </p> <pre><code>import java.io.*; class Customer { String name; int ID; int purchasequantity; double purchaseprice; Customer() { name = ""; ID = 0; purchasequantity = 0; purchaseprice = 0.0; } } </code></pre> <hr> <pre><code>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&lt;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&lt;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); } } </code></pre> <p>Output : </p> <pre><code>Size of Array 3 Enter Customer Name : Nirav Exception in thread "main" java.lang.NullPointerException at StoreSell.main(StoreSell.java:38) </code></pre> <p>Explanation : </p> <ol> <li>The said program doesn't throw any compilation error.</li> <li>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.</li> <li>I have tried to store the data retrieved from Console into a temp variable (Not an array element), and it stores correctly.</li> <li>Hence, I can conclude that the problem occurs only when it tries to store data into an array object.</li> <li>However, array is created successfully. I have tried to print the array length. and it gives correct length.. 3.</li> </ol> <p>Please help me on this, I have tried to google lot on it, but not able to find any fix for the same.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload