简体   繁体   中英

object reference not set to an instance

I am new to c# and web services... Normally i use python for a long time...

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...

When i try to access it iwth suds (python), my first test function Hello World runs correctly, that shows me my serice is accessible...

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

As i said, i am new to c# and web services... So i could not spot my mistake :(

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 . If so, I would:

  • Examine under what circumstances someFunc may return null and decide whether that is ok or not.
  • If it is OK, add a null check in your service call, so that you don't try to get availableBalance from null references
  • If it's not OK, fix the bug in 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. A return is missing.

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. 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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