0

I want to know how do you access an attribute in a class inside an array like an example below:

import java.util.*;

public class DogTest{
    public class Dog {
        int Quantity;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Dog dogs[] = new Dog[15];

        for ( int i = 1;  i <=15; i++){
            System.out.println("Enter number of Dogs ");
            dogs[i].Quantity = scan.nextInt();
        }
    }
}

The code above does not seem to work. dogs[i].Quantity is derived from my C++ knowledge by the way.

Error msg:

Exception in thread "main" java.lang.NullPointerException

Is my structure wrong? Or there is another way to do it?

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 4
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – SomeJavaGuy Jan 09 '17 at 12:29
  • 4
    `new Dog[15];` initializes the values at each array position with it´s default value, which is `null` for `Objects` – SomeJavaGuy Jan 09 '17 at 12:30

2 Answers2

0

Arrays start at position 0. So at end of your loop you try to access dogs[15] which does not exist. Essentially an array of size 15 is accessed by numbers 0-14. This may be the problem. Try starting loop like this

for(int i=0;i<15;i++)
{
}
mikekane
  • 13
  • 8
0

First of all declare a class for itself, not as an inner class like you did.Never give fields first uppercase letter, that is naming convention.

public class Dog{
int quantity;
}

And, your actual problem is that when you declare an array of dogs, you declared an array of size, in your case, 15 but it doesn't contain any objects. You just initialised and array which holds 15 nulls and can be filled with Dog objects. And because that you get a null pointer exception.So, first you should fill your array with the dog objects, something like this:

for (int i = 0; i < dogs.length; i++){
dogs[i] = new Dog(); // calls a constructor for Dog object
}

And, after that, you can access your objects trough for loop to change a field quantity

for(int i = 0; i < dogs.length; i++){
dogs[i].quantity = i;
}

Also, I would recommend to make your fields private and make getter and setter methods for accessing and changing their value.

Edit: And yes, mikekane was right about the array size, you would get an ArrayIndexOutOfBoundsException just after you fix this problem with the code you've tried to solve it...

lmilunovic
  • 191
  • 1
  • 10
  • I can't use "dogs[i] = new Dog();" since No enclosing instance of type DogTest is accessible. Must qualify the allocation with an enclosing instance of type DogTest (e.g. x.new A() where x is an instance of DogTest). – Brandon Ian P. Penalosa Jan 09 '17 at 13:14
  • Did you make your class Dog inner class of the DogTest for purpose? Like class within a class... – lmilunovic Jan 09 '17 at 13:35
  • The Dog class is inside the DogTest. DogTest is the main file name – Brandon Ian P. Penalosa Jan 09 '17 at 13:36
  • Well, put it outside unless you are learning how the inner classes work, and I don't think you are because you still aren't sure how array work in java. – lmilunovic Jan 09 '17 at 13:38
  • You can use `static` modifier for class `Dog` like `public static class Dog`. But it is better to use an other file for class `Dog`. – J. Tennié Jan 09 '17 at 14:16