-2

My app won't launch and points to a Null Pointer Exception on my FileReader fr. I initialized it as null to prevent a "Variable may not have been initialized" error. I know the file I want it to use is there under downloads. The file is also inside the main project folder and putting just "academiccalendar.json" does not work either.

My main activity:

public class MainActivity extends AppCompatActivity {


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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BufferedReader br;
    FileReader fr = null;
    try {
        fr = new FileReader("C:/Users/Ali/Downloads/academiccalendar.json");
    }
    catch (FileNotFoundException e){
        e.printStackTrace();
    }
    br = new BufferedReader(fr);
    //br = new BufferedReader(new FileReader("C:/Users/Ali/Downloads/academiccalendar.json"));


    mobileArray = gson.fromJson(br, Event[].class);

My logcat output:

12-31 20:06:37.368 9449-9449/com.example.test.testassigment E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test.testassigment/com.example.test.testassigment.MainActivity}: java.lang.NullPointerException
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
  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)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)
  at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
  at java.io.Reader.<init>(Reader.java:64)
  at java.io.BufferedReader.<init>(BufferedReader.java:92)
  at java.io.BufferedReader.<init>(BufferedReader.java:80)
  at com.example.test.testassigment.MainActivity.onCreate(MainActivity.java:42)
  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)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Johnny
  • 103
  • 1
  • 13
  • @Rotwang This is NOT about a NullPointerException. Its a different question that you could refer to as the question has been asked before. The problem reported before. – greenapps Dec 15 '16 at 11:40
  • @greenapps `Caused by: java.lang.NullPointerException` ... You are saying that `NullPointerException != NullPointerException`... ? – Phantômaxx Dec 15 '16 at 11:46
  • Irrelevant of course. Better look at my answer to 'discover' the real problem. – greenapps Dec 15 '16 at 11:48
  • @greenapps `Irrelevant ...` a NullPointerException is never irrelevant. – Phantômaxx Dec 15 '16 at 11:49
  • I think it's irrelevant here and not to the point. You better provide a link to a post where one tried to read a windows path too. – greenapps Dec 15 '16 at 11:51
  • @greenapps All NPE are caused by the same error: an object is referenced before it has been instanced. That's the real point. – Phantômaxx Dec 15 '16 at 12:30
  • Quote: `Why can't filereader find my file?` The question has been asked before: http://stackoverflow.com/questions/31059060/reading-file-from-c-drive-from-android-emulator or http://stackoverflow.com/questions/29588927/how-to-read-text-file-from-computer-on-android – greenapps Dec 15 '16 at 12:33
  • @greenapps How does it change the concept of NullPointerException? – Phantômaxx Dec 15 '16 at 12:42
  • Not at all. But if you give it too much attention it distracts from the real problem. OP is better served with solutions for its duplicate question by the links i provided. You are encouraged to use them. – greenapps Dec 15 '16 at 12:55
  • @greenapps StackOverflow is better served by marking duplicates as such. You are encouraged to. – Phantômaxx Dec 15 '16 at 12:58

2 Answers2

3
"C:/Users/Ali/Downloads/academiccalendar.json"

That is a path on your Windows PC. Windows uses drive letters.

Your Android app cannot read files from your PC of course. Or from mine ;-).

I suppose your Android app wants to read a file which is on your Android device.

Try to determine the file on the file system of your Android device.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • I added the file to downloads in my android device, but how do I access it from there? Neither "Download//academiccalendar.json" nor "academiccalendar.json" work – Johnny Dec 15 '16 at 19:23
  • The path to Downloads directory you can get with `Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)`. – greenapps Dec 15 '16 at 22:44
0

Because of your path defination. You can use this:

fr = new FileReader("C:\\Users\\Ali\\Downloads\\academiccalendar.json");
kemal akoğlu
  • 182
  • 13