0

I am trying to assign some id values to an Array in my application but the Activity keeps crashing and I think it is because of that array.
Before I added it to the code, the data displayed but once I added it things started going wrong.

Here is the Activity code:

private static final String EVENT_POST_URL = "http://10.0.2.2/ZCASPlatform/courseinfo.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_EVENT = "events";
private static final String TAG_POST = "posts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";

String option;
String idnum;
String discuss;
String[] discussId;

private ProgressDialog pDialog;
private JSONArray Details = null;
private ArrayList < HashMap < String, String >> List;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnLogout = (Button) findViewById(R.id.logout);
    TxtFname = (TextView) findViewById(R.id.fnametxt);
    Bundle required = getIntent().getExtras();
    option = required.getString("keyPress");
    idnum = required.getString("keyNum");
    btnLogout.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            Intent login = new Intent(getApplicationContext(), LoginActivity.class);
            login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(login);
            finish();
        }
    });
}

@Override
protected void onResume() {
    super.onResume();
    new LoadCourses().execute();
}

public void updateJSONdata() {
    List = new ArrayList < HashMap < String, String >> ();
    JSONParser jParser = new JSONParser();
    List < NameValuePair > params = new ArrayList < NameValuePair > ();
    params.add(new BasicNameValuePair("idnumber", idnum));
    params.add(new BasicNameValuePair("pressed", option));
    JSONObject json = jParser.makeHttpRequest(EVENT_POST_URL, "POST", params);
    try {
        if (option.equals("event")) {
            Details = json.getJSONArray(TAG_EVENT);
        } else if (option.equals("forum")) {
            Details = json.getJSONArray(TAG_POST);
        }
        for (int i = 0; i < Details.length(); i++) {
            int pos = 0;
            JSONObject c = Details.getJSONObject(i);
            String id = c.getString(TAG_ID);
            String name = c.getString(TAG_NAME);
            discussId[pos] = id;
            HashMap < String, String > map = new HashMap < String, String > ();
            map.put(TAG_ID, id);
            map.put(TAG_NAME, name);
            List.add(map);
            pos++;
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

public void updateList() {
    ListAdapter adapter = new SimpleAdapter(this, List, R.layout.activty_view_course,
    new String[] {
        TAG_ID, TAG_NAME
    }, new int[] {
        R.id.idnumber, R.id.fullname
    });
    setListAdapter(adapter);
    ListView lv = getListView();
}

public class LoadCourses extends AsyncTask < Void, Void, Boolean > {@Override
    protected void onPreExecute() {
        pDialog = new ProgressDialog(EventForumActivity.this);
        pDialog.setMessage("Loading Information");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected Boolean doInBackground(Void...arg0) {
        updateJSONdata();
        return null;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        pDialog.dismiss();
        updateList();
    }
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    String discuss = discussId[position];
    try {
        Bundle passing = new Bundle();
        passing.putString("discuss", discuss);
        Class openClass = Class.forName("com.example.zcas.ForumActivity");
        Intent newPage = new Intent(EventForumActivity.this, openClass);
        newPage.putExtras(passing);
        startActivity(newPage);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

And my log cat displays:

04-19 20:23:09.381: E/AndroidRuntime(814): FATAL EXCEPTION: AsyncTask #3
04-19 20:23:09.381: E/AndroidRuntime(814): java.lang.RuntimeException: An error occured while executing doInBackground()
04-19 20:23:09.381: E/AndroidRuntime(814): Caused by: java.lang.NullPointerException
04-19 20:23:09.381: E/AndroidRuntime(814):  at com.example.zcas.EventForumActivity.updateJSONdata(EventForumActivity.java:113)
4
  • On which line does the exception occur? Commented Apr 19, 2015 at 18:38
  • in the updateJSONdata() on the line where i'm assigning the value of id to the array discuss[pos ] Commented Apr 19, 2015 at 18:40
  • 1
    You never allocate memory for this array, sure it will get NullPointer Commented Apr 19, 2015 at 18:41
  • thanks alot. its working. cant believe i missed that Commented Apr 19, 2015 at 18:50

2 Answers 2

2

The discussId array is never instantiated:

discussId = new String[whateverLength];
Sign up to request clarification or add additional context in comments.

Comments

1

prior to your for loop

for (int i = 0; i < Details.length(); i++)

add this:

discussId = new String[Details.length()];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.