Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Hybrid View

  1. #1
    Join Date
    Aug 2011
    Location
    WA State
    Posts
    40

    NullReferenceException

    This is just bugging the crap out of me so I figured after spending a couple of hours trying to learn through my error and not getting anywhere, I'd kick it up to the brain trust here on the forums.

    In my GameManager.cs class I'm getting a NullReferenceException in my Login method (shown below). The null reference is on response.Response.Username which I'm trying to assign to _displayUsername.

    I can give that variable a value and comment out the assignment without any errors, so I'm guessing that the value of .Username isn't gettting passed back from my database somewhere.

    Obviously I missed something somewhere, so any hints on where to look next are appreciated. Both CommandContext and CommandContext` look correct.

    Code:
       private void Login(string email, string password)
        {
            _state = GameManagerState.Sending;
           NetworkManager.Instance.Dispatch(new LoginCommand(email, password), response =>
               {
                    if (response.IsValid)
                    {
                        _state = GameManagerState.LoggedIn;
                        _displayUsername = response.Response.Username;
                    }
                    else
                    {
                        _state = GameManagerState.Error;
                        _error = response.ToErrorString();
                    }
               });
        }
    I'm sort of hoping that I just missed something during the troubleshooting edit near the end of the video because not being able to troubleshoot a NullReferance is demoralizing.

    -Seth

  2. #2
    Join Date
    Aug 2011
    Location
    WA State
    Posts
    40
    I should add that functionally, everything works. The null reference only affects the little "Success! : <name>" GUILayout.Label that shows up when login is successful.

    But I suspect that getting that user name back out of the database is going to be more of an issue in a class or twelve.

    -Seth

  3. #3
    Join Date
    Oct 2011
    Location
    Norway
    Posts
    21
    How does your LoginHandler look like?

  4. #4
    Join Date
    Aug 2011
    Location
    WA State
    Posts
    40
    Here's my LoginCommand.cs
    Code:
    using RuneSlinger.Base.Abstract;
    
    namespace RuneSlinger.Base.Commands
    {
        public class LoginCommand : ICommand<LoginResponse>
        {
            public string Email { get; private set ; }
            public string Password { get; private set ; }
    
            public LoginCommand(string email, string password)
            {
                Email = email;
                Password = password;
            }
        }
    
        public class LoginResponse : ICommandResponse
        {
            public uint ID { get; private set; }
            public string email { get; private set; }
            public string Username { get; private set; }
    
            public LoginResponse(uint id, string email, string username)
            {
                ID = id;
                this.email = email;
                Username = username;
            }
        }
    }
    And my LoginHandler.cs

    Code:
    using System.Linq;
    using NHibernate;
    using NHibernate.Linq;
    using RuneSlinger.Base.Commands;
    using RuneSlinger.Server.Abstract;
    using RuneSlinger.Server.Entities;
    
    namespace RuneSlinger.Server.CommandHandlers
    {
        public class LoginHandler : ICommandHandler<LoginCommand>
        {
            private readonly ISession _database;
    
            public LoginHandler(ISession database)
            {
                _database = database;
            }
    
            public void Handle(CommandContext context, LoginCommand command)
            {
                if (string.IsNullOrWhiteSpace(command.Email))
                {
                    context.RaisePropertyError("Email", "Required");
                    return;
                }
                if (string.IsNullOrWhiteSpace(command.Password))
                {
                    context.RaisePropertyError("Password", "Required");
                    return;
                }
                var user = _database.Query<User>().SingleOrDefault(s => s.Email == command.Email);
                if (user == null || !user.Password.EqualsPlaintext(command.Password))
                {
                    context.RaiseOperationError("Invalid Email or Password");
                    return;
                }
    
                context.SetResponse(new LoginResponse(user.Id, user.Email, user.Username));
            }
        }
    }
    Thanks for taking a look at this for me.

    -Seth

  5. #5
    Join Date
    Oct 2011
    Location
    Norway
    Posts
    21
    That looks fine to me. I'd probably check the database to make sure the data was actually stored properly. If it was, I'd debug LoginHandler.cs and check that the data was extracted properly from the db. Then just follow the trail back to the client to see where it went wrong.

    Just yell if you need help along the way

  6. #6
    Join Date
    Aug 2011
    Location
    WA State
    Posts
    40
    The username field (tuple?) is lowercase, but I thought we set that up as case in-sensitive a some point.

    I'll see if I can debug out what is being passed through the handler class.

    Thank you for your help.

    -troy

  7. #7
    Join Date
    Oct 2011
    Location
    Norway
    Posts
    21
    SQL is case-insensitive, thus nhibernate will map the class property "Username" to the mysql field "username" without issue. And a tuple is an ordered list of elements

  8. #8
    Join Date
    May 2009
    Posts
    199
    Hi Sethanon,

    I just had this exact same problem myself.
    Debugging my program led me to realise the Response property of the CommandContext class (in RuneSlinger.Server) was null because I had not filled in the SetResponse method within that class.

    The SetResponse method should read...

    Code:
    public void SetResponse(ICommandResponse response)
    {
          Response = response;
    }
    Maybe you also missed it?

    After adding it in unfortunately I got another error which as yet I have not been able to fix, despite checking the video several times and comparing my code to Nelson's.

    The error I get is...

    JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'RuneSlinger.Base.Commands.LoginResponse' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
    To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
    Path ''.

    I don't wish to hijack your thread but if you happen to find the same problem maybe we can seek out a solution together

  9. #9
    Join Date
    May 2009
    Posts
    199
    Update: The error I described above is no more, and I have no idea why!?@#!?

    I re-built and closed both solutions then logged in using a different user than the one I had been using for testing so far. It worked correctly with no more errors. Then I again tried with my normal test user and it worked.

    No idea why

  10. #10
    Join Date
    Aug 2011
    Location
    WA State
    Posts
    40
    No worries about hijacking a thread. Any troubleshooting tips are always welcome.

    I did catch that value being assigned to Response in CommandContext.cs. And I am getting logged in.

    I added in a debug line to GameManager.cs

    Code:
            else if (_state == GameManagerState.LoggedIn)
            {
                Debug.Log(_displayUsername);
                GUILayout.Label("Success! " + _displayUsername);
            }
    When I create a new user, the correct user name spams into the Unity console and displays correctly.

    When I restart the project and try logging into the user I created, I get the error below in the Unity console and the debug log spams the value of _displayUsername as NULL.

    I did try debugging out the value of response.Response.Username when it gets assigned to _displayUsername in the Login method, but Unity get's confused between CommandContext.cs and the generic version and throws up.


    NullReferenceException: Object reference not set to an instance of an object
    GameManager.<Login>m__2 (Assets.Code.CommandContext`1 response) (at Assets/Code/GameManager.cs:105)
    NetworkManager+<Dispatch>c__AnonStorey2`1[RuneSlinger.Base.Commands.LoginResponse].<>m__5 (System.Collections.Generic.Dictionary`2 parameters) (at Assets/Code/NetworkManager.cs:66)
    NetworkManager.OnOperationResponse (ExitGames.Client.Photon.OperationResponse operationResponse) (at Assets/Code/NetworkManager.cs:87)
    ExitGames.Client.Photon.PeerBase.DeserializeMessag eAndCallback (System.Byte[] inBuff)
    ExitGames.Client.Photon.EnetPeer.DispatchIncomingC ommands ()
    ExitGames.Client.Photon.PhotonPeer.DispatchIncomin gCommands ()
    ExitGames.Client.Photon.PhotonPeer.Service ()
    NetworkManager.Update () (at Assets/Code/NetworkManager.cs:37)

    That being said, I'm having a pretty good time working through the troubleshooting here and greatly appreciate the tips. Of course, if my client was just dying, I'm sure the fun factor would be lower.

    -Seth

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •