0

I'm trying to open a Json file which is in the raw folder inside res My file structure

But it fails with every method I have tried, I just need the string since I can parse Json Strings already.

This is my Json reader

public class Parser extends Activity {
    public String getStringFromJson(String path) {
        StringBuffer sb = new StringBuffer();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(getAssets().open(path)));
            String temp;
            while ((temp = br.readLine()) != null)
                sb.append(temp);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close(); // stop reading
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}

It fails in the line:

br = new BufferedReader(new InputStreamReader(getAssets().open(path)));

And this is what logcat said:

02-03 10:44:13.440 2374-2374/com.example.ealcazar.kolibry E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ealcazar.kolibry/com.example.ealcazar.kolibry.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.BufferedReader.close()' on a null object reference

is there an easier way to read a Json? what Path should I use when trying to access my file in the RAW folder?

Thank you very much.

Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
Enrique Alcazar
  • 381
  • 7
  • 21
  • My problem was that I was confusing the res folder with the assets folder, and also I was getting a wrong context. I created an assets folder in Android Studio and also got the context from my mainActivity by declaring it as static and assign it in onCreate (MainActivity.context.getResources) – Enrique Alcazar Feb 15 '16 at 10:40

1 Answers1

6

Replace these line of code

br = new BufferedReader(new InputStreamReader(getAssets().open(path)));

With these

br = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.json));//Here json is the name of the file which is placed inside the RAW folder in yours resource
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
  • `Parser.this.getResources()` => `getResources()` ... you don't need `Parser.this` – Selvin Feb 03 '16 at 09:57
  • Sometime it throws the exception there @Selvin .. I used the ClassName.getResoure() instead of getResource() method..any ways thanks for suggestion :) – Ravindra Kushwaha Feb 03 '16 at 09:59
  • What you think.. Is it bad practice to use the CLASSNAME.this before access the resources?? – Ravindra Kushwaha Feb 03 '16 at 10:03
  • Thanks for the answer, but the problem isn't fixed, at least now it seems to know where the file is, the crash is still in the same line and the logcat is now :02-03 11:05:38.740 12864-12864/com.example.ealcazar.kolibry E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ealcazar.kolibry/com.example.ealcazar.kolibry.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.BufferedReader.close()' on a null object reference – Enrique Alcazar Feb 03 '16 at 10:05
  • it is not about resources ... `getResources()` is a method ... like many others ... there is no need for Class.this.method() if "you are in the Class" – Selvin Feb 03 '16 at 10:05
  • ok thanks a lot @Selvin ...for yours useful comments...I am editing my answer.. :) – Ravindra Kushwaha Feb 03 '16 at 10:06
  • @EnriqueAlcazar .. you have to make check before br.close that if(br!=null){ br.close} – Ravindra Kushwaha Feb 03 '16 at 10:09
  • @EnriqueAlcazar .. Is it workable for you ?? .Or you getting some error or exception – Ravindra Kushwaha Feb 03 '16 at 10:21
  • @RavindraKushwaha it is still not working, your idea was good, but if that's happening it means the br is empty and that means the line is still failing. the logical i now saying 02-03 11:23:49.820 21788-21788/com.example.ealcazar.kolibry E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ealcazar.kolibry/com.example.ealcazar.kolibry.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference – Enrique Alcazar Feb 03 '16 at 10:23
  • @EnriqueAlcazar try these lines of code on link.. http://stackoverflow.com/a/15934525/3946958 .. and let me know in case of concern – Ravindra Kushwaha Feb 03 '16 at 10:48
  • If it is workable for you than plz accept it once :) – Ravindra Kushwaha Feb 03 '16 at 11:04
  • @RavindraKushwaha still more exceptions :java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference I think It's not finding my Json, but is there this is te code `InputStream ins = getResources().openRawResource( getResources().getIdentifier(Path, "raw", getPackageName())); strJson = readTextFile(ins);` and Path is test which is the name of my file – Enrique Alcazar Feb 05 '16 at 08:28
  • please check these links..it might be helpful for you http://stackoverflow.com/a/15934525/3946958 http://stackoverflow.com/a/9752719/3946958 and last one http://www.41post.com/3985/programming/android-loading-files-from-the-assets-and-raw-folders – Ravindra Kushwaha Feb 05 '16 at 09:06