-2

i have a listview with the special id @android:id/list

the json data has just an array with objects in it.

public class MainActivity extends ListActivity {

String KEY_DESCRIPT = "DESCRIPTION:";

String KEY_NAME = "NAME:";
String line = null;
JSONArray datalist=null;
JSONObject getobj=null;
BufferedReader br;
StringBuilder sb;
String Web=null;
JSONArray jresult=null;
URL url;
HttpURLConnection cn;
String TAG="MYACTIVITY";
String name = null;
String [] sortedlist;
ProgressBar loading;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    loading= (ProgressBar) findViewById(R.id.progressBar1);
    Web = "http://api.mtgdb.info/search/Dragon";
    new jsonGet().execute(Web);
}

protected class jsonGet extends AsyncTask<String , Void, JSONArray>{

    @Override
    protected JSONArray doInBackground(String... params) {
        try {
            url= new URL(params[0]);
            cn=(HttpURLConnection) url.openConnection();
            br= new BufferedReader(new InputStreamReader(cn.getInputStream()));
            sb= new StringBuilder();
            while((line = br.readLine())!=null){
                sb.append(line+"\n");   
            }
            br.close();



            datalist= new JSONArray(sb.toString());


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {

            e.printStackTrace();
        }

        return datalist;

    }

    @Override
    protected void onPostExecute(JSONArray result) {
        super.onPostExecute(result);
        jresult=result;
        passList();

        }

    }



public void passList() {
    loading.setVisibility(View.INVISIBLE);
    if (jresult==null){
        Log.e(TAG, "ERROR");

    }else{
        ArrayList<HashMap<String, String>> newlist =new ArrayList<HashMap<String,String>>();

    for (int i =0;i<jresult.length();i++){
        try{
        getobj= jresult.getJSONObject(i);
        name=getobj.getString("name");
        String description =getobj.getString("description");
        HashMap<String, String> secondlist= new HashMap<String, String>();
        secondlist.put(KEY_NAME, name);
        secondlist.put(KEY_DESCRIPT, description);
        newlist.add(secondlist);
        }catch (Exception e){
            Log.i(TAG, "THis error");
        }

    }

    Arrays.sort(sortedlist);

    String [] keys ={KEY_NAME,KEY_DESCRIPT};
    int [] ids ={android.R.id.text1,android.R.id.text2};
    SimpleAdapter adapter = 
            new SimpleAdapter(this, 
                    newlist, android.R.layout.simple_list_item_2, keys, ids);
    setListAdapter(adapter);
    }
}

Stacktrace:

1-06 22:49:10.019: D/AndroidRuntime(1476): Shutting down VM
11-06 22:49:10.023: W/dalvikvm(1476): threadid=1: thread exiting with uncaught exception (group=0xa4d50b20)
11-06 22:49:10.023: E/AndroidRuntime(1476): FATAL EXCEPTION: main
11-06 22:49:10.023: E/AndroidRuntime(1476): Process: com.example.mtg, PID: 1476
11-06 22:49:10.023: E/AndroidRuntime(1476): java.lang.NullPointerException
11-06 22:49:10.023: E/AndroidRuntime(1476):     at java.util.ComparableTimSort.sort(ComparableTimSort.java:142)
11-06 22:49:10.023: E/AndroidRuntime(1476):     at java.util.Arrays.sort(Arrays.java:1970)
11-06 22:49:10.023: E/AndroidRuntime(1476):     at com.example.mtg.MainActivity.passList(MainActivity.java:122)
11-06 22:49:10.023: E/AndroidRuntime(1476):     at com.example.mtg.MainActivity$jsonGet.onPostExecute(MainActivity.java:91)
11-06 22:49:10.023: E/AndroidRuntime(1476):     at com.example.mtg.MainActivity$jsonGet.onPostExecute(MainActivity.java:1)
11-06 22:49:10.023: E/AndroidRuntime(1476):     at android.os.AsyncTask.finish(AsyncTask.java:632)
11-06 22:49:10.023: E/AndroidRuntime(1476):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-06 22:49:10.023: E/AndroidRuntime(1476):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
11-06 22:49:10.023: E/AndroidRuntime(1476):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-06 22:49:10.023: E/AndroidRuntime(1476):     at android.os.Looper.loop(Looper.java:136)
11-06 22:49:10.023: E/AndroidRuntime(1476):     at android.app.ActivityThread.main(ActivityThread.java:5001)
11-06 22:49:10.023: E/AndroidRuntime(1476):     at java.lang.reflect.Method.invokeNative(Native Method)
11-06 22:49:10.023: E/AndroidRuntime(1476):     at java.lang.reflect.Method.invoke(Method.java:515)
11-06 22:49:10.023: E/AndroidRuntime(1476):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-06 22:49:10.023: E/AndroidRuntime(1476):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-06 22:49:10.023: E/AndroidRuntime(1476):     at dalvik.system.NativeStart.main(Native Method)
BatScream
  • 19,260
  • 4
  • 52
  • 68
Andrew
  • 1
  • Please read this help page about formatting a question: http://stackoverflow.com/help/formatting – Tom Nov 06 '14 at 23:09

1 Answers1

1

Your exception occurs here:

Arrays.sort(sortedlist);

The problem is, that you're declaring that variable as String [] sortedlist;, but you never initialize that variable with an array (i.e. sortedlist = new String[x];). That means that sortedlist remains null (this is the standard value of a new object type) and this causes the NullPointerException.

Check why you never do that and update/fix your code.

Mind that it is prohibited to write null elements into that array. That means, the array String[] sortedlist = {"a", null, "b"}; would also cause a NullPointerException if you call Arrays.sort(sortedlist).

Tom
  • 16,842
  • 17
  • 45
  • 54
  • Thank you so should I just put =new string []; nothing in the field? – Andrew Nov 07 '14 at 00:19
  • @Andrew Why should you do this? What is the purpose of sorting an empty array? Think about what you're trying to sort there and then make sure, that this data is in this array. – Tom Nov 07 '14 at 00:27
  • Well it was working until I decided to try the hashmap; I don't understand. It did sort and the json names were in order I just got confused – Andrew Nov 07 '14 at 00:33
  • @Andrew It sounds like you're not using the variable `sortedlist` anymore. If this is the case, then just remove the line `Arrays.sort(sortedlist);` and the variable `String [] sortedlist;`. Then try this question about sorting a `HashMap`: http://stackoverflow.com/questions/780541/how-to-sort-a-hashmap-in-java – Tom Nov 07 '14 at 00:45