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