0

Im new to java so i imagine this is a very obvious question but for the life of me i dont know why this dosent work, i am extremely 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);
    }
}

}

I feel like this should work, but when i run it i get

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
kolosy
  • 3,029
  • 3
  • 29
  • 48

2 Answers2

0

You haven't initialized wares to anything, so wares.tellMe() crashes because wares is null

kolosy
  • 3,029
  • 3
  • 29
  • 48
0

Also shop is a static class you might want to avoid instantiating it (even though it is legal)

Adonis
  • 4,670
  • 3
  • 37
  • 57