2

I have a class extending listFragment. I have written code for fetching json response in a method which is called in oncreate of the class. For fetching json in background I have created new inner class which extends asyncTask. I can get the jsonarrays and strings in the json response in the logcat. But when I try to save them in a string array and pass them my custom baseadapter, I get a nullpointer exception.

    public class OnlineInfo extends ListFragment {

public static String result;
public String[] Technologies1;
public String[] TechnologyDescription1;

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
     downloadjsonresponse();



}

 public class Download extends AsyncTask<String, Void, String>
    {


        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            StringBuilder stringbuilder = new StringBuilder();
            HttpClient client = new DefaultHttpClient();
            HttpGet url = new HttpGet(params[0]);
            try
            {
                Log.d("in background", "in background");
                HttpResponse response = client.execute(url);
                HttpEntity entity = response.getEntity();
                InputStream stream = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
                String line ;
                while((line = reader.readLine()) != null)
                {
                    stringbuilder.append(line);
                }
            }

            catch(ClientProtocolException e)
            {
                Log.d("error in clientprotocol", "error");
                e.printStackTrace();
            }
            catch(IOException e)
            {
                Log.d("error in IO", "error");
                e.printStackTrace();
            }
            return stringbuilder.toString();


        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub

            OnlineInfo.result = result;
            try {
                JSONObject jsonobject = new JSONObject(result);
                JSONArray raja = jsonobject.getJSONArray("Technologies");
                //String raja = jsonobject.getJSONArray("Technologies").getJSONObject(0).getString("desc");
                //Log.d("desc:", raja);
                for(int i=0; i<raja.length();i++)
                {

                    Technologies1[i] = raja.getJSONObject(i).getString("name");
                    TechnologyDescription1[i] = raja.getJSONObject(i).getString("desc");
                    Log.d("technology :", raja.getJSONObject(i).getString("name"));
                    Log.d("technologydescription :", raja.getJSONObject(i).getString("desc"));
                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.d("desc:", "error");
            }
            getListView().setAdapter(new AdapterForOnlineInfo(getActivity(), OnlineInfo.this.Technologies1, OnlineInfo.this.TechnologyDescription1));


        }

        }

 public void downloadjsonresponse()
    {
        Download jsonresponse = new Download();
        jsonresponse.execute("http://www.avantajsoftwares.com/result.json");

    }
}

I could get the results in logcat whenever I comment these two lines in for-loop:

    Technologies1[i] = raja.getJSONObject(i).getString("name");
TechnologyDescription1[i] = raja.getJSONObject(i).getString("desc");

Don't know what's going wrong. Please somebody provide me some insight....:-(

Yury
  • 20,618
  • 7
  • 58
  • 86
vivek
  • 45
  • 10

1 Answers1

0

I think you need to allocate space in your string arrays to hold the new strings.

Just before your for loop, try putting something like this:

Technologies1 = new String[raja.length()];
TechnologyDescription1 = new String[raja.length()];
Sparky
  • 8,437
  • 1
  • 29
  • 41