I am in dire need of assistance please. I have a class called Fields and I wish to create an Array of Field objects but when I execute the code below:
static Field[] theField;
static Scanner userInput = new Scanner(System.in);
static void createFields()
{
System.out.print("Enter the number of fields required: ");
int numFields = userInput.nextInt();
theField = new Field[numFields];
for (int i = 0; i < numFields; i++)
{
System.out.print("Enter a name for field " + (i + 1) + ": ");
String name = userInput.nextLine();
theField[i].setFieldName(name);
}
}
Then I get the following output and error in the console:
Enter the number of fields required: 3
Enter a name for field 1: Exception in thread "main" java.lang.NullPointerException
at TestChart.createFields(TestChart.java:44)
at TestChart.main(TestChart.java:14)
Please can you guys help resolve the error. I have been trying since last night to no avail.