मैं जावा में प्रोग्रामिंग में नया हूं और मुझे नहीं पता कि मेरे कोड में क्या चल रहा है।जावा NullPointerException - लघु कार्यक्रम
यह मुझसे कहता है:
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 लिखा है और उनमें से दोनों झूठे थे, इसलिए वस्तु मौजूद है! मैं समझता हूँ कि नहीं क्यों यह NullPointerException :(
मैं अनुमान लगाता हूं कि 'खान' देश में कुछ प्रकार की गतिशील सूची है? इससे पहले कि आप इसे जोड़ने का प्रयास करें, क्या इसे आरंभ किया गया है? – Logard
क्या आपने खानों को शुरू किया है? जो मुझे लगता है कि एक सरणीसूची है। –
क्या आप हमें पोस्ट कर सकते हैं कि आपने 'खानों' की घोषणा कैसे की? – Zakaria