1

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

    }
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38

2 Answers2

1

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.

QBrute
  • 4,405
  • 6
  • 34
  • 40
  • countSick shouldnt be static since it references data that is bound to the village object. – Basti Oct 26 '20 at 18:31
  • Thank you! i did however run into yet another problem this time it has to do with the fact that when i try to use my countSick function by typing. countsick=countSick(); i get an error saying. error: non-static method countSick() cannot be referenced from a static context –  Oct 26 '20 at 18:59
  • @SebastianSjöberg Yes, because you're trying to call the method in a static fashion. You need to call it from your `village` instance. See here: https://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context – QBrute Oct 26 '20 at 19:33
0

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.

Basti
  • 1,117
  • 12
  • 32