对象引用未设置为GsmmComLibrary中的对象实例吗? [英] Object reference not set to an instance of an object in GsmmComLibrary ?

查看:72
本文介绍了对象引用未设置为GsmmComLibrary中的对象实例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好;

我正在使用gsmcomm库.打开ComPort并连接后尝试发送短信时,发生错误对象引用未设置为对象的实例"

可能是什么原因呢?

这是图像:


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

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:



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");


    }

推荐答案

在您使用Response.Write时,我假设这是一个网站.您应该正确标记问题.您还应该阅读ASP.NET的工作原理,并可能学习使用调试器.该错误表示它的意思,这是正确的.即使您没有告诉我们造成错误的行,也很明显.

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.

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



这很好,很好.



This is well and good.

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

  Response.Write("mesaj gonderildi");



在这里,comm为null,因为它是一个新的回发并且Web是无状态的,因此每次回发都会重新创建您的页面,并且无法记住您上次创建的变量.您不需要进行连接"步骤,您正在查看的示例就假定这不是Web应用程序.



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.


默认情况下,ASP.NET不会保留在页面级别定义的变量的状态,因此,当您第一次点击btnConnect时,将初始化comm对象,但也会发生回发,并且页面将重新呈现,并且变量将重置为null,因此当您点击btnSend时,comm object再次为null,因此出现对象引用未设置为对象实例"错误.这仅表示您正在尝试使用尚未初始化的对象.最好的选择是将对象创建和发送功能移到一个单独的业务或操作层项目中,该项目将作为dll驻留在服务器端,然后就可以正确使用它了.

希望对您有所帮助.

或您可以使用ViewState或Session变量在回发之间存储和保留此comm对象
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.

or you can use ViewState or Session variables to store and preserve this comm object in between postbacks


这篇关于对象引用未设置为GsmmComLibrary中的对象实例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆