0

I am trying to retrieve value from the database and am using EditText to display in the android application. But the project is stopped whenever I try to retrieve it. I am using eclipse to develop the android project. Select.java

package com.example.erp_medlabplus;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

@SuppressLint("NewApi")
public class SelectActivity extends Activity {

    @SuppressLint("NewApi")
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select);

        Bundle b = getIntent().getExtras();
        EditText ed = (EditText) findViewById(R.id.patient_id);
        ed.setText(b.getCharSequence("Contents"));

        Button button = (Button) findViewById(R.id.button1);
        StrictMode.setThreadPolicy(policy);

        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                String result = null;
                InputStream is = null;
                EditText editText = (EditText) findViewById(R.id.patient_id);
                String v1 = editText.getText().toString();
                EditText editText1 = (EditText) findViewById(R.id.CT_Scan);

                EditText editText2 = (EditText) findViewById(R.id.MRI_Scan);
                EditText editText3 = (EditText) findViewById(R.id.xray);
                EditText editText4 = (EditText) findViewById(R.id.ECG);
                EditText editText5 = (EditText) findViewById(R.id.Radiology);

                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

                nameValuePairs.add(new BasicNameValuePair("id", v1));
                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://192.168.42.100:8888/ImageUpload/select.php");
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();

                    Log.e("log_tag", "connection success ");
                }
                catch (Exception e) {
                    Log.e("log_tag", "Error in http connection " + e.toString());
                    Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();
                }

                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 {
                    JSONObject object = new JSONObject(result);
                    String ch = object.getString("re");
                    if (ch.equals("success")) {
                        Toast.makeText(getApplicationContext(), "Retrieval Sucess", Toast.LENGTH_SHORT).show();
                        JSONObject no = object.getJSONObject("0");
                        String w = no.getString("CT_Scan");
                        String w1 = no.getString("MRI_Scan");
                        String w2 = no.getString("XRay");
                        String w3 = no.getString("ECG");
                        String w4 = no.getString("Radiology");

                        editText1.setText(w);
                        editText2.setText(w1);
                        editText3.setText(w2);
                        editText4.setText(w3);
                        editText5.setText(w4);
                    } else {
                        Toast.makeText(getApplicationContext(), "Record is not available.. Enter valid number", Toast.LENGTH_SHORT).show();
                    }
                }
                catch (JSONException e) {
                    Log.e("log_tag", "Error parsing data " + e.toString());
                    Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_select, menu);
        return true;
    }

}

activity_select.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    android:background="@drawable/bg3">

<TextView
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_margin="3dp"
    android:text="Sample Retrieval"
    android:gravity="center"
    android:background="@drawable/bg"
    android:textColor="#FFFFFF"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/patid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_marginTop="20dp"
    android:text="Patient ID"
    android:textColor="#0E52AB"
    android:layout_below="@+id/header"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textStyle="bold"
     />

<EditText
    android:id="@+id/patient_id"
    android:layout_width="130dp"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="20dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/header"
    android:layout_toRightOf="@+id/patid"
    android:background="@drawable/edittextdesign"
    android:ems="10"
    android:numeric="integer" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button1"
    android:text="Retrieve"
    android:radius="14dp"
    android:layout_below="@+id/patid"
    android:gravity="center"
    android:textColor="#FFFFFF"
    android:textSize="20sp"
    android:layout_marginTop="20dp"
    android:layout_width="120dp"
    android:layout_height="40dp"
    android:shadowColor="#259FA8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    android:background="@drawable/bg"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Tests to be taken:"
    android:textColor="#0E52AB"
    android:layout_marginTop="20dp"
    android:id="@+id/test"
    android:layout_below="@+id/button1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textStyle="bold"/>


<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="20dp"
    android:layout_below="@+id/test"
    android:textColor="#0E52AB"
    android:text="CT Scan"
    />


<EditText
    android:id="@+id/CT_Scan"
    android:layout_width="130dp"
    android:layout_marginTop="10dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:inputType="text"
    android:ellipsize="start"
    android:gravity="center"
    android:background="@drawable/edittextdesign"
    android:ems="10"
     />

<Button
    android:id="@+id/CTScan"
    android:layout_width="80dp"
    android:layout_height="40dp"
    android:textColor="#FFFFFF"
    android:ellipsize="start"
    android:gravity="center"
    android:onClick="scan"
    android:text="Scan"
    android:background="@drawable/bg"
    android:layout_above="@+id/textView2"
    android:layout_toRightOf="@+id/patient_id"
    android:layout_toEndOf="@+id/patient_id" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/CT_Scan"
    android:layout_marginTop="10dp"
    android:text="MRI Scan"
    android:textColor="#0E52AB"/>

<EditText
    android:id="@+id/MRI_Scan"
    android:layout_width="130dp"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:background="@drawable/edittextdesign"
    android:layout_marginTop="10dp"
    android:ellipsize="start"
    android:gravity="center"
    android:layout_below="@+id/textView2"
    android:ems="10" />

<Button
    android:id="@+id/MRIScan"
    android:layout_width="80dp"
    android:layout_height="40dp"
    android:textColor="#FFFFFF"
    android:ellipsize="start"
    android:gravity="center"
    android:onClick="scan"
    android:text="Scan"
    android:background="@drawable/bg"
    android:layout_above="@+id/textView3"
    android:layout_alignLeft="@+id/CTScan"
    android:layout_alignStart="@+id/CTScan" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/MRI_Scan"
    android:layout_marginTop="10dp"
    android:text="XRay"
    android:textColor="#0E52AB"/>

<EditText
    android:id="@+id/XRay"
    android:layout_width="130dp"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:background="@drawable/edittextdesign"
    android:ellipsize="start"
    android:gravity="center"
    android:layout_below="@+id/textView3"
    android:layout_marginTop="10dp"
    android:ems="10" />

<Button
    android:id="@+id/xRay"
    android:layout_width="80dp"
    android:layout_height="40dp"
    android:textColor="#FFFFFF"
    android:ellipsize="start"
    android:gravity="center"
    android:onClick="scan"
    android:text="Scan"
    android:background="@drawable/bg"
    android:layout_above="@+id/textView4"
    android:layout_alignLeft="@+id/MRIScan"
    android:layout_alignStart="@+id/MRIScan" />
<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="10dp"
    android:layout_below="@+id/XRay"
    android:text="ECG"
    android:textColor="#0E52AB"/>

<EditText
    android:id="@+id/ECG"
    android:layout_width="130dp"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:background="@drawable/edittextdesign"
    android:ellipsize="start"
    android:gravity="center"
    android:layout_below="@+id/textView4"
    android:layout_marginTop="10dp"
    android:ems="10" />
<Button
    android:id="@+id/E_C_G"
    android:layout_width="80dp"
    android:layout_height="40dp"
    android:textColor="#FFFFFF"
    android:ellipsize="start"
    android:gravity="center"
    android:onClick="scan"
    android:text="Scan"
    android:background="@drawable/bg"
    android:layout_above="@+id/textView5"
    android:layout_alignLeft="@+id/xRay"
    android:layout_alignStart="@+id/xRay" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Radiology"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="10dp"
    android:layout_below="@+id/ECG"
    android:textColor="#0E52AB"/>

<EditText
    android:id="@+id/Radiology"
    android:layout_width="130dp"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:background="@drawable/edittextdesign"
    android:ellipsize="start"
    android:gravity="center"
    android:layout_below="@+id/textView5"
    android:layout_marginTop="10dp"
    android:ems="10" />

<Button
    android:id="@+id/radiologyScan"
    android:layout_width="80dp"
    android:layout_height="40dp"
    android:textColor="#FFFFFF"
    android:ellipsize="start"
    android:gravity="center"
    android:onClick="scan"
    android:text="Scan"
    android:background="@drawable/bg"
    android:layout_alignBottom="@+id/Radiology"
    android:layout_alignLeft="@+id/E_C_G"
    android:layout_alignStart="@+id/E_C_G" />
    </RelativeLayout>

select.php:

<?php
    error_reporting(E_ALL ^ E_DEPRECATED);
    $con = mysql_connect("localhost","root","");

    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("erp", $con);
    $v1=$_REQUEST['id'];
    //$v1=2;
    if($v1==NULL) {
        $r["re"]="Enter the number!!!";
        print(json_encode($r));
        die('Could not connect: ' . mysql_error());
    }
    else { 
        $i=mysql_query("SELECT * FROM scan_entry_value WHERE id='$v1'",$con);
        $check='';
        //if($i === FALSE) { 
        //die(mysql_error());} // TODO: better error handling

        while($row = mysql_fetch_array($i)) {
            //echo $row['CT_Scan'];
            $r[]=$row;
            $check=$row['id'];               
        }        

        if($check==NULL) {            
            $r["re"]="Record is not available";
            print(json_encode($r));   
        }
        else {                
            $r["re"]="success";
            print(json_encode($r));
        } 
    }
    mysql_close($con); 
?> 
2
  • 2
    it is more important to post error log along with the code,to help people understand the exact problem.If not,you may end up in loosing reputation. Commented Mar 31, 2015 at 12:03
  • Which of the 6 calls to setText does the NPE occur on? Commented Mar 31, 2015 at 12:10

1 Answer 1

1

I'm going to assume that this line's the cause of your problem:

EditText editText3=(EditText)findViewById(R.id.xray);

Your activity_select.xml doesn't define a control with the ID xray hence the call to findViewById returns a null and your call to editText3.setText() results in an NPE. It does, however, have TextViews with IDs XRay and xRay. Try using one of those instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Oops!!! Was my mistake.Made the changes and now the app works perfect!!! Thanks a ton

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.