0

I was following this question Using JSON File in Android App Resources to try to access my JSON file and pass it as an argument to my fromJSON call, however I get an NPE on my mobileArray initialization because jsonString's value is still null. What's going on here? My problem isn't the NPE itself but with my value not getting initialized.

EDIT: Tried passing InputStreamReader but it still crashes, updated code and added logcat

Event[] myArray;
Gson gson = new Gson();

InputStream is = getResources().openRawResource(R.raw.myJSON);
InputStreamReader isReader = new InputStreamReader(is);


myArray = gson.fromJson(isReader, Event[].class);

Logcat:

   Caused by: java.lang.NullPointerException
at com.example.test.testassigment.Event.setDayOfTheWeek(Event.java:36)
at com.example.test.testassigment.Event.<init>(Event.java:21)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:108)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:210) 
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41) 
at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:72) 
at com.google.gson.Gson.fromJson(Gson.java:887) 
at com.google.gson.Gson.fromJson(Gson.java:825) 
at com.example.test.testassigment.MainActivity.onCreate(MainActivity.java:70) 
at android.app.Activity.performCreate(Activity.java:4486) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) 
at android.app.ActivityThread.access$600(ActivityThread.java:127) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4511) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511)
Community
  • 1
  • 1
Rishar
  • 39
  • 1
  • 4
  • Get rid of most of that. Call `gson.fromJson()`, supplying an `InputStreamReader` wrapped around your `InputStream`. If you continue to have problems, update your question with your revised code plus the complete Java stack trace associated with your crash. – CommonsWare Dec 15 '16 at 20:23
  • `jsonString's value is still null` nope. from what you have here, I'd say `gson` is null. – njzk2 Dec 15 '16 at 20:28
  • I had intialized gson as new Gson() but not included that in my code snippet, fixed that now – Rishar Dec 15 '16 at 20:30

0 Answers0