Results 1 to 12 of 12
  1. #1
    black06vfr is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default 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);

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Not sure if this will solve the NPE your seeing, but you do have a flaw in your program:
    Java Code:
    if(myCats[c].declawed [B][COLOR="Red"]=[/COLOR][/B] true && myCats[c].age > 3)
    "=" is an assignment... you want to use the comparation sign "==".

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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?

  4. #4
    black06vfr is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default 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;
    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    So does this mean that you've solved your program's problems?

  6. #6
    black06vfr is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    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

  7. #7
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Well, obviously it throws a npe
    You use
    Java Code:
    myCats[c].set(blabla)
    before instantiating a new cat for every position in myCats.

    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.

  8. #8
    black06vfr is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    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;

  9. #9
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    what cat1, cat2, cat3 are?
    are they cat objects?
    you should new a cat object and assign to array element

  10. #10
    black06vfr is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    Problem solved, thanks for the help everyone

  11. #11
    kishan's Avatar
    kishan is offline Member
    Join Date
    Apr 2009
    Posts
    57
    Rep Power
    0

    Default

    how u solved ur problem ?
    can u please send the modifications done to avoid NPE.

  12. #12
    black06vfr is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    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

  1. Array Input
    By Rose88 in forum New To Java
    Replies: 2
    Last Post: 04-19-2009, 10:39 PM
  2. Problem - input in two-dimensional array
    By PVL268 in forum New To Java
    Replies: 5
    Last Post: 03-09-2009, 04:58 AM
  3. input placed in array
    By smilejava in forum New To Java
    Replies: 5
    Last Post: 11-12-2007, 07:29 AM
  4. input placed in array
    By smilejava in forum New To Java
    Replies: 1
    Last Post: 11-05-2007, 12:32 PM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •