Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java NullPointerException - Short Program

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 :(

like image 368
Iburidu Avatar asked Nov 17 '12 23:11

Iburidu


People also ask

What is NullPointerException in Java example?

NullPointerException is a runtime exception in Java that occurs when a variable is accessed which is not pointing to any object and refers to nothing or null. Since the NullPointerException is a runtime exception, it doesn't need to be caught and handled explicitly in application code.

How do I fix NullPointerException in Java?

In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

What is the reason for NullPointerException in Java?

Some of the common reasons for NullPointerException in java programs are: Invoking a method on an object instance but at runtime the object is null. Accessing variables of an object instance that is null at runtime. Throwing null in the program.


2 Answers

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.

like image 195
Jon Skeet Avatar answered Oct 06 '22 00:10

Jon Skeet


Yes, mine could be not null but what about mines? Which I guess it's a ArrayList<Mine> or something like that, did you inizialize it as mines = new ArrayList<Mine>()? (or whichever collection it is)

like image 27
Jack Avatar answered Oct 06 '22 00:10

Jack