转换http实体结果时出错java.lang.NullPointerException:lock == null

时间:2015-04-06 13:27:49

标签: java android json adt

我是开发Android应用程序的新手。我已经阅读了很多关于我问的问题的相关帖子,但帖子中的提示或解决方案并没有解决我的问题。 (寻找一周的解决方案,真的需要帮助才能继续我的项目)非常感谢...
错误行号121 Image LinK

  

update.java:

     package pounkumarpurushothaman.ghssvmm;

     import android.app.Activity;
        import android.support.v7.app.ActionBarActivity;
     import android.os.Bundle;
 import android.util.Log;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
 import android.widget.EditText;
 import android.widget.LinearLayout;
 import android.widget.Spinner;
 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.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 import java.io.BufferedReader;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.util.ArrayList;


 public class update extends ActionBarActivity {
public static final String KEY_121 ="http://ghssvmm.site40.net/update.php";
static Activity thisActivity = null;
InputStream is = null;
//  LinearLayout yy=(LinearLayout)findViewById(R.id.uy);
// LinearLayout pp=(LinearLayout)findViewById(R.id.up);
 //LinearLayout cc=(LinearLayout)findViewById(R.id.uc);
//LinearLayout ca=(LinearLayout)findViewById(R.id.uca);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_update);
    getServerData();
}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
private void getServerData() {
    thisActivity=this;
    EditText f = (EditText) findViewById(R.id.ufst);
    EditText l = (EditText) findViewById(R.id.ulst);
    Spinner bld=(Spinner)findViewById(R.id.ubld);
    Spinner stat=(Spinner)findViewById(R.id.ustatus);
    Spinner bth=(Spinner)findViewById(R.id.ubat);
    EditText dob = (EditText) findViewById(R.id.udob);
    EditText phn1 = (EditText) findViewById(R.id.uphn);
    EditText phn2 = (EditText) findViewById(R.id.uphn2);
    EditText padd = (EditText) findViewById(R.id.upadd);
    EditText radd = (EditText) findViewById(R.id.uradd);
    EditText vill = (EditText) findViewById(R.id.uvill);
    Spinner sec=(Spinner)findViewById(R.id.usec);
    Spinner deg=(Spinner)findViewById(R.id.udeg);
    EditText dept = (EditText) findViewById(R.id.udept);
    EditText clg = (EditText) findViewById(R.id.uclg);
    EditText yop = (EditText) findViewById(R.id.uyop);
    EditText pos = (EditText) findViewById(R.id.upos);
    EditText cmp = (EditText) findViewById(R.id.ucom);
    EditText cadd = (EditText) findViewById(R.id.ucaddr);


    String result = "";
    //the year data to send
    final ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("email","abc@gmail.com"));
    Thread thread = new Thread(new Runnable(){
        @Override
        public void run() {
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(KEY_121);
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
            } catch (Exception e) {
                Toast.makeText(thisActivity, "Error in http connection " + e.toString(),Toast.LENGTH_LONG).show();
            }
        }
    });

    thread.start();




    //convert response to string
    try{

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line+"n");
        }
        is.close();
        result=sb.toString();
    }catch(Exception e){
        Toast.makeText(this, "Error converting result "+e.toString(),Toast.LENGTH_LONG).show();
    }
    //parse json data
    try{
        JSONArray jArray = new JSONArray(result);
        for(int i=0;i<jArray.length();i++){
            JSONObject json_data = jArray.getJSONObject(i);
            f.setText(json_data.getString("fname"));
            l.setText(json_data.getString("lname"));
            dob.setText(json_data.getString("dob"));
            switch(json_data.getString("blood")){
                case "O Positive":
                    bld.setSelection(1);
                    break;
                case "O Negative":
                    bld.setSelection(2);
                    break;
                case "A Positive":
                    bld.setSelection(3);
                    break;
                case "A Negative":
                    bld.setSelection(4);
                    break;
                case "B Positive":
                    bld.setSelection(5);
                    break;
                case "B Negative":
                    bld.setSelection(6);
                    break;
                case "AB Positive":
                    bld.setSelection(7);
                    break;
                case "AB Negative":
                    bld.setSelection(8);
                    break;
            }
            switch(json_data.getString("section")) {
                case "A":
                    sec.setSelection(1);
                    break;
                case "B":
                    sec.setSelection(2);
                    break;
                case "C":
                    sec.setSelection(3);
                    break;
            }
            phn1.setText(json_data.getString("phn"));
            phn2.setText(json_data.getString("phn2"));
            padd.setText(json_data.getString("add"));
            radd.setText(json_data.getString("add2"));
            switch(json_data.getString("status")) {
                case "Student":
                    stat.setSelection(1);
                   /* cc.setVisibility(View.INVISIBLE);
                    yy.setVisibility(View.INVISIBLE);
                    pp.setVisibility(View.INVISIBLE);
                    ca.setVisibility(View.INVISIBLE);*/
                    break;
                case "Employee":
                    stat.setSelection(2);
                    /*cc.setVisibility(View.VISIBLE);
                    yy.setVisibility(View.VISIBLE);
                    pp.setVisibility(View.VISIBLE);
                    ca.setVisibility(View.VISIBLE);*/
                    break;
                case "Un Employee":
                    stat.setSelection(3);
                   /* cc.setVisibility(View.INVISIBLE);
                    yy.setVisibility(View.VISIBLE);
                    pp.setVisibility(View.INVISIBLE);
                    ca.setVisibility(View.INVISIBLE);*/
                    break;
            }
            switch(json_data.getString("degree")) {
                case "B.E":
                    deg.setSelection(1);
                    break;
                case "Arts and Science":
                    deg.setSelection(2);
                    break;
                case "Medical":
                    deg.setSelection(3);
                    break;
            }
            dept.setText(json_data.getString("dept"));
            clg.setText(json_data.getString("clg"));
            yop.setText(json_data.getString("yop"));
            pos.setText(json_data.getString("pos"));
            cmp.setText(json_data.getString("com"));
            cadd.setText(json_data.getString("caddr"));
            vill.setText(json_data.getString("vill"));
            //Get an output to the screen

        }
    }catch(JSONException e) {
        Toast.makeText(this, "Error parsing data " + e.toString(), Toast.LENGTH_LONG).show();
    }

}

}

**

  

Logcat:   **

04-06 14:11:39.580  25919-25919/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: pounkumarpurushothaman.ghssvmm, PID: 25919
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{pounkumarpurushothaman.ghssvmm/pounkumarpurushothaman.ghssvmm.update}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
        at android.app.ActivityThread.access$800(ActivityThread.java:148)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5312)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
        at android.app.Activity.findViewById(Activity.java:2081)
        at pounkumarpurushothaman.ghssvmm.update.<init>(update.java:34)
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.Class.newInstance(Class.java:1572)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1088)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
        at android.app.ActivityThread.access$800(ActivityThread.java:148)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5312)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

1 个答案:

答案 0 :(得分:0)

以下是执行POST请求后如何获得响应。

                    HttpResponse response = httpClient.execute(httpPost);
                    String op = EntityUtils.toString(response.getEntity(), "UTF-8");

                    Log.d("Response:", op);