另一个类中的空指针异常调用方法

时间:2015-03-04 15:37:34

标签: java android nullpointerexception

我在一个名为Animals.java的类中有一个方法

public String loadJSONFromAsset()

我的目标是在我的AnimalInformation.java文件中重用它,或者最终将它放在它自己的.java文件中,但是当我这样做时,我仍然有相同的结果。

要打电话给我,我正在使用

Animals jsonLoader = new Animals();
try {

        JSONArray mainNode = new JSONArray(jsonLoader.loadJSONFromAsset()); // call the connection to json

        if(mainNode != null) //puts the values into an array
        {
            for(int i=0;i<mainNode.length();i++)
            {
                JSONObject eachObject = mainNode.getJSONObject(i);

                String location = eachObject.getString("zooLocation");


            }
        }
    }
    catch (JSONException e)
    {
        e.printStackTrace();
    }

所以我在JSONArray中调用该方法,但我认为它在这里导致空指针异常。

03-04 15:31:52.700    1735-1735/com.myzooo.zoome E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.myzooo.zoome, PID: 1735
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myzooo.zoome/com.myzooo.zoome.AnimalInformation}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at android.content.ContextWrapper.getAssets(ContextWrapper.java:83)
        at com.myzooo.zoome.Animals.loadJSONFromAsset(Animals.java:177)
        at com.myzooo.zoome.AnimalInformation.onCreate(AnimalInformation.java:28)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)


任何帮助将不胜感激,因为我现在有点迷失,因为我试图使用它的方式,我不完全确定它是否是正确的方法。 感谢。

编辑:loadJSONFromAssets()第177行是InputStream is = getAssets()。open(“animals.json”);

public String loadJSONFromAsset() {

    StringBuilder stringBuilder = new StringBuilder();
    try {
        InputStream is = getAssets().open("animals.json");
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(is));

        String line;
        while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line);
        }

        bufferedReader.close();

        Log.d("X", "Response Ready:" + stringBuilder.toString());

        return stringBuilder.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

2 个答案:

答案 0 :(得分:0)

问题在于:

InputStream is = getAssets().open("animals.json");

您需要一个Context来调用getAssets()方法。您可以将Context传递给loadJSONFromAsset()方法,并将代码更改为:

public String loadJSONFromAsset(Context ctxt) {

    StringBuilder stringBuilder = new StringBuilder();
    try {
        InputStream is = ctxt.getAssets().open("animals.json");
        ...

答案 1 :(得分:0)

getAssets()为您提供NullPointerException,因为它返回null。它需要在Activity Context中调用,所以只需将它放在那里并调用context.getAssets()