logcat fail3中出错:java.lang.NullPointerException

logcat fail3中出错:java.lang.NullPointerException,java,android,json,nullpointerexception,Java,Android,Json,Nullpointerexception,我在Android中创建了一个页面,获取ID和名称,然后将其插入到我的数据库中,但我在logcat中发现了这个错误 logcat中的错误为: 通过1(854):连接成功 pass2(854):连接成功 失败3(854):java.lang.NullPointerException 我的代码是: public class Sampleactivity extends Activity { String name; String id; InputStream is=null; String re

我在Android中创建了一个页面,获取ID和名称,然后将其插入到我的数据库中,但我在logcat中发现了这个错误

logcat中的错误为:

通过1(854):连接成功
pass2(854):连接成功
失败3(854):java.lang.NullPointerException

我的代码是

public class Sampleactivity extends Activity {

String name;
String id;
InputStream is=null;
String result=null;
String line=null;
int code;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mainprogram);
      final EditText e_id=(EditText) findViewById(R.id.editText1);
        final EditText e_name=(EditText) findViewById(R.id.editText2);
        Button insert=(Button) findViewById(R.id.button1);
        insert.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                id=e_id.getText().toString();
                name=e_name.getText().toString();


                insert();
            }
        });
}


public void insert()
{


ArrayList<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id", id));
nameValuePairs.add(new BasicNameValuePair("name", name));
try{

    HttpClient httpclient=new DefaultHttpClient();
    HttpPost httppost=new HttpPost("http://10.0.2.2/insert.php");
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response=httpclient.execute(httppost);
    HttpEntity entity=response.getEntity();
    is=entity.getContent();
    Log.e("pass 1","connection success");
}
catch(Exception e)
{
    Log.e("fail 1",e.toString());
    Toast.makeText(getApplicationContext(), "invalid ip address",Toast.LENGTH_LONG).show();
}

try
{
    BufferedReader reader=new BufferedReader
            (new InputStreamReader(is, "iso-8859-1"),8);   

StringBuilder sb=new StringBuilder();
while((line=reader.readLine())!=null)
{
    sb.append(line + "\n");
}

is.close();
String  result=sb.toString();
Log.e("pass2","connection success");
}
catch(Exception e)
{
    Log.e("fail2",e.toString());

}

try
{

    JSONParser parser_obj=new JSONParser();
JSONArray   jsonArray=(JSONArray)parser_obj.parse(result);

    JSONObject json_data=new JSONObject();
    for(int i=0;i<jsonArray.length();i++)
    {
        json_data=jsonArray.getJSONObject(i);

    }
    code=json_data.getInt("code");
    if(code==1)
    {
        Toast.makeText(getApplicationContext(), "Inserted Successfully",Toast.LENGTH_SHORT).show();

}

    else
    {
        Toast.makeText(getApplicationContext(), "sorry try again",Toast.LENGTH_LONG).show();
    }

}

catch(Exception e)
{
    Log.e("fail 3",e.toString());
}


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub

    getMenuInflater().inflate(R.menu.activity_mainprogram, menu);
    return true;
}



}
public类Sampleactivity扩展活动{
字符串名;
字符串id;
InputStream=null;
字符串结果=null;
字符串行=null;
int代码;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u主程序);
最终EditText e_id=(EditText)findViewById(R.id.editText1);
最终EditText e_name=(EditText)findViewById(R.id.editText2);
按钮插入=(按钮)findViewById(R.id.button1);
insert.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图arg0){
//TODO自动生成的方法存根
id=e_id.getText().toString();
name=e_name.getText().toString();
插入();
}
});
}
公开作废插入()
{
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“id”,id));
添加(新的BasicNameValuePair(“name”,name));
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://10.0.2.2/insert.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
Log.e(“通过1”,“连接成功”);
}
捕获(例外e)
{
Log.e(“fail 1”,e.toString());
Toast.makeText(getApplicationContext(),“无效ip地址”,Toast.LENGTH_LONG.show();
}
尝试
{
BufferedReader reader=新的BufferedReader
(新的InputStreamReader(is,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
而((line=reader.readLine())!=null)
{
sb.追加(第+行“\n”);
}
is.close();
字符串结果=sb.toString();
Log.e(“pass2”,“连接成功”);
}
捕获(例外e)
{
Log.e(“fail2”,e.toString());
}
尝试
{
JSONParser_obj=新的JSONParser();
JSONArray JSONArray=(JSONArray)解析器_obj.parse(结果);
JSONObject json_data=new JSONObject();

对于(int i=0;i请在
catch
s中使用
e.getStackTrace()
,而不是
e.toString()
!,这样您就可以知道NPE的确切抛出位置。(最后,编辑您的初始帖子以包含完整的stacktrace)

从这里我看到,NPE有很多机会

JSONArray jsonArray = (JSONArray)parser_obj.parse(result); // right operand probably returns null
json_data = jsonArray.getJSONObject(i); // NPE is thrown
因此,在使用它之前,您必须检查
jsonArray
是否为空。如果我是对的,那么您必须检查
result
中发生了什么问题
问题出在php文件的目录中
我使用xampp,在htdocs中,我创建文件夹并将php文件放入其中,但这不是真的,然后放入此代码

> HttpPost httppost=new HttpPost("http://10.0.2.2/insert.php");
我将php文件放入htdocs并使之成为现实

感谢并对我的问题表示歉意

请发布完整的LogCatalo标记你的应用程序crashesI发送所有logcat的行,因为我使用了try and catch logcat显示了这条消息,复制打印的内容,而不是你认为我们应该看到的内容,854的行是什么?我在try 3和854中的错误不是lineno我的评论,或者在try 3中,但NPE再次出现了错误.try 3中的代码是真的吗?请在
catch
s中使用
e.getStackTrace()
,这样您就可以知道NPE的确切抛出位置。(最后,编辑您的初始帖子以包含完整的stacktrace)