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

I m using gsmcomm library.And while trying to send sms after i open ComPort and connect , error occurs "Object reference not set to an instance of an object"

what can be reason for it ?

This is image :


http://img423.yukle.tc/images/7996sms_sistem.png[^]

you can download solution folder here and you can test : http://www.ebitech.net/smsTest.rar

If you don't want to download the code is listed below:



C#
public SmsSubmitPdu pdu;
   public GsmCommMain comm=null;
    protected void Page_Load(object sender, EventArgs e)
    {

       
    }
  



   
    protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
    {

    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        pdu = new SmsSubmitPdu("test", "05452756736");
        comm.Open();
        comm.SendMessage(pdu);
        comm.Close();

        Response.Write("mesaj gonderildi");

    }
    protected void btnConnect_Click1(object sender, EventArgs e)
    {
        comm = new GsmCommMain(int.Parse(txtPort.Text), 3600, 360);
        comm.Open();
        Response.Write("port baglandi");


    }
Posted
Updated 20-Aug-12 10:39am
v3
Comments
[no name] 20-Aug-12 16:39pm    
[Edit] Removed screaming [/Edit]

As you're using Response.Write, I assume this is a web site. You should tag your questions properly. You should also read up on how ASP.NET works and perhaps learn to use a debugger. The error means what it says, and it is right. Even though you didn't tell us what line creates the error, it's obvious.

C#
comm = new GsmCommMain(int.Parse(txtPort.Text), 3600, 360);
        comm.Open();
        Response.Write("port baglandi");


This is well and good.

C#
pdu = new SmsSubmitPdu("test", "05452756736");
  comm.Open();
  comm.SendMessage(pdu);
  comm.Close();

  Response.Write("mesaj gonderildi");


Here, comm is null, because it's a new postback and the web is stateless, your page gets recreated on each postback and can't remember the variables you created last time. You need to not have a 'connect' step, the samples you're looking at, assume this is not a web app.
 
Share this answer
 
ASP.NET by default doesn't preserve the state of the variables defined at page level So when the first time you hit btnConnect, the comm object gets initialised but also postback happens and the page is re-rendered and the variables are reset i.e. to null so when you hit btnSend, comm object is null again and hence the "object reference not set to instance of object" error. It simply means that you are trying to use an object that hasn't been initialised. Your best bet is to move this object creation and sending functionality to a seperate business or operation layer project which will reside on server side as a dll and then you would be able to use it correctly.

hope this helps.

[EDIT:] or you can use ViewState or Session variables to store and preserve this comm object in between postbacks
 
Share this answer
 
v2
Comments
beratxt 20-Aug-12 16:34pm    
What is the corrected code ? Can you tell me exactly ? Please inform me .I want to use opened port in all page that i open.Do you suggest to use Session or Cookie for port value ?
I.explore.code 20-Aug-12 16:37pm    
try doing something like this, it should work:

protected void btnConnect_Click1(object sender, EventArgs e)
{
comm = new GsmCommMain(int.Parse(txtPort.Text), 3600, 360);
comm.Open();
//store in session for later use
Session["Comm"] = comm;
Response.Write("port baglandi");
}

and here:
protected void btnSend_Click(object sender, EventArgs e)
{
pdu = new SmsSubmitPdu("test", "05452756736");
//re-populate the comm object from the session.
comm = Session["Comm"];
comm.SendMessage(pdu);
comm.Close();

Response.Write("mesaj gonderildi");
}
give it a go, it should work. I personally don't use Session variables much so i prefer moving all such functionalities in a separate class residing on server side and then using it from there. But for now, see if this works in your case.
beratxt 20-Aug-12 16:44pm    
comm = Session["Comm"]; has a problem cant imlicitly convert to GsmComm... etc ?
I.explore.code 20-Aug-12 16:49pm    
try typecasting it: comm = (GsmCommMain)Session["Comm"];
beratxt 20-Aug-12 16:52pm    
Thanks gladiatron ! the problem has been solved :)

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