1

I'm working on a small chat room in android and now when I set a custom ListView I get the following error, any ideas?

public class roomActivity extends Activity
{
    ListView mainListView;
    ProgressDialog progressDialog;
    String[] jsonUsernameArray, jsonMessageArray;
    UpdateTask mTask = null;

    @Override
    public void onCreate(Bundle activityCycle)
    {
        super.onCreate(activityCycle);
        setContentView(R.layout.room_layout);
        mainListView = (ListView)findViewById(R.id.roomListView);
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setMessage("Fetech the data...");
        progressDialog.setCancelable(false);
        progressDialog.show();
        mTask = new UpdateTask();
        mTask.execute();
    }

    public class UpdateTask extends AsyncTask<Void, Void, JSONObject> 
    {
        JSONArray _roomData = null;
        protected void onPostExecute(JSONObject jsonResult)
        {
            try 
            {
                _roomData = jsonResult.getJSONArray("roomInfo");
                for (int cnt = 0; cnt < _roomData.length(); cnt++)
                {
                    JSONObject c = _roomData.getJSONObject(cnt);
                    jsonUsernameArray[cnt] = c.getString("name");
                    jsonMessageArray[cnt] = c.getString("message");
                }
            } 
            catch (JSONException e) 
            {
                e.printStackTrace();
            }
            mainListView.setAdapter(new customAdapter());
            progressDialog.dismiss();
        }

        @Override
        protected JSONObject doInBackground(Void... backgroundProcess)
        {
            UserFunctions userFunction = new UserFunctions();
            return userFunction.sendMessageToROOM("50", "Reza", "Salam dadash :-)", "89988");    
        }   
    }

    public class customAdapter extends BaseAdapter
    {

        public int getCount() 
        {
            return jsonMessageArray.length;
        }

        public Object getItem(int itemData) 
        {
            return null;
        }

        public long getItemId(int itemPosition) 
        {
            return itemPosition;
        }

        public View getView(int _position, View mainView, ViewGroup mainViewGroup) 
        {
            LayoutInflater maniInFlater = getLayoutInflater();
            View _row = null;
            TextView _usernameText, _messageText;
            if (_position % 2 == 0)
            {
                _row = maniInFlater.inflate(R.layout.conversion_layout_odd, mainViewGroup, false);
                _usernameText = (TextView)_row.findViewById(R.id.txtUsername);
                _messageText = (TextView)_row.findViewById(R.id.txtMessage);
                _usernameText.setText(jsonUsernameArray[_position]);
                _messageText.setText(jsonMessageArray[_position]);  
            }
            else
            {
                //do sth...
            }
            return _row;
        }   

    }
}

Logcat errors:

08-01 00:53:04.065: E/AndroidRuntime(17114): FATAL EXCEPTION: main
08-01 00:53:04.065: E/AndroidRuntime(17114): java.lang.NullPointerException
08-01 00:53:04.065: E/AndroidRuntime(17114):    at com.merlingames.thermotalk.roomActivity$UpdateTask.onPostExecute(roomActivity.java:52)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at com.merlingames.thermotalk.roomActivity$UpdateTask.onPostExecute(roomActivity.java:1)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at android.os.AsyncTask.finish(AsyncTask.java:602)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at android.os.Looper.loop(Looper.java:137)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at android.app.ActivityThread.main(ActivityThread.java:4575)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at java.lang.reflect.Method.invokeNative(Native Method)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at java.lang.reflect.Method.invoke(Method.java:511)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at dalvik.system.NativeStart.main(Native Method)

Edited :

08-01 01:26:07.260: E/AndroidRuntime(18381):    at android.app.ActivityThread.main(ActivityThread.java:4575)
08-01 01:27:36.327: W/dalvikvm(18870): threadid=1: thread exiting with uncaught exception (group=0x40a561f8)
08-01 01:27:36.327: E/AndroidRuntime(18870): FATAL EXCEPTION: main
08-01 01:27:36.327: E/AndroidRuntime(18870): java.lang.NullPointerException
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.widget.AbsListView.obtainView(AbsListView.java:2024)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.widget.ListView.onMeasure(ListView.java:1155)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.view.View.measure(View.java:12723)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:579)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:392)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.view.View.measure(View.java:12723)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.view.View.measure(View.java:12723)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.view.View.measure(View.java:12723)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.view.View.measure(View.java:12723)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.os.Looper.loop(Looper.java:137)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.app.ActivityThread.main(ActivityThread.java:4575)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at java.lang.reflect.Method.invokeNative(Native Method)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at java.lang.reflect.Method.invoke(Method.java:511)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at dalvik.system.NativeStart.main(Native Method)
5
  • 2
    What is roomActivity.java line 52? Commented Jul 31, 2012 at 20:30
  • @kcoppock jsonUsernameArray[cnt] = c.getString("name"); Commented Jul 31, 2012 at 20:45
  • Then either c or jsonUsernameArray are null. Most likely jsonUsernameArray because you never initialize it. Commented Jul 31, 2012 at 20:47
  • @kcoppock No, it's not ! Commented Jul 31, 2012 at 20:49
  • Edited. One of the two is. If that is line 52, then the only possibilities are that one of those two variables is null. Commented Jul 31, 2012 at 20:51

2 Answers 2

1

You should add this line to initialize your array:

_roomData = jsonResult.getJSONArray("roomInfo"); 

//Add this line
jsonUsernameArray = new String[_roomData.length()];
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, That error has been gone, But I get new errors. See my edit please.
You're returning null in getItem(). So yes, obtaining a View is going to give you a NullPointerException. Return the item at that position in the array, not null.
No, you need to be able to figure that sort of thing out yourself, it's outside of the scope of the original question, and my comment was basically pseudocode.
If you're getting null pointer exceptions, the best thing you can do is open a debugger, look for the thingies that're null, and figure out if they're supposed to be null or not. So to make that delicious soup of not-at-all-otherworldly-origins, you'll need two parts debugger, one part eyeballs, and three parts documentation. After that, pour it into a bowl made of obsidian and you're good to go.
Has been solved, Just I set View _row = null; to View _row;, That was not related to getItem().
|
1

Your array jsonUsernameArray is never initialized. Learn how to use arrays correctly:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

1 Comment

Thanks, That error has been gone, But I get new errors. See my edit please.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.