JAVA программа дает ошибку java.lang.NullPointerException

Так как я новичок то за тупые ошибки простите)

Задание такое: Есть два типа пользователя (два типа потока). Один - Хозяин, имеет в своем арсенале список вещей (Вещь: цена и вес), второй - Вор, имеет рюкзак (Рюкзак: предельный вес, который может в себя вместить). Поток Хозяина выполняет работу по выкладыванию вещей в квартиру. Поток Вора - забирает вещи из квартиры. При этом Вор должен забрать такие вещи, чтобы их ценность была максимальной и вес их должен быть меньше предельного веса, который может поместиться в рюкзак.

Объектные модели:

  1. Вещь; атрибуты: вес, ценность
  2. Хозяин; атрибуты: Вещи; действия: внести вещи в квартиру
  3. Рюкзак; атрибуты: предельный вес
  4. Вор; атрибуты: рюкзак. Действия: сложить вещи в рюкзак.

Ограничения:

  1. Если работает поток Хозяина, то вор не должен класть вещи в рюкзак.
  2. Если работает Вор, то Хозяин не может войти в квартиру

Возможные ограничения системы:

  1. Хозяев может быть 1..n.
  2. потоки Хозяев БЕЗ взаимной блокировки: несколько хозяев могут выкладывать вещи в квартиру одновременно
  3. Воров может быть 1..m.
  4. Потоки Воров со ВЗАИМНОЙ блокировкой: воровать одновременно может только 1 вор."

Мой код

public class Main {
    public static Apartament apartment = new Apartament();
    public static void main(String[] args) {
        Sync sync = new Sync();
        ExecutorService ex = Executors.newFixedThreadPool(2);
        ex.execute(new MyOwner());
        //ex.execute(new MyOwner());
        ExecutorService ex2 = Executors.newFixedThreadPool(2);
        ex2.execute(new MyThief());
        //ex2.execute(new MyThief());
    }
}
class Sync{
    
}
class Thing {
    int cost;
    int weight;
    public Thing() {
            Random rand = new Random();
            this.cost = rand.nextInt(10)+1; 
            this.weight = rand.nextInt(10)+1;
    }
    public int getCost() {
        return cost;
    }
    public int getWeight() {
        return weight;
    }
    @Override
    public String toString() {
        return " [cost=" + cost + ", weight=" + weight + "]";
    }
    
}
class Bag extends Main{
    final int maxWeight = 20;
    int weight = 0;
    int cost = 0;
    Thing thing;
    List<Thing> listThingBag = new ArrayList<Thing>();
    public Bag() throws InterruptedException {
        boolean truesWhile = true;
        int point = 0;
        while(truesWhile) {
            if(apartment.listThing.size()>0) {
                int max = apartment.listThing.get(0).getCost(),length = apartment.listThing.size(),value = 0;
                for(int i = 0;i<length;i++) {
                    if(apartment.listThing.get(i).getCost()>max && (apartment.listThing.get(i).getWeight() + weight) <=maxWeight) {
                        max = apartment.listThing.get(i).getCost();
                        value = i;
                    }
                }
                
                if(apartment.listThing.get(value).getWeight()+weight<=maxWeight) {
                    weight+=apartment.listThing.get(value).getWeight();
                    listThingBag.add(apartment.listThing.get(value));
                    //System.out.println("------------------give"+value);
                    apartment.give(value);
                }else {
                    point++;
                    if(point==25) truesWhile = false;
                }
            /**/
            }else {
                truesWhile=false;
            }
        }
    }
    public List<Thing> getListThing() {
        return listThingBag;
    }
}
class Thief extends Main{
    public Thief() throws InterruptedException{
        //System.out.println("size:" +apartment.listThing.size());
        while(true) {
            //while(apartment.listThing.size()>5) {
                //notify();
            //}
            //wait();
            
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Bag bag = new Bag();
            //System.out.println(bag.getListThing().toString());
            //System.out.println(bag.getListThing());
        }
    }
}
class Owner extends Main{
    Thing thing;
    //Apartament apartment = new Apartament();
    public Owner() throws InterruptedException {
        while(true) {
            
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        this.thing = new Thing();
        
        //while(apartment.listThing.size()<1) {
            add();
        //  notify();
        //}
        ///wait();
    }
    }
    void add() {
        apartment.add(thing);
    }
}
class Apartament{
    Object obj = new Object();
    List<Thing> listThing = new ArrayList<Thing>();
    
    int ThinkInApartament = 0;
    int countPut = 0;
    
    Sync s = new Sync();
    public synchronized void add(Thing thing) {
        //synchronized (s) {
        countPutMode(1);
        listThing.add(thing);
        System.out.println(listThing.get(listThing.size()-1));
        System.out.println("Put "+countPut +" element"+listThing.get(listThing.size()-1));
        System.out.println();
        //}
    }
    public synchronized void give(int value) {
        //synchronized (s) {
        if(listThing.size()>0) {
        System.out.println("Give "+countPut + " element" + listThing.get(value));
        System.out.println();
        listThing.remove(value);
        countPutMode(-1);
        }
        //}
    }
    public synchronized void countPutMode(int x) {
        if(x>0) countPut ++;
        else countPut--;
    }
    public List<Thing> getListBag() {
        return listThing;
    }

    public void setListBag(List<Thing> listThing) {
        this.listThing = listThing;
    }
}
class MyOwner extends Thread{
    public void run() {
            try {
                Owner own = new Owner();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("one");
    }
}
class MyThief extends Thread{
    public void run() {
        //for(int i = 0;i<5;i++) {
        //while(true) {
            try {
                Thief theif = new Thief();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("two");
            //break;
        //}}
    }

}

Ошибку дает на 87 и 89 строке.

Тут

int max = apartment.listThing.get(0).getCost(),length = apartment.listThing.size(),value = 0;

Тут

if(apartment.listThing.get(i).getCost()>max && (apartment.listThing.get(i).getWeight() + weight) <=maxWeight)```

Ответы (0 шт):