Object Reference Not Set

I keep getting this error, and I can’t figure out how to fix it. Anyone able to assist me? :frowning:


This is my PhotonEngine.cs

using ExitGames.Client.Photon;
using UnityEngine;

public class PhotonEngine : MonoBehaviour, IPhotonPeerListener
{
    public PhotonPeer Peer { get; protected set; }
    public GameState State { get; protected set; }
    public ViewController Controller { get; set; }

    public string ServerAddress;
    public string ApplicationName;

    private static PhotonEngine _instance;

    public void Awake()
    {
        _instance = this;
    }

    public void Start()
    {
        DontDestroyOnLoad(this);
        State = new Disconnected(_instance);
        Application.runInBackground = true;
        Initialize();
    }

    public static PhotonEngine Instance
    {
        get { return _instance; }
    }

    public void Initialize()
    {
        Peer = new PhotonPeer(this, ConnectionProtocol.Udp);
        Peer.Connect(ServerAddress, ApplicationName);
        State = new WaitingForConnection(_instance);
    }

    public void Disconnect()
    {
        if (Peer != null)
        {
            Peer.Disconnect();
        }
        State = new Disconnected(_instance);
    }

    public void Update()
    {
        State.OnUpdate();
    }

    public void SendOp(OperationRequest request, bool sendReliable, byte channelId, bool encrypt)
    {
        State.SendOperation(request, sendReliable, channelId, encrypt);
    }

    public static void UseExistingOrCreateNewPhotonEngine(string serverAddress, string applicationName)
    {
        GameObject tempEngine;
        PhotonEngine myEngine;

        tempEngine = GameObject.Find("PhotonEngine");
        if (tempEngine == null)
        {
            tempEngine = new GameObject("PhotonEngine");
            tempEngine.AddComponent<PhotonEngine>();
        }

        myEngine = tempEngine.GetComponent<PhotonEngine>();

        myEngine.ApplicationName = applicationName;
        myEngine.ServerAddress = serverAddress;
    }

    #region Implementation of IphotonPeerListener

    public void DebugReturn(DebugLevel level, string message)
    {
        Controller.DebugReturn(level, message);
    }

    public void OnOperationResponse(OperationResponse operationResponse)
    {
        Controller.OnOperationResponse(operationResponse);
    }

    public void OnStatusChanged(StatusCode statusCode)
    {
        switch (statusCode)
        {
            case StatusCode.Connect:
                Peer.EstablishEncryption();
                break;
            case StatusCode.Disconnect:
            case StatusCode.DisconnectByServer:
            case StatusCode.DisconnectByServerLogic:
            case StatusCode.DisconnectByServerUserLimit:
            case StatusCode.Exception:
            case StatusCode.ExceptionOnConnect:
            case StatusCode.TimeoutDisconnect:
                Controller.OnDisconnected("" + statusCode);
                State = new Disconnected(_instance);
                break;
            case StatusCode.EncryptionEstablished:
                State = new Connected(_instance);
                break;
            default:
                Controller.OnUnexpectedStatusCode(statusCode);
                State = new Disconnected(_instance);
                break;
        }
    }

    public void OnEvent(EventData eventData)
    {
        Controller.OnEvent(eventData);
    }

    #endregion
}

This is my ViewController.cs

using System;
using System.Collections.Generic;
using UnityEngine;
using ExitGames.Client.Photon;

public class ViewController : IViewController
{
    private readonly View _controlledView;
    private readonly byte _subOperationCode;
    public View ControlledView { get { return _controlledView; } }
    private readonly Dictionary<byte, IPhotonOperationHandler> _operationHandlers = new Dictionary<byte, IPhotonOperationHandler>();
    private readonly Dictionary<byte, IPhotonEventHandler> _eventHandlers = new Dictionary<byte, IPhotonEventHandler>();

    public ViewController(View controlledView, byte subOperationCode = 0)
    {
        _controlledView = controlledView;
        _subOperationCode = subOperationCode;
        if (PhotonEngine.Instance == null)
        {
            Application.LoadLevel(0);
        }
        else
        {
            PhotonEngine.Instance.Controller = this;
        }
    }

    public Dictionary<byte, IPhotonOperationHandler> OperationHandlers
    {
        get { return _operationHandlers; }
    }

    public Dictionary<byte, IPhotonEventHandler> EventHandlers
    {
        get { return _eventHandlers; }
    }

    #region Implementation of Controller

    public bool IsConnected { get { return PhotonEngine.Instance.State is Connected; } }
    public void ApplicationQuit()
    {
        PhotonEngine.Instance.Disconnect();
    }

    public void Connect()
    {
        if (!IsConnected)
        {
            PhotonEngine.Instance.Initialize();
        }
    }

    public void SendOperation(OperationRequest request, bool sendReliable, byte channelId, bool encrypt)
    {
        PhotonEngine.Instance.SendOp(request, sendReliable, channelId, encrypt);
    }

    public void DebugReturn(DebugLevel level, string message)
    {
        _controlledView.LogDebug(string.Format("{0} - {1}", level, message));
    }

    public void OnOperationResponse(OperationResponse operationResponse)
    {
        IPhotonOperationHandler handler;
        if (operationResponse.Parameters.ContainsKey(_subOperationCode) &&
            OperationHandlers.TryGetValue(
                Convert.ToByte(operationResponse.Parameters[_subOperationCode]),
                out handler))
        {
            handler.HandleResponse(operationResponse);
        }
        else
        {
            OnUnexpectedOperationResponse(operationResponse);
        }
    }

    public void OnEvent(EventData eventData)
    {
        IPhotonEventHandler handler;
        if (EventHandlers.TryGetValue(eventData.Code, out handler))
        {
            handler.HandleEvent(eventData);
        }
    }

    public void OnUnexpectedEvent(EventData eventData)
    {
        _controlledView.LogError(string.Format("Unexpected event {0}.", eventData.Code));
    }

    public void OnUnexpectedOperationResponse(OperationResponse operationResponse)
    {
        _controlledView.LogError(string.Format("Unexpected Operation error {0} from operation {1}",
            operationResponse.ReturnCode, operationResponse.OperationCode));
    }

    public void OnUnexpectedStatusCode(StatusCode statusCode)
    {
        _controlledView.LogError(string.Format("Unexpected Status {0}", statusCode));
    }

    public void OnDisconnected(string message)
    {
        _controlledView.Disconnected(message);
    }

    #endregion
}

And finally the interface…

using ExitGames.Client.Photon;

public interface IViewController
{
    bool IsConnected { get; }
    void ApplicationQuit();
    void Connect();

    void SendOperation(OperationRequest request, bool sendReliable, byte channelId, bool encrypt);

    #region Implementation of IPhotonPeerListener

    void DebugReturn(DebugLevel level, string message);
    void OnOperationResponse(OperationResponse operationResponse);
    void OnEvent(EventData eventData);
    void OnUnexpectedEvent(EventData eventData);
    void OnUnexpectedOperationResponse(OperationResponse operationResponse);
    void OnUnexpectedStatusCode(StatusCode statusCode);
    void OnDisconnected(string message);



    #endregion
}

Any help that I can get would be SO appreciated. Much love <3 Savai

‘controller’ is probably null or let’s say it kind of has to be the one that is null.

Not 100% sure what you are meaning by this. It doesn’t really offer any solution to my problem.

In line 81 of PhotonEngine.cs you trying to use the Controller property but it looks like it has not been set; try this:

public void DebugReturn(DebugLevel level, string message)
    {
        if(Controller != null)
        {
            Controller.DebugReturn(level, message);
        }
        else
        {
            Debug.Log("Controller has not been set")
        }
       
    }

JEEZ! How did I miss that! It offered a solution to my problem, thanks a million! :smile:

Sorry for that, i assumed you know what it means when something is ‘null’ and causes an exception. So i just pointed out which object has to be assigned before using it.