Java NullPointerException - Short Program

exception, java, nullpointerexception, program-entry-point, runtime-error

Solution

If this is failing:

this.mines.add(mine); //dies here

... then I suspect `mines` is a null reference. You haven't shown any declaration for it or initialization - but that should be your first port of call. Chances are it's just a case of changing:

private List<Mine> mines;

to

private List<Mine> mines = new ArrayList<Mine>();

or something similar.

Problem

I'm new in programming in Java and I do not understand what's going on in my code. It tells me: ``` Exception in thread "main" java.lang.NullPointerException at Main.Country.addMine(Country.java:37) at Main.Main.main(Main.java:21) Java Result: 1 ``` My main.java is simple: ``` 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 ``` this is addMine method: ``` 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." ); ``` and Mine.java is: ``` 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; } } ``` As You can see I wrote 2 println-s and both of them were false, so the object exists! I don't understand why it shows NullPointerException :(

Original source