Java でのプログラミングは初めてで、自分のコードで何が起こっているのかわかりません。
それは私に言います:
Exception in thread "main" java.lang.NullPointerException
at Main.Country.addMine(Country.java:37)
at Main.Main.main(Main.java:21)
Java Result: 1
私の main.java は単純です:
Continent Europe = new Continent("Europe");
Country asd = new Country("asd", Europe);
Mine mine = new Mine(100,100,100,100);
System.out.println(mine == null);
asd.addMine(mine); //dies here
これは addMine メソッドです:
public void addMine(Mine mine) {
System.out.println(mine == null);
this.mines.add(mine); //dies here
this.iron += mine.iron;
this.gold += mine.gold;
this.stone += mine.stone;
this.wood += mine.wood;
System.out.println("Mine has been successfully added to the country with the given values."
);
Mine.java は次のとおりです。
public class Mine implements Building { //Building is an empty interface :)
protected int iron;
protected int gold;
protected int stone;
protected int wood;
public Mine(int iron, int gold, int stone, int wood) {
this.iron += iron;
this.gold += gold;
this.stone += stone;
this.wood += wood;
}
}
ご覧のとおり、私は 2 つの println-s を書きましたが、どちらも false だったので、オブジェクトが存在します! NullPointerException が表示される理由がわかりません:(