FATAL EXCEPTION:main:java.lang.RuntimeException:无法启动活动ComponentInfo:java.lang.NullPointerException:storage == null

时间:2018-06-13 10:23:17

标签: android listview nullpointerexception

我在应用程序中获得Null指针异常。我正在创建一个列表应用程序,它从数据库中提取数据并在Android上的ListView上显示。由于此NullPointerException错误,我的应用程序在开始之前崩溃,然后才打开。任何人都可以帮助我。我是Android新手,请帮助我,我非常需要。

public class MainActivity extends AppCompatActivity {

    ListView listview;
    ArrayAdapter<String> adapter = null;
    String address = "http://192.168.0.109/view.php";
    InputStream is = null;
    String line = null;
    String result = null;
    String[] data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        System.out.println(0);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        System.out.println(1);

        listview = (ListView) findViewById(R.id.list_view);


        //Allow Netwrok in MainFrame//
        StrictMode.setThreadPolicy((new StrictMode.ThreadPolicy.Builder().permitNetwork().build()));
        //Retrieve Data//
        System.out.println(1.5);
        getData();
        //Adapter//
        System.out.println(2);

       //Error Occured java.lang.NullPointerException: storage == null//
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
        listview.setAdapter(adapter);
        // getJSON("http://192.168.0.101/view.php");
    }

    private String getData() {
        try {
            System.out.println(2.5);
            URL url = new URL(address);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();

            con.setRequestMethod("GET");

            is = new BufferedInputStream(con.getInputStream());


        } catch (Exception e) {

            e.printStackTrace();
        }

        //Read InputSteam content into String//
        try {
            System.out.println(3);
            BufferedReader br = null;
            br = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = null;
            sb = new StringBuilder();

            while ((line = br.readLine()) != null) {

                System.out.println(3.2);
                sb.append(line + "\n");
            }
            is.close();

//Error Occurred java.lang.NullPointerException: 
//Attempt to invoke virtual method 'java.lang.String
////java.lang.String.toString()' on a null object reference//


            result.toString();

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

        //Parse JSON//
        try {
            System.out.println(3.5);
            JSONArray ja = null;

            //Error Occurred: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference//
            ja = new JSONArray(result);
            JSONObject jo = null;
            data = new String[ja.length()];

            for (int i = 0; i < ja.length(); i++) {
                System.out.println(4);
                jo = ja.getJSONObject(i);
                data[i] = jo.getString("EmpName");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(4);
        return result;
    }
}

//这是执行时代码的完整日志崩溃条目。 注意:我正在外部设备上运行调试//

I/InstantRun: starting instant run server: is main process
V/Monotype: SetAppTypeFace- try to flip, app = helloworld.krish.com.listview
V/Monotype:     Typeface getFontPathFlipFont - systemFont = default#default
I/System.out: 0
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
V/Monotype: SetAppTypeFace- try to flip, app = helloworld.krish.com.listview
                Typeface getFontPathFlipFont - systemFont = default#default
I/System.out: 1
              1.5
              2.5
I/DpmTcmClient: RegisterTcmMonitor from: com.android.okhttp.TcmIdleTimerMonitor
I/System.out: 3
I/System.out: 3.2
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
                  at helloworld.krish.com.listview.MainActivity.getData(MainActivity.java:86)
                  at helloworld.krish.com.listview.MainActivity.onCreate(MainActivity.java:49)
                  at android.app.Activity.performCreate(Activity.java:6323)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
                  at android.app.ActivityThread.access$900(ActivityThread.java:153)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5451)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I/System.out: 3.5
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
W/System.err:     at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
                  at org.json.JSONTokener.nextValue(JSONTokener.java:94)
                  at org.json.JSONArray.<init>(JSONArray.java:92)
                  at org.json.JSONArray.<init>(JSONArray.java:108)
                  at helloworld.krish.com.listview.MainActivity.getData(MainActivity.java:96)
                  at helloworld.krish.com.listview.MainActivity.onCreate(MainActivity.java:49)
                  at android.app.Activity.performCreate(Activity.java:6323)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
                  at android.app.ActivityThread.access$900(ActivityThread.java:153)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5451)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I/System.out: 4
              2
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: helloworld.krish.com.listview, PID: 14294
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{helloworld.krish.com.listview/helloworld.krish.com.listview.MainActivity}: java.lang.NullPointerException: storage == null
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
                      at android.app.ActivityThread.access$900(ActivityThread.java:153)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5451)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                   Caused by: java.lang.NullPointerException: storage == null
                      at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
                      at java.util.Arrays.asList(Arrays.java:155)
                      at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:137)
                      at helloworld.krish.com.listview.MainActivity.onCreate(MainActivity.java:52)
                      at android.app.Activity.performCreate(Activity.java:6323)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
                      at android.app.ActivityThread.access$900(ActivityThread.java:153) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5451) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
I/Process: Sending signal. PID: 14294 SIG: 9
Application terminated.

1 个答案:

答案 0 :(得分:0)

您调用.toString()的结果变量永远不会被赋予null以外的值。

您在代码顶部将其初始化为null

String result = null;

然后永远不要给它一个价值。您无法调用null对象上的方法,您将获得该异常。正如有人在评论中指出的那样,这可能是What is a NullPointerException, and how do I fix it?的重复。

结果也是一个String,你不需要在它上面调用toString()