New to java, need a little help with array input
Hello all,
I am new to programming. I have to take this class for my degree, and we just started getting into OOP(object oriented programming). I wrote a program which is good enough for the assignment, but my professor wanted me to clean it up and use an array of objects and loop to get input. I am having a little trouble with getting input into the array of objects. It compiles fine, but I get a NullPointer exception error when I try to put in input. Here is the program.
import java.util.Scanner; //imports the java util class
public class JonesRickieWeek6Prog2 //opens the class JonesRickieWeek6Prog
{
public static void main(String[] args)
{
Cat[] myCats;
myCats = new Cat[3];
Scanner input = new Scanner(System.in); //intializes the new Scanner object input
int c;
for(c = 0; c < 3; c++)
{
System.out.print("Enter the name of the Cat: ");
String name = input.next();
System.out.print("Enter the age of the Cat: ");
int age = input.nextInt();
System.out.print("Enter the weight of the Cat: ");
double weight = input.nextDouble();
System.out.print("Enter the breed of the Cat: ");
String breed = input.next();
System.out.print("Does the Cat have claws? True or False: ");
boolean declawed = input.nextBoolean();
myCats[c].set(name, age, weight, breed, declawed);
}
for(c = 0; c < 3; c++)
{
if(myCats[c].declawed = true && myCats[c].age > 3)
{
System.out.print("\n\nThe Cats over 3 with claws are:\n\n");
System.out.print("Name: " + myCats[c].name);
System.out.print("Age: " + myCats[c].age + " years old");
}
}
} //ends main method
} //ends class JonesRickieWeek6Prog
The line it is having the issue with is this one:
myCats[c].set(name, age, weight, breed, declawed);