简体   繁体   English

对象引用未设置为实例

[英]object reference not set to an instance

I am new to c# and web services... Normally i use python for a long time... 我是C#和Web服务的新手...通常我使用python很长时间了...

I need to wirete a web service that will be run on IIS, local debugging was successful, i deploy it to my local test server but since that is ona another computer, i could not test it completely via a web browser... 我需要连接将在IIS上运行的Web服务,本地调试成功,我将其部署到本地测试服务器,但是由于那是另一台计算机,因此我无法通过Web浏览器对其进行完整的测试...

When i try to access it iwth suds (python), my first test function Hello World runs correctly, that shows me my serice is accessible... 当我尝试使用suds(python)访问它时,我的第一个测试函数Hello World正确运行,这表明我可以访问我的服务...

but when i try to call a service funton that accepts a parameter and return a custon defined data type, my service simply returns object reference not set to an instance of an object 但是,当我尝试调用接受参数的服务函数并返回custon定义的数据类型时,我的服务只是返回object reference not set to an instance of an object

As i said, i am new to c# and web services... So i could not spot my mistake :( 如我所说,我是C#和Web服务的新手...所以我无法发现我的错误:(

public class Balance{
    private decimal _currentBalance;
    public decimal currentBalance
    {
        get { return _currentBalance; }
        set { _currentBalance = value; }
    }
}



public class Service1 : System.Web.Services.WebService
{
    private string _deviceId;

    public string deviceId
    {
        get { return _deviceId; }
        set { _deviceId = value; }
    }
    CrAc conn;
    CResult connResult;

    private void connectToServer()
    {
        conn= new CrAc();
        connResult= conn.Connect(deviceId);
    }

    private bool connectionControl()
    {
        return connResult.CRCStatus;
    }

    [WebMethod(Description="asdf")]
    public Balance checkBalance(string deviceId) {
        Balance balance = new balance {currentBalance= 0.00m};
        this.deviceId = deviceId;
        connectToServer();
        if (connectionControl())
        {
            XResult askBalance = conn.someFunc(deviceId);
            balance.currentBalance = askBalance.availableBalance;
        }
     return balance;
    }
}

What am i missing? 我想念什么?

It's impossible to be certain based on the given information, but my bet is that baglanti.someFunc(deviceId) returns null , and that you get an exception when trying to get bakiyeSorgusu.availableBalance . 根据给定的信息无法确定,但是我敢打赌, baglanti.someFunc(deviceId)返回null ,并且在尝试获取bakiyeSorgusu.availableBalance时会遇到bakiyeSorgusu.availableBalance If so, I would: 如果是这样,我会:

  • Examine under what circumstances someFunc may return null and decide whether that is ok or not. 检查在什么情况下someFunc可能返回null并确定是否可以。
  • If it is OK, add a null check in your service call, so that you don't try to get availableBalance from null references 如果可以,请在服务调用中添加一个空检查,这样您就不会尝试从空引用中获取availableBalance
  • If it's not OK, fix the bug in someFunc . 如果someFunc ,请修复someFunc的错误。

As a side note, I would recommend you to write code in English if possible (at least when posting code samples online). 作为附带说明,我建议您尽可能用英语编写代码(至少在在线发布代码示例时)。 That way the code will make sense to a lot more people. 这样,代码将对更多人有意义。

Your method bakiyeSor isn't return a value. 您的方法bakiyeSor没有返回值。 A return is missing. 缺少return

Finally, we solve the problem... 最后,我们解决了这个问题...

I was using a DLL that i got from api provider, which has some control function for cpu and network device id's. 我使用的是api提供程序提供的DLL,该DLL具有对cpu和网络设备ID的控制功能。 My development machine was a win7, but on the test server, we have virtual win7 that runs on virtual box... Since virtual Operating systems do not have real cpu or network IDs', api DLL fails on a virtual operating system,but runs perfectly on a real machine... 我的开发机器是win7,但在测试服务器上,我们有在虚拟盒子上运行的虚拟win7 ...由于虚拟操作系统没有真正的cpu或网络ID,因此api DLL在虚拟操作系统上会失败,但可以运行完美地在真实的机器上...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM