I am new in java and I've got a problem while working with arrays of objects. My main program is like this:
package bicycledemo;
class BicycleDemo {
public static void main(String[] args) {
// Create two different
// Bicycle objects with an array
Bicycle[] bike = new Bicycle[2];
bike[0].cadence=50; //line 10, where the NullPointerException prompts out
bike[0].gear=2;
bike[1].cadence=10;
bike[1].gear=3;
System.out.println("gear: "+bike[0].gear+"\n"+"cadence: "+bike[0].cadence+"\n");
System.out.println("gear: "+bike[1].gear+"\n"+"cadence: "+bike[1].cadence+"\n");
System.out.println("\b");
}
}
and the Bicycle class is this one:
package bicycledemo;
public class Bicycle {
public Bicycle() {
}
public int cadence;
public int gear;
}
When I run the program, the output error is:
Exception in thread "main" java.lang.NullPointerException
at bicycledemo.BicycleDemo.main(BicycleDemo.java:10)
Java Result: 1
I suppose that what happens is that the object bike is not correctly created, but I don't see why.
Thank you very much for your help! I am really desperate to resolve this!