Unable to start activity ComponentInfo{com.example.my_app/com.example.my_app.MainActivity}: java.lang.NullPointerException


Ask A Question





Login To post a question

I am trying to display the data on mysql on and android app

this is my code
package com.example.my_app;

import java.io.BufferedReader;
import java.io.InputStream;
import android.os.Bundle;
import android.util.Log; 
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TableRow.LayoutParams;

public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /* Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener()
 {
@SuppressWarnings(“deprecation”)
public void onClick(View view)
{*/
    String result = null;
    InputStream is = null;
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://192.168.43.70/my_app/demo.php");
        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");
                //  Toast.makeText(getApplicationContext(), “Input Reading pass”, Toast.LENGTH_SHORT).show();
            }
            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.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));
                if (flag == 1) {
                    TextView b6 = new TextView(MainActivity.this);
                    b6.setText("Id");
                    b6.setTextColor(Color.BLUE);
                    b6.setTextSize(15);
                    tr.addView(b6);
                    TextView b19 = new TextView(MainActivity.this);
                    b19.setPadding(10, 0, 0, 0);
                    b19.setTextSize(15);
                    b19.setText("name");
                    b19.setTextColor(Color.BLUE);
                    tr.addView(b19);
                    TextView b29 = new TextView(MainActivity.this);
                    b29.setPadding(10, 0, 0, 0);
                    b29.setText("age");
                    b29.setTextColor(Color.BLUE);
                    b29.setTextSize(15);
                    tr.addView(b29);
                    tv.addView(tr);
                    final View vline = new View(MainActivity.this);
                    vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 2));
                    vline.setBackgroundColor(Color.BLUE);
                    tv.addView(vline);
                    flag = 0;
                } else {
                    JSONObject json_data = jArray.getJSONObject(i);
                    Log.i("log_tag”, ”id: " + json_data.getInt("Id") + ", Username: " + json_data.getString("username") + ", No: " + json_data.getString("comment"), null);
                    TextView b = new TextView(MainActivity.this);
                    String stime = String.valueOf(json_data.getInt("Id"));
                    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("username");
                    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("comment");
                    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.MATCH_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();
        }

    }

}

my errors are

07-21 03:33:38.036: E/AndroidRuntime(6963): FATAL EXCEPTION: main  
07-21 03:33:38.036: E/AndroidRuntime(6963): java.lang.RuntimeException: 
Unable to start activity 
ComponentInfo{com.example.my_app/com.example.my_app.MainActivity}: 
java.lang.NullPointerException
07-21 03:33:38.036: E/AndroidRuntime(6963):     at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-21 03:33:38.036: E/AndroidRuntime(6963):     at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-21 03:33:38.036: E/AndroidRuntime(6963):     at 
android.app.ActivityThread.access$600(ActivityThread.java:141)
07-21 03:33:38.036: E/AndroidRuntime(6963):     at  
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-21 03:33:38.036: E/AndroidRuntime(6963):     at 
android.os.Handler.dispatchMessage(Handler.java:99)
07-21 03:33:38.036: E/AndroidRuntime(6963):     at 
android.os.Looper.loop(Looper.java:137)
07-21 03:33:38.036: E/AndroidRuntime(6963):     at 

android.app.ActivityThread.main(ActivityThread.java:5041)
07-21 03:33:38.036: E/AndroidRuntime(6963):     at java.lang.reflect.Method.invokeNative(Native Method)
07-21 03:33:38.036: E/AndroidRuntime(6963):     at java.lang.reflect.Method.invoke(Method.java:511)
07-21 03:33:38.036: E/AndroidRuntime(6963):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-21 03:33:38.036: E/AndroidRuntime(6963):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-21 03:33:38.036: E/AndroidRuntime(6963):     at dalvik.system.NativeStart.main(Native Method)
07-21 03:33:38.036: E/AndroidRuntime(6963): Caused by: java.lang.NullPointerException
07-21 03:33:38.036: E/AndroidRuntime(6963):     at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
07-21 03:33:38.036: E/AndroidRuntime(6963):     at org.json.JSONTokener.nextValue(JSONTokener.java:94)
07-21 03:33:38.036: E/AndroidRuntime(6963):     at org.json.JSONArray.<init>(JSONArray.java:87)
07-21 03:33:38.036: E/AndroidRuntime(6963):     at org.json.JSONArray.<init>(JSONArray.java:103)
07-21 03:33:38.036: E/AndroidRuntime(6963):     at com.example.my_app.MainActivity.onCreate(MainActivity.java:107)
07-21 03:33:38.036: E/AndroidRuntime(6963):     at android.app.Activity.performCreate(Activity.java:5104)
07-21 03:33:38.036: E/AndroidRuntime(6963):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-21 03:33:38.036: E/AndroidRuntime(6963):     at 

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-21 03:33:38.036: E/AndroidRuntime(6963):     ... 11 more

my android manifest is

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.my_app" 
     android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" 
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET" />

<application android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme">

    <activity android:name="com.example.my_app.MainActivity" 
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

my activity main is

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent"
     android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin" 
      android:paddingLeft="@dimen/activity_horizontal_margin" 
      android:paddingRight="@dimen/activity_horizontal_margin" 
      android:paddingTop="@dimen/activity_vertical_margin" 
      tools:context=".MainActivity">

<HorizontalScrollView android:layout_width="match_parent"
     android:layout_height="wrap_content"
      android:focusableInTouchMode="true" 
      android:focusable="true">

    <TableLayout android:id="@+id/table"
         android:layout_width="wrap_content" 
         android:layout_height="match_parent"
          android:focusableInTouchMode="true" 
          android:focusable="true"></TableLayout>

</HorizontalScrollView>

my php file is

<?php
$username ='root';
$password ='';
$hostname ='localhost';
$database ='my_app';

$localhost = mysql_connect($hostname,$username,$password) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database,localhost);
$i=mysql_query(“select * from smart healthcare”);

$num_rows = mysql_num_rows($i);
while($row = mysql_fetch_array($i))
{

$r[]=$row;
$check=$row['Id'];
}

if($check==NULL)
{
$r[$num_rows]=”Record is not available”;
print(json_encode($r));
}
else
{
$r[$num_rows]=”success”;
print(json_encode($r));
}

mysql_close($localhost);
?>

For more info please click herehttps://stackoverflow.com/q/51772499



Have a question or need a custom quote?
info@smartinfocare.com