Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ';int java.lang.String.length()';关于空对象引用_Android - Fatal编程技术网

Android ';int java.lang.String.length()';关于空对象引用

Android ';int java.lang.String.length()';关于空对象引用,android,Android,我对安卓非常陌生。我知道这个问题是重复的,我尝试了所有的解决方案,但我没有解决我的问题。我希望你能理解。这是我的代码,用于从MYSQL检索数据,并显示为表格式。但我还是坚持了一些。我尝试了很多解决方案来解决我的问题,但没有得到结果。每次我运行我的应用程序时,它都会崩溃。而且我在表视图中显示数据时也失败了 main.java public class MainActivity extends Activity { public void onCreate(Bundle savedI

我对安卓非常陌生。我知道这个问题是重复的,我尝试了所有的解决方案,但我没有解决我的问题。我希望你能理解。这是我的代码,用于从
MYSQL
检索数据,并显示为
格式。但我还是坚持了一些。我尝试了很多解决方案来解决我的问题,但没有得到结果。每次我运行我的应用程序时,它都会崩溃。而且我在表视图中显示数据时也失败了

main.java

    public class MainActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String result = null;
        InputStream is = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://IP/stat_api/myjson.json");
            HttpResponse response = httpclient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

            Log.e("log_tag", "Connection Success");
            //   Toast.makeText(getApplicationContext(), “pass”, Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Log.e("log_tag", "Error in HTTP Connection" + e.toString());
            Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();

        }
        //convert response to string
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();

            result = sb.toString();
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result" + e.toString());
            Toast.makeText(getApplicationContext(), "Input reading fail", Toast.LENGTH_SHORT).show();

        }

        //parse json data
        try {
            JSONArray jArray = new JSONArray(result);
            TableLayout tv = (TableLayout) findViewById(R.id.table);
            tv.removeAllViewsInLayout();
            int flag = 1;
            for (int i = -1; i < jArray.length() - 1; i++) {
                TableRow tr = new TableRow(MainActivity.this);
                tr.setLayoutParams(new LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
                if (flag == 1) {
                    TextView tv1 = new TextView(MainActivity.this);
                    tv1.setText("eid");
                    tv1.setTextColor(Color.BLUE);
                    tv1.setTextSize(15);
                    tr.addView(tv1);

                    TextView tv2 = new TextView(MainActivity.this);
                    tv2.setPadding(10, 0, 0, 0);
                    tv2.setTextSize(15);
                    tv2.setText("name");
                    tv2.setTextColor(Color.BLUE);
                    tr.addView(tv2);

                    TextView tv3 = new TextView(MainActivity.this);
                    tv3.setPadding(10, 0, 0, 0);
                    tv3.setText("user_name");
                    tv3.setTextColor(Color.BLUE);
                    tv3.setTextSize(15);
                    tr.addView(tv3);

                    tv.addView(tr);
                    final View vline = new View(MainActivity.this);
                    vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
                    vline.setBackgroundColor(Color.BLUE);
                    tv.addView(vline);
                    flag = 0;
                } else {
                    JSONObject json_data = jArray.getJSONObject(i);
                    Log.i("log_tag", "eid: " + json_data.getInt("eid") + ", name: " + json_data.getString("name") + ", user_name: " + json_data.getString("user_name"));
                    TextView b = new TextView(MainActivity.this);
                    String stime = String.valueOf(json_data.getInt("eid"));
                    b.setText(stime);
                    b.setTextColor(Color.RED);
                    b.setTextSize(15);
                    tr.addView(b);

                    TextView b1 = new TextView(MainActivity.this);
                    b1.setPadding(10, 0, 0, 0);
                    b1.setTextSize(15);
                    String stime1 = json_data.getString("name");
                    b1.setText(stime1);
                    b1.setTextColor(Color.BLACK);
                    tr.addView(b1);
                    TextView b2 = new TextView(MainActivity.this);
                    b2.setPadding(10, 0, 0, 0);
                    String stime2 = json_data.getString("user_name");
                    b2.setText(stime2);
                    b2.setTextColor(Color.BLACK);
                    b2.setTextSize(15);
                    tr.addView(b2);
                    tv.addView(tr);
                    final View vline1 = new View(MainActivity.this);
                    vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
                    vline1.setBackgroundColor(Color.WHITE);
                    tv.addView(vline1);
                }
            }
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data" + e.toString());
            Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
        }

    }

}
日志:

Process: com.example.newtable.newtable, PID: 5668


java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newtable.newtable/com.example.newtable.newtable.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                 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: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
                                                                                 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 com.example.newtable.newtable.MainActivity.onCreate(MainActivity.java:65)
                                                                                 at android.app.Activity.performCreate(Activity.java:6237)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                 at android.os.Looper.loop(Looper.java:148) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                 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) 
Process:com.example.newtable.newtable,PID:5668
java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.newtable.newtable/com.example.newtable.newtable.MainActivity}:java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“int java.lang.String.length()”
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)上
位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
位于android.app.ActivityThread.-wrap11(ActivityThread.java)
在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)上
位于android.os.Handler.dispatchMessage(Handler.java:102)
位于android.os.Looper.loop(Looper.java:148)
位于android.app.ActivityThread.main(ActivityThread.java:5417)
位于java.lang.reflect.Method.invoke(本机方法)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run上(ZygoteInit.java:726)
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
原因:java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“int java.lang.String.length()”
位于org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
位于org.json.JSONTokener.nextValue(JSONTokener.java:94)
位于org.json.JSONArray(JSONArray.java:92)
位于org.json.JSONArray(JSONArray.java:108)
位于com.example.newtable.newtable.MainActivity.onCreate(MainActivity.java:65)
位于android.app.Activity.performCreate(Activity.java:6237)
位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)上
位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
位于android.app.ActivityThread.-wrap11(ActivityThread.java)
在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)上
位于android.os.Handler.dispatchMessage(Handler.java:102)
位于android.os.Looper.loop(Looper.java:148)
位于android.app.ActivityThread.main(ActivityThread.java:5417)
位于java.lang.reflect.Method.invoke(本机方法)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run上(ZygoteInit.java:726)
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

您实际上是在使用api,而不是从数据库中读取。好的解决方案不是手动解析JSON,而是使用Gson库和POJO模式

请发布您的日志。是的,我会更新它@SachinBahukhandi@T.KarthikeyanT.Karthikeyan您是否真的在JSONArray中得到了结果?你查过了吗debuging@T.KarthikeyanT.Karthikeyan请确保您从服务器得到了正确的响应,并且您的JSONArray具有该结果。@GrIsHu,我无法调试,因为应用程序云未进入。在此之前,它将关闭。首先感谢您的回复。你能给我一些吗?好的,谢谢。为什么上面的代码不起作用呢?或者我的代码有这样的错误概念?一些字符串是空的(可能json没有这样的字段)。点击错误输出,它会显示带有错误的行。顺便说一下,解析json是一个相当长的操作,你不应该在主线程上进行。
Process: com.example.newtable.newtable, PID: 5668


java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newtable.newtable/com.example.newtable.newtable.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                 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: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
                                                                                 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 com.example.newtable.newtable.MainActivity.onCreate(MainActivity.java:65)
                                                                                 at android.app.Activity.performCreate(Activity.java:6237)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                 at android.os.Looper.loop(Looper.java:148) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                 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)