Home > database >  Method to read all the messages on android returning null
Method to read all the messages on android returning null

Time:07-29

I am developing a Message application for Android OS and I searched on this site and found a page that describes very well how to build a class that extends a native Java language Object that can hold all the details of a single sms in one instance. The class that represents an SMS object looks like below

 public class IMessage:Java.Lang.Object
    {
        //data field to hold the message id
        private string _message_id;
        //hold the message address in a string
        private string _address;
        private string _msg;
        private string _readState;
        private string _time;
        private string _folderName;
        
        //define the getter for the id
        public string id
        {
            get => _message_id;
            set => id = value;
        }
        public string Address
        {
            get => _address;
            set => _address = value;
        }

        public string Message
        {
            get => _msg;
            set => _msg = value;
        }

        public string ReadState
        {
            get => _readState;
            set => _readState = value;
        }

        public string Time
        {
            get => _time;
            set => _time = value;
        }

        public string FolderName
        {
            get => _folderName;
            set => _folderName = value;
        }
        
    }

I then tried to use this method to read all the text messages whether categorized as inbox or sent but the method is returning null because I captured the error from the logcat Attempt to invoke interface method 'int java.util.List.size()' on a null object reference. So that is because the Android System tries to build my list view and before doing that it tries to check the number of items in the array first but then the array is null. So help me figure out why the method below is returning null instead of a list of messages

List<IMessage> GetMessages()
        {
            //declare the content uri for messages
            string message_uri = "content://sms/";
            List<IMessage> lstSms = new List<IMessage>();
            IMessage sms = new IMessage();
            Uri message = Uri.Parse(message_uri);
            //get the content resolver
            ContentResolver cr = ContentResolver;
            ICursor cursor = cr.Query(message, null, null, null, null);
            int totalSmsCount = cursor.Count;
            //check to avoid null pointer exception
            if (cursor.MoveToFirst())
            {
                for (int i = 0; i < totalSmsCount; i  )
                {
                    sms = new IMessage();
                    sms.id = cursor.GetString(cursor.GetColumnIndexOrThrow("_id"));
                    sms.Address = cursor.GetString(cursor.GetColumnIndexOrThrow("address"));
                    sms.Message = cursor.GetString(cursor.GetColumnIndexOrThrow("body"));
                    sms.ReadState = cursor.GetString(cursor.GetColumnIndexOrThrow("read"));
                    sms.Time = cursor.GetString(cursor.GetColumnIndexOrThrow("date"));
                    //filter for inbox messages only
                    if (cursor.GetString(cursor.GetColumnIndexOrThrow("type")).Contains("1"))
                    {
                        sms.FolderName = "inbox";
                    }
                    else
                    {
                        sms.FolderName = "sent";
                    }
                    //add the sms object to our list 
                    lstSms.Add(sms);
                    //move to the next iteration 
                    cursor.MoveToNext();
                }
            }
            //close the cursor
            cursor.Close();
            return lstSms;
        }

I asked the Android operating system to let me read the text messages by declaring this permission in my manifest and also requesting it from the user at runtime

<uses-permission android:name="android.permission.READ_SMS"/>

CodePudding user response:

The problem was caused by a typo in the class IMessage, I assigned to the parameter name instead of the in class data field

//should use below
 public string id
        {
            get => _message_id;
            set => _message_id = value;
        }
//instead of
  public string id
        {
            get => _message_id;
            set => id = value;
        }
  • Related