0
進出口新的Java的,所以我想這是一個很明顯的問題 但對我的生活我不知道爲什麼這個dosent工作,我感到非常cluless屬性的對象方法調用對其他對象
public class start {
public static void main(String[] args){
Shop shop = new Shop();
shop.tellMe();
}
static class Shop{
Item wares;
void tellMe(){
this.wares.itemOut();
}
}
public class Item{
int price = 0;
String name = "Sunglasses";
void itemOut(){
System.out.println(price + " " + name);
}
}
}
我覺得這應該工作,但是當我運行它,我得到
Exception in thread "main" java.lang.NullPointerException
at start$Shop.tellMe(start.java:14)
at start.main(start.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)m