Exception in thread "main" java.lang.NullPointerException on Java Array -


i'm getting error code:

exception in thread "main" java.lang.nullpointerexception @ mainclass.main(mainclass.java:20)

could identify error, think has initializing array?

mainclass.java

public class mainclass {   public static void main(string[] args) {      //dummy vars simulate user input     double price = 2.75;      //declare array of wincalcs      wincalc[] staging1;     staging1 = new wincalc[100];       (int x=0; x<staging1.length; x++ ) {         staging1[x].price = price;         staging1[x].quantity = x+1;         staging1[x].calctotal();         }    } 

}

wincalc.java

public class wincalc {  public double price; public double quantity; public double total;  public wincalc () {     price= 0;     quantity = 0;     total = 0; }  public void calctotal() {     this.total = price * quantity; } 

}

you forgot create objects

for (int x=0; x<staging1.length; x++ ) {     staging1[x] = new wincalc();        // ... } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -