Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all.

I am working on a project that requires an sms to be received and the contents extracted by my C# application.

I am using the GSMcomm library to communicate with a telit EZ-10 GSM modem connected to my computer.

New message notifications are enabled, hence the modem sends a command(containing the location of the new sms) to the PC whenever a new SMS is received.

I am able to receive the SMS fine but i cannot extract the required sms content. I receive the following error during the foreach statement:

"NullReferenceException was unhandled by user code
Object reference not set to an instance of an object."

While debugging, the received sms does not show in the "sms" textbox and the exception does not explicitly occur. The exception only occurs when I set a breakpoint at the foreach statment and then tell the debugger to execute the next statement . The exception occurs at this line: SmsPdu smsrec = message.Data;



Form Code follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using GsmComm.GsmCommunication;
using GsmComm.Interfaces;
using GsmComm.PduConverter;
using GsmComm.Server;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        GsmCommMain comm;
        string actualtext;
        
        public Form1()
        {
         InitializeComponent();
        comm = new GsmCommMain(9, 115200);
        comm.Open();
        comm.EnableMessageNotifications();
        comm.MessageReceived += new   MessageReceivedEventHandler(comm_MessageReceived);

        }

        private void comm_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            IMessageIndicationObject obj = e.IndicationObject;
            MemoryLocation loc = (MemoryLocation)obj;
            DecodedShortMessage[] messages;
            messages = comm.ReadMessages(PhoneMessageStatus.ReceivedUnread, loc.Storage);

            foreach (DecodedShortMessage message in messages)
            {
                SmsDeliverPdu data = new SmsDeliverPdu();

                SmsPdu smsrec = message.Data;
                ShowMessage(smsrec);

            }
        }

        private void ShowMessage(SmsPdu pdu)
        {// Received message
            SmsDeliverPdu data = (SmsDeliverPdu)pdu;

            actualtext = data.UserDataText;
            SetSomeText(actualtext);

            return;
        }
       
        delegate void SetTextCallback(string text);
        private void SetSomeText(string text)
        {
            if (sms.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetSomeText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                sms.Text = text;
            }
        }
    }
}
Posted
Updated 6-Sep-10 0:49am
v3

1 solution

This is most probably because the messages array is set to null after calling the ReadMessages() method.

Thus the problem is probably in the method mentioned above. I can't tell you how to fix it since I have absolutely no knowledge of the particlular library... I suggest reading the documentation carefully.

EDIT: Now I noticed you wrote the error happens "during the foreach statement". If that means it's inside the body of the foreach, then my answer is wrong... Please specify the error a bit better.
 
Share this answer
 
v2
Comments
sach262 6-Sep-10 6:50am    
thanks for your reply! Yes i also suspected maybe an object is being set to null. I have updated the question and specified the error for you better.
Kubajzz 6-Sep-10 7:18am    
If NullReferenceException occurs at the following statement: "SmsPdu smsrec = message.Data;", then it is most probably because "message" is null. Again, I have no idea how to fix it since I have no knowledge of your application and the library you use... Use the debugger to track the state of your variables and I'm sure you'll find the source of your problem.
sach262 6-Sep-10 7:24am    
ok cool , thanks for your help,i will try track the message array through the code and check when it is becoming null.
sach262 6-Sep-10 19:12pm    
On closer inspection of the code during debugging and using breakpoints I have realized that the line: foreach (DecodedShortMessage message in messages) is actually not being executed at all!! For some reason the code just ignores it. Hence the NullReferenceException when I forced the line: SmsPdu smsrec = message.Data to occur in the debugger.
So the real issue is why the foreach statement is not being executed??
Kubajzz 7-Sep-10 3:45am    
This sounds pretty weird. Are you compiling your project using debug configuration, without optimizations disabled etc? A line of code can't just get skipped, especially a control-flow statement such as foreach. Maybe the debugger steps over the line, but that doesn't mean that the line is not executed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900