force close of application

Go To StackoverFlow.com

0

I am trying to access my json data "name" on jsp page from android app.. I am getting error and the application get force closed.

my jsp code is as follows...

<%@page contentType="text/html; charset=UTF-8"%>
<%@page import="org.json.simple.JSONObject"%>
<%
JSONObject obj=new JSONObject();
obj.put("name","foo");
obj.put("num",new Integer(100));
obj.put("balance",new Double(1000.21));
obj.put("is_vip",new Boolean(true));
obj.put("nickname",null);
out.print(obj);
out.flush();
%>

my android java code is as follows...

package com.campuspro.start;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class JsonDemo extends Activity 
{
TextView tv=(TextView)findViewById(R.id.jsonresult);
HttpClient client=new DefaultHttpClient();
String url="http://10.0.2.2:7001/f/json.jsp";

@Override
protected void onCreate(Bundle savedInstanceState) {

    // TODO Auto-generated method stub
    try{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result_json);
    HttpGet get=new HttpGet(url);
    HttpResponse r=client.execute(get);
    int status=r.getStatusLine().getStatusCode();
    if(status==200)
    {
        HttpEntity e=r.getEntity();
        String data=EntityUtils.toString(e);
        JSONArray arr=new JSONArray(data);
        JSONObject last=arr.getJSONObject(0);
        String result=last.getString("name");
        tv.setText(result);
    }

}catch(Exception e)
{

}
}

}

my log cat error on eclipse is...

04-04 09:30:53.910: D/AndroidRuntime(345): Shutting down VM
04-04 09:30:53.910: W/dalvikvm(345): threadid=1: thread exiting with uncaught exception           (group=0x4001d800)
04-04 09:30:54.190: E/AndroidRuntime(345): FATAL EXCEPTION: main
04-04 09:30:54.190: E/AndroidRuntime(345): java.lang.RuntimeException: Unable to   instantiate activity ComponentInfo{com.campuspro.start/com.campuspro.start.JsonDemo}:   java.lang.NullPointerException
04-04 09:30:54.190: E/AndroidRuntime(345):  at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
04-04 09:30:54.190: E/AndroidRuntime(345):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-04 09:30:54.190: E/AndroidRuntime(345):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-04 09:30:54.190: E/AndroidRuntime(345):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-04 09:30:54.190: E/AndroidRuntime(345):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 09:30:54.190: E/AndroidRuntime(345):  at android.os.Looper.loop(Looper.java:123)
04-04 09:30:54.190: E/AndroidRuntime(345):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-04 09:30:54.190: E/AndroidRuntime(345):  at java.lang.reflect.Method.invokeNative(Native Method)
04-04 09:30:54.190: E/AndroidRuntime(345):  at java.lang.reflect.Method.invoke(Method.java:521)
04-04 09:30:54.190: E/AndroidRuntime(345):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-04 09:30:54.190: E/AndroidRuntime(345):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-04 09:30:54.190: E/AndroidRuntime(345):  at dalvik.system.NativeStart.main(Native Method)
04-04 09:30:54.190: E/AndroidRuntime(345): Caused by: java.lang.NullPointerException
04-04 09:30:54.190: E/AndroidRuntime(345):  at android.app.Activity.findViewById(Activity.java:1637)
04-04 09:30:54.190: E/AndroidRuntime(345):  at com.campuspro.start.JsonDemo.<init>(JsonDemo.java:18)
04-04 09:30:54.190: E/AndroidRuntime(345):  at java.lang.Class.newInstanceImpl(Native Method)
04-04 09:30:54.190: E/AndroidRuntime(345):  at java.lang.Class.newInstance(Class.java:1429)
04-04 09:30:54.190: E/AndroidRuntime(345):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-04 09:30:54.190: E/AndroidRuntime(345):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
04-04 09:30:54.190: E/AndroidRuntime(345):  ... 11 more
2012-04-04 04:25
by sagar


2

You have error in

TextView tv=(TextView)findViewById(R.id.jsonresult);

You should set it up after setContentView(R.layout.result_json);

2012-04-04 04:28
by Roman Truba
A Great place to move this declaration, along with the others near it, would be in onCreate after setContentView(...) - Phil 2012-04-04 04:32
thanks @Roman Truba.. but i am not able to see my value of "name" as "foo" on android app .. why is it so?? - sagar 2012-04-04 07:20
@sagar Try to debug your data and jsonarra - Roman Truba 2012-04-04 08:58
how to do that?? - sagar 2012-04-04 17:26
@sagar there is a bug sign in eclipse. http://www.vogella.de/articles/EclipseDebugging/article.htm - Roman Truba 2012-04-05 01:38
Ads