0

I have method which return the latitude of the location

public double[] getlat(){
     double lat[]=new double[20];

     JSONObject json = JSONFunction.getJSONfromURL("https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=1000&types=bank&sensor=true&key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");    
try{
    JSONArray  JArray = json.getJSONArray("results");
       Log.v(TAG, "getting results");
    for(int i=0;i<JArray.length();i++){                     
        JSONObject e = JArray.getJSONObject(i);
        JSONObject location=e.getJSONObject("geometry").getJSONObject("location");
        lat[i] = location.getDouble("lat");     
}catch(JSONException e)        {
     Log.e("log_tag", "Error parsing data "+e.toString());
}

return lat;

}

I recieved this array in main activity as

    public class Test extends Activity {
   private static final String TAG = "test";

/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle icicle) {
    // Be sure to call the super class.
    super.onCreate(icicle);
     StringBuilder sb =new StringBuilder();
    double a[]=getlat();
    for(int i=0;i<a.length;i++) {
        sb.append(a[i]+"\n");
    }
 TextView tv=(TextView)findViewById(R.id.text);
 tv.setText(sb);
 }

Giving me nullpointor exception but in logcat i can see the latitude array displayed The problem is in returing the lat. Correct me where i am wrong..

Kara
  • 6,115
  • 16
  • 50
  • 57
Sunny
  • 14,522
  • 15
  • 84
  • 129

2 Answers2

1

you din't use setContentView() in the onCreate() method..

tv.setText(sb); , so this line is giving you the error.. as it can't find the textView.

Yashwanth Kumar
  • 28,931
  • 15
  • 65
  • 69
1

Your Variable sb is a StringBuilder, kindly update your code this

tv.setText(sb.toString());

Lucifer
  • 29,392
  • 25
  • 90
  • 143