Results 1 to 12 of 12
- 04-26-2009, 11:43 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 6
- Rep Power
- 0
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);
- 04-26-2009, 11:48 AM #2
Not sure if this will solve the NPE your seeing, but you do have a flaw in your program:
"=" is an assignment... you want to use the comparation sign "==".Java Code:if(myCats[c].declawed [B][COLOR="Red"]=[/COLOR][/B] true && myCats[c].age > 3)
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-26-2009, 12:21 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
And also are you sure that you have implement the correct method in the Cat class with correct number of arguments in the set method?
- 04-26-2009, 04:22 PM #4
Member
- Join Date
- Apr 2009
- Posts
- 6
- Rep Power
- 0
thanks for the advice
I have changed the assignment operator as CJSLMAN suggested. As far as the Cat class for the set method, here is what it looks like. Again, thanks for your help.
public void set(String name, int age, double weight, String breed, boolean declawed)
{
this.name = name;
this.age = age;
this.weight = weight;
this.breed = breed;
this.declawed = declawed;
}
-
So does this mean that you've solved your program's problems?
- 04-26-2009, 05:39 PM #6
Member
- Join Date
- Apr 2009
- Posts
- 6
- Rep Power
- 0
Sorry, I guess my last post was a little misleading. It does still throw the NPE and the same line, even after I changed the assignment operator
- 04-26-2009, 07:16 PM #7
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
Well, obviously it throws a npe
You use
before instantiating a new cat for every position in myCats.Java Code:myCats[c].set(blabla)
I'm not gonna tell you what to do, you should know that. Just to help you learning ;)
If you still don't know, just post :)I die a little on the inside...
Every time I get shot.
- 04-28-2009, 12:08 PM #8
Member
- Join Date
- Apr 2009
- Posts
- 6
- Rep Power
- 0
Well, I have tried a few different ways to get it to work, all still throwing the NPE at line 31. I think I understand what you mean but instantiating a new position for every object in the array, but I am obviously not doing it right. When I try to get it to compile it doesn't work. I have tried it inside and outside of the for loop. Just to be sure, here is what I tried.
myCats[1] = cat1;
myCats[2] = cat2;
myCats[3] = cat3;
- 04-28-2009, 12:18 PM #9
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
what cat1, cat2, cat3 are?
are they cat objects?
you should new a cat object and assign to array element
- 04-28-2009, 05:46 PM #10
Member
- Join Date
- Apr 2009
- Posts
- 6
- Rep Power
- 0
Problem solved, thanks for the help everyone
- 04-29-2009, 06:44 AM #11
how u solved ur problem ?
can u please send the modifications done to avoid NPE.
- 04-29-2009, 05:46 PM #12
Member
- Join Date
- Apr 2009
- Posts
- 6
- Rep Power
- 0
Here is what I had to do.
Cat cat1; //instantiates cat1
Cat cat2; //instantiates cat2
Cat cat3; //instantiates cat3
cat1 = new Cat(); //creates a new cat object
cat2 = new Cat(); //creates a new cat object
cat3 = new Cat(); //creates a new cat object
Cat[] myCats = {cat1, cat2, cat3}; //creates an array of cat objects
Similar Threads
-
Array Input
By Rose88 in forum New To JavaReplies: 2Last Post: 04-19-2009, 10:39 PM -
Problem - input in two-dimensional array
By PVL268 in forum New To JavaReplies: 5Last Post: 03-09-2009, 04:58 AM -
input placed in array
By smilejava in forum New To JavaReplies: 5Last Post: 11-12-2007, 07:29 AM -
input placed in array
By smilejava in forum New To JavaReplies: 1Last Post: 11-05-2007, 12:32 PM -
how to take input and verify input in Java programs
By bilal_ali_java in forum Advanced JavaReplies: 0Last Post: 07-21-2007, 08:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks