Object reference not set to a instance.

crzyone9584
edited December 2011 in Photon Server
2011-12-21 09:09:24,288 [14] ERROR PhotonHostRuntime.PhotonDomainManager - System.NullReferenceException: Object reference not set to an instance of an object.
at Server.DataHandler.EditorLogin(OperationRequest request, SendParameters parameters) in C:\Users\hello\Desktop\theworld\The World\Server\DataHandler.cs:line 92
at Server.TheWorldPeer.OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters) in C:\Users\goodbye\Desktop\theworld\The World\Server\TheWorldPeer.cs:line 42
at Photon.SocketServer.PeerBase.OnReceiveInternal(Byte[] data, SendParameters sendParameters) in c:\build\photon-socketserver-sdk_3.0\src\Photon.SocketServer\PeerBase.cs:line 641
at ExitGames.Concurrency.Core.DefaultExecutor.Execute(List`1 toExecute) in c:\Dev\exitgames-libs\src\Core\Concurrency\Core\DefaultExecutor.cs:line 21
at ExitGames.Concurrency.Fibers.PoolFiber.Flush(Object ) in c:\Dev\exitgames-libs\src\Core\Concurrency\Fibers\PoolFiber.cs:line 216
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

Here is my entire TheWorldPeer.cs along with DataHandler.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;
using ExitGames.Logging;
using TheWorld.Common;

namespace Server
{
    public class TheWorldPeer : PeerBase
    {
        private readonly ILogger Log = LogManager.GetCurrentClassLogger();
        private DataHandler _dataHandler = new DataHandler();
        
        public TheWorldPeer(IRpcProtocol protocal, IPhotonPeer peer)
            : base(protocal, peer)
        {
            Log.Debug("Connection Received from: " + peer.GetRemoteIP());
        }

        protected override void OnDisconnect()
        {
            // Handles the clients disconnect from the server
            Log.Info("Client disconnected");
        }

        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
            switch (operationRequest.OperationCode)
            {
                case (byte)OperationCode.Register:
                    //Handles Registration
                    _dataHandler.Registration(operationRequest, sendParameters);
                    break;
                case (byte)OperationCode.Login:
                    // Handles the login information here.
                    _dataHandler.Login(operationRequest, sendParameters);
                    break;
                case (byte)OperationCode.EditorLogin:
                    _dataHandler.EditorLogin(operationRequest, sendParameters);
                    break;
                case (byte)OperationCode.Logout:
                    // Handles the logout of the game not the Disconnect
                    break;
                case (byte)OperationCode.WriteToConsole:
                    break;
            }
        }
    }
}

DataHandler.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Photon.SocketServer;
using TheWorld.Common;
using Server.Game;
using ExitGames.Logging;

namespace Server
{
    public class DataHandler
    {
        TheWorldPeer _peer;

        private readonly ILogger Log = LogManager.GetCurrentClassLogger();

        // Handles the registration
        public void Registration(OperationRequest operationRequest, SendParameters sendParameters)
        {
            var username = (string)operationRequest.Parameters[(byte)DataCode.Username];
            var password = (string)operationRequest.Parameters[(byte)DataCode.Password];
            var email = (string)operationRequest.Parameters[(byte)DataCode.Email];

            Log.Debug("Adding new user, " + username);

            /* Take the username and create folder and a file named after the user name.
             * Add Password and username and user level inside file. Add three character
             * slots to the file and leave them blank until the character is created.
            */

            // Specify a "currently active folder"
            string activeDir = @"C:\Users\well hello there\Desktop\theworld\deploy\TheWorld.Server\Data\Users\";

            // Create new path
            string newPath = System.IO.Path.Combine(activeDir, username);

            // create the sub folder
            System.IO.Directory.CreateDirectory(newPath);

            // Create the new user file.
            // Use XML Serailaization
            Lists.user.Add(new Users(Lists.user.Count + 1, username, password, email, 1));

            XML.SerializeUser<Users>(Lists.user[Lists.user.Count - 1], newPath);


        }

        public void Login(OperationRequest request, SendParameters parameters)
        {
            var username = (string)request.Parameters[(byte)DataCode.Username];
            var password = (string)request.Parameters[(byte)DataCode.Password];
            Log.Debug(username + " , " + password);
            foreach (Users u in Lists.user)
            {
                if (u.Name == username && u.Password == password)
                {
                    // Login successful

                    // TODO: Send reply to client letting them know they logged in
                    Log.Debug("Login okay");
                }
                else
                {
                    // Login in wrong. Lets find out why

                    // TODO: Figure out why login didnt work
                }
            }
        }

        #region Editors

        // Login
        public void EditorLogin(OperationRequest request, SendParameters parameters)
        {
            var username = (string)request.Parameters[(byte)DataCode.Username];
            var password = (string)request.Parameters[(byte)DataCode.Password];
            
            foreach (Users u in Lists.user)
            {                
                if (u.Name == username && u.Password == password && u.Rank >= 2)
                {

                    //Check the rank. If they are 2 or higher then allow them to enter editor
                    if (u.Rank >= 2)
                    {
                        Log.Debug(parameters.ChannelId.ToString());
                        Log.Debug(username + " has logged into the editors. They have a rank of " + u.Rank);
                        var response = new OperationResponse((byte)OperationCode.Login);
                        response.Parameters.Add((byte)DataCode.SuccesfulLogin, "true");
                        response.Parameters.Add((byte)DataCode.Rank, u.Rank);
                        _peer.SendOperationResponse(response, parameters);
                    }

                }
                else
                {
                    // Login in wrong. Lets find out why
                    Log.Info(username + " attempted to log into the editors. They have a rank of " + u.Rank);
                    // TODO: Figure out why login didnt work
                }
            }
        }
    }
}

Why am i getting this error? This is the only thing that is holding me back of having a workable login system

Comments

  • The exception is thrown on line 92, which is this one:
    response.Parameters.Add((byte)DataCode.SuccesfulLogin, "true");
    

    The "Parameters" property of the OperationResponse is not created by the default constructor, use the constructor that takes a "parameters" object, like this, and you should be fine:
    var response = new OperationResponse((byte)OperationCode.Login, new Dictionary<byte, object>());
    response.Parameters.Add((byte)DataCode.SuccesfulLogin, "true");
    
  • Thanks now I'm getting the following. How is line 94 wrong now?
    2011-12-22 10:13:31,518 [19] ERROR PhotonHostRuntime.PhotonDomainManager - System.NullReferenceException: Object reference not set to an instance of an object.
    at Server.DataHandler.EditorLogin(OperationRequest request, SendParameters parameters) in C:\Users\crzyone9584\Desktop\theworld\The World\Server\DataHandler.cs:line 94
    at Server.TheWorldPeer.OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters) in C:\Users\hello\Desktop\theworld\The World\Server\TheWorldPeer.cs:line 44
    at Photon.SocketServer.PeerBase.OnReceiveInternal(Byte[] data, SendParameters sendParameters) in c:\build\photon-socketserver-sdk_3.0\src\Photon.SocketServer\PeerBase.cs:line 641
    at ExitGames.Concurrency.Core.DefaultExecutor.Execute(List`1 toExecute) in c:\Dev\exitgames-libs\src\Core\Concurrency\Core\DefaultExecutor.cs:line 21
    at ExitGames.Concurrency.Fibers.PoolFiber.Flush(Object ) in c:\Dev\exitgames-libs\src\Core\Concurrency\Fibers\PoolFiber.cs:line 216
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
    at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
    at System.Threading.ThreadPoolWorkQueue.Dispatch()
    at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

    I think I have it right which is
    _peer.SendOperationResponse(response, parameters);
    

    Sending things to the server was easy. I figured it be the same way but with SendOperationResponse() instead of SendOpCustom()
  • Anyone know what I'm doing wrong? I've been reading and looking at the tutorials and source code for almost a week now. I'm really lost on this error.
  • You've only declared the "_peer" variable (line 14: TheWorldPeer _peer;), but never initialized it.

    Debugging NullReferenceExceptions is a quite basic programming skill for which we can not really provide support in the forums; they are not "Photon-specific", but a general C# feature. So i would suggest that you read a bit about exception handling and C# code debugging in general, it might help you a lot in the future. :-)

    If you need some hints how you can debug your Photon server code while it is running, this topic might help you: viewtopic.php?f=5&t=423
  • Wow i totally missed that. Thanks for the help. It works now. Sorry for all the trouble.