-1

I need some help. I am getting the exception in code line "e_svr.svr = svr;". I don't know why because svr is not null. Any help will be appreciated. I am trying to save an instance of the object in a Dictionary.

  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,ConcurrencyMode=ConcurrencyMode.Multiple)]   
public class EPS_Service : IEPS_Service
{
    public decimal cal;
    static Dictionary<string, EPS_Instance> OpenConnections =
        new Dictionary<string, EPS_Instance>();

    public invoice_data Invoice_to_Client(input_params inputparams)
    {


        EPS_Instance e_svr = new EPS_Instance();
        invoice_data Invoice = new invoice_data();

        if (OpenConnections.TryGetValue(inputparams.VSId + inputparams.Username, out e_svr))
        {
            Trace.WriteLine("FOUND!");
            e_svr.svr.PrepareByMoney(e_svr.ck, inputparams.VSId, inputparams.meter_number, inputparams.amount, 0, ref cal);
            object tr_bl = e_svr.svr.GetInvoiceData(e_svr.ck, e_svr.svr.Confirm(e_svr.ck));
            Invoice.invoice_header = fill_invoice_header(tr_bl);
            Invoice.invoice_body = fill_invoice_body(tr_bl);
            Invoice.invoice_extra = fill_invoice_extra(tr_bl);
            Invoice.invoice_steps = fill_invoice_steps(tr_bl);
            Invoice.invoice_transfer = fill_invoice_transfer(tr_bl);
        }
        else
        {
            Trace.WriteLine("NOT FOUND");
            EPS30Ora.EPS30Svr svr = new EPS30Ora.EPS30Svr();
            if (svr.LogOnEx("EDENOR", inputparams.VSId, inputparams.Username, inputparams.Password, "EPS30Ora", ref inputparams.ck) == EPS30Ora.LogonResult.psOK)
            {
                e_svr.ck = inputparams.ck;
                e_svr.svr = svr; 
                OpenConnections.Add(inputparams.VSId + inputparams.Username, e_svr);
                svr.PrepareByMoney(inputparams.ck, inputparams.VSId, inputparams.meter_number, inputparams.amount, 0, ref cal);
                object tr_bl = svr.GetInvoiceData(inputparams.ck, svr.Confirm(inputparams.ck));
                Invoice.invoice_header = fill_invoice_header(tr_bl);
                Invoice.invoice_body = fill_invoice_body(tr_bl);
                Invoice.invoice_extra = fill_invoice_extra(tr_bl);
                Invoice.invoice_steps = fill_invoice_steps(tr_bl);
                Invoice.invoice_transfer = fill_invoice_transfer(tr_bl);

            }
        }



      return Invoice; 
    }
}
doelleri
  • 19,232
  • 5
  • 61
  • 65
  • 1
    e_svr seems like the likely candidate from the statement where the error occurs, but that doesn't seem possible because if e_svr were null, you'd get an exception on the previous line. You'll need to debug it and see which object is null (and make sure it's actually throwing the exception on that line). – DSway Sep 11 '13 at 14:32
  • Thanks for the answer. The problem is solved. I was defining e_svr as a public class. Now i changed it to public struct and it is working fine. – user2386560 Sep 11 '13 at 14:48
  • 5
    @user2386560 That's merely a (horrible) band-aid solution. You should debug your code to determine what exactly is null. – Kendall Frey Sep 11 '13 at 14:54
  • possible duplicate of [What is a NullReferenceException in .NET and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net-and-how-do-i-fix-it) – Dour High Arch Sep 11 '13 at 17:09
  • e_svr is definitely null in your example. OR if e_svr.svr is a property then there is a null reference exception happening inside it. – Quibblesome Sep 11 '13 at 17:47

1 Answers1

1

I don't know if changing to struct is good idea. If you are know what you are doing, so be it. But remember if EPS_Instance is big structure pass over parameter in OpenConnections.Add can overflow the stock.

Problem is in else statement. If TryGetValue return false, the parameter out is set to null. So next if you try to do this

e_svr.ck = inputparams.ck;

the NullReferenceException will raise, because e_svr IS null.

In declaration of e_svr there is no point to create new instance. So your code should look like

EPS_Instance e_svr;
if (OpenConnections.TryGetValue(inputparams.VSId + inputparams.Username, out e_svr))
{
    //...
}
else
{
    e_svr = new EPS_Instance();
    //...
}
Carnifex
  • 533
  • 3
  • 10