简体   繁体   English

如何解决System.NullReferenceException

[英]How to solve System.NullReferenceException

I am calling a web service in my php page. 我在php页面中调用了Web服务。 The web services are in C#. Web服务在C#中。 When I try to call a method using soap client object, it displays me error like: 当我尝试使用soap客户端对象调用方法时,它显示如下错误:

System.NullReferenceException: Object reference not set to an instance of an object.

The code I use to call Web service method is : 我用来调用Web服务方法的代码是:

 $Username = "username";
 $Password = "password";
 $LifetimeRequest = 60*60*24;


$soap_data = array(
   'Username' => $Username,
   'Password' => $Password,
   'LifetimeRequest' => $LifetimeRequest
);
$client = new SoapClient('http://50.56.173.161:8502/AdomniService.svc?wsdl');
$response = $client->ClientLogin($soap_data);
var_dump($response);

When I use var_dump it shows output like: 当我使用var_dump ,显示如下输出:

object(stdClass)#2 (1) {
  ["ClientLoginResult"]=>
  object(stdClass)#3 (3) {
    ["Error"]=>
    object(stdClass)#4 (5) {
      ["Private"]=>
      float(2)
      ["Public"]=>
      int(1)
      ["Details"]=>
      string(284) "System.NullReferenceException: Object reference not set to an instance of an object.
   at Adomni.AdomniService.ClientLogin(ClientLoginRequest request) in C:\Users\megiddo\Documents\Visual Studio 2010\Projects\Adomni\AdOmniAPIService\AdomniService\AdomniClientService.svc.cs:line 107"
      ["ErrorCode"]=>
      int(0)
      ["ErrorMessage"]=>
      NULL
    }
    ["Status"]=>
    int(-1)
    ["Token"]=>
    object(stdClass)#5 (8) {
      ["Private"]=>
      float(2)
      ["Public"]=>
      int(1)
      ["EventNotificationUri"]=>
      NULL
      ["IsManager"]=>
      bool(false)
      ["LifetimeRequest"]=>
      int(0)
      ["Password"]=>
      NULL
      ["TokenId"]=>
      int(0)
      ["UserName"]=>
      NULL
    }
  }
}

Can anyone tell me what am I doing wrong here? 谁能告诉我我在做什么错? Thanks in advance. 提前致谢。

The code which was used in C# is like: C#中使用的代码如下:

using System; 
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Security;
using System.IO;
using System.Data;
using AdOmniWebPortal.AdOmniService;

namespace AdOmniWebPortal
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void AdOmniLogin_Authenticate(object sender, AuthenticateEventArgs e)
        {
            AdomniServiceClient Client = new AdomniServiceClient();
            LoginRequest LoginRequest = new LoginRequest();
            LoginResponse LoginResponse = new LoginResponse();

            LoginRequest.Username = AdOmniLogin.UserName;
            LoginRequest.Password = AdOmniLogin.Password;
            LoginRequest.LifetimeRequest = 60*60*24;
            //This guy will be changed
            LoginRequest.EventNotificationURL = new Uri("http://herp-a-derp.com/awesome.html");

            LoginResponse = Client.Login(LoginRequest);

            if (LoginResponse.Status == 0)
            {
                System.Web.Security.FormsAuthentication.RedirectFromLoginPage(LoginResponse.Token.UserName, true);
                LifetimeToken token = LoginResponse.Token;
                Session["Token"] = token;

                GetUserRequest request = new GetUserRequest() { Token = token };
                GetUserResponse response = Client.GetUser(request);

                if (response.GetUser.Type == AdOmniService.UserType.Seller)
                {
                    Response.Redirect("~/Pages/Seller/SellerHomeDashboard.aspx");
                }

                if (response.GetUser.Type == AdOmniService.UserType.Client)
                {
                    Response.Redirect("~/Pages/Buyer/BuyerHomeDashboard.aspx");
                }

                if (response.GetUser.Type == AdOmniService.UserType.None)
                {
                    Response.Redirect("~/Pages/Buyer/BuyerHomeDashboard.aspx");
                }
            }
            else
            {
                Response.Redirect("~/Login.aspx");
                Response.Write(LoginResponse.Error.ErrorMessage);
            }
        }
    }
}

I have put the whole .cs page content in Edit. 我已将整个.cs页面内容放入“编辑”中。

Use Fiddler (a http debug proxy) 使用Fiddler(http调试代理)

that will allow you to peak inside of the request being made to the web service (in xml format) 这将使您可以深入了解对Web服务的请求(以xml格式)

so you can see if you are missing anything. 这样就可以查看您是否缺少任何内容。

channel your c# client through fiddler, and take a look 通过提琴手引导您的C#客户端,然后看看

http://www.fiddler2.com/fiddler2/ http://www.fiddler2.com/fiddler2/

Maybe in your PHP script, you should also set the EventNotificationURL variable. 也许在您的PHP脚本中,您还应该设置EventNotificationURL变量。

Take a look at this section in the error response: 看看错误响应中的这一部分:

  ["EventNotificationUri"]=>
  NULL

Maybe the service expects you to pass in a EventNotificationUri value, just like you pass in the the Password , Username and LifetimeRequest . 也许服务希望您传递一个EventNotificationUri值,就像您传递PasswordUsernameLifetimeRequest

[EDIT] [编辑]

Try to change your variable name from Username to UserName . 尝试将变量名从Username更改为UserName As far as I found out, PHP should be case sensitive in this matter, so "Username" != "UserName" 据我了解,PHP在此问题上应区分大小写,因此"Username" != "UserName"

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

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