user14373185
user14373185

Reputation:

problem where I try to add a class to an array and it only give me null

I have an assignment where I am supposed to use classes to build a village population in an array and where every instance in the array is a special class which at the moment only contains a boolean value determining if they are sick or not but it only adds null to the array.

import java.lang.reflect.Array;
import java.util.Random;
public class Start {
    public static void main(String[] args) {
        Village village = new Village();

    }
}

class Village {
    final int SIZE = 1000;
    Person[] population = new Person[SIZE];
    Village() {
        for (int i = 0; i < SIZE; i = i + 1) {
            Person personen = new Person();
            Array.set(population, i, personen);
        }
    }
    static int countSick; {
        int sjuka = 0;
        for (Person personen: population) {
            boolean checker = personen.isSick;
            if (checker == true) {
                sjuka = sjuka + 1;
            }
        }
    }

}
class Person {
    boolean isSick;
    final double INIT_SICK_PROB = 0.32;
    Person() {
        Random rand = new Random();
        double checker = rand.nextDouble();
        if (checker <= INIT_SICK_PROB) {
            isSick = true;
        } else {
            isSick = false;
        }

    }
}

Upvotes: 1

Views: 76

Answers (2)

QBrute
QBrute

Reputation: 4534

Look at your class Village:

class Village {
    final int SIZE = 1000;
    Person[] population = new Person[SIZE];
    Village() {
        for (int i = 0; i < SIZE; i = i + 1) {
            Person personen = new Person();
            Array.set(population, i, personen);
        }
    }
    static int countSick; {
        int sjuka = 0;
        for (Person personen: population) {
        boolean checker = personen.isSick;
            if (checker == true) {
                sjuka = sjuka + 1;
            }
        }
    }

}

These lines static int countSick; {...} create a static variable countSick followed by a code block.

This code block is an initializer block and will be run before the constructor. That means,

for (Person personen: population) {

will be accessed before it is populated by the constructor and therefore throws a NullPointerException.

You probably wanted to make countSick a method. For that you need to change the line to

public int countSick() { 

instead. That's how a method definition looks. Also, since you probably want to access the population array from a Village instance, you need to remove the static modifier.

Also, don't forget to return the value sjuka at the end of the method, otherwise you'll get a compiler error.

Upvotes: 1

Basti
Basti

Reputation: 1190

Your main-Method currently only creates a new Village. Its array should be initialized and filled since this happens in its constructor. But the values are never accessed after the initialization.

The field "countSick" is never accessed and is also declared static inside Village an therefore class bound, while the field population is object bound. i believe this is meant to be a method that returns the count?

Your architecture has flaws in general which i would guess result from missing knowledge about object orientation and the Java language?

  • First initialize the village
  • Then fill its array with people
  • Then access the array through the instance of the village to run your check on sick people

This means you need 2 Entity-Class Village and Person. A method that can process the villages array to read the sick count. And your Main-Class that declares the start of your process.

Upvotes: 0

Related Questions