Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, happy new year to all :) and now to problem can somebody tell me first what is meaning of nullreference but of easy way.
Second why is nullreference meaking me problem when am in game and my screen starts bliking black, my esp losse color etc..

CODE FROM ESP:

C#
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.IO;
using UnityEngine;
using EFT;
using EFT.Interactive;

namespace Absolutly
{
    public class Abso : MonoBehaviour
    {

        public Abso() { }
        #region variable
        private GameObject GameObjectHolder;

        private IEnumerable<Player> _playerInfo;
        private IEnumerable<ExfiltrationPoint> _extractPoints;
        private IEnumerable<LootItem> _item;

        private float _playNextUpdateTime;
        private float _evaNextUpdateTime;
        protected float _infoUpdateInterval = 15f;


        private bool _isInfoMenuActive = true;
        private bool _showItems;
        private bool _pInfor;
        private bool _showExtractInfo;

        private float _viewdistance = 300f;
        private float _lootItemDistance = 50f;
        private float _espUpdateInterval = 50f;
        private float _itemsNextUpdateTime;
        private Player _localPlayer;
        private Vector3 camPos;
        #endregion

        #region import

        [DllImport("user32.dll")]
        static extern bool GetCursorPos(out POINT lpPoint);

        [DllImport("user32.dll")]
        static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

        private const int MOUSEEVENTF_MOVE = 0x0001;
        public static void Move(int xDelta, int yDelta)
        {
            mouse_event(MOUSEEVENTF_MOVE, xDelta, yDelta, 0, 0);
        }
        public struct POINT
        {
            public int X;
            public int Y;
        }
        #endregion

        private void Awake()
        {
            Debug.logger.logEnabled = false;
        }

        private void Start()
        {
            Clear();
        }
        private void Clear()
        {
            _playerInfo = null;
            _extractPoints = null;
            _item = null;
            _itemsNextUpdateTime = 0;
            _localPlayer = null;
        }

        public void Load()
        {
            GameObjectHolder = new GameObject();
            GameObjectHolder.AddComponent<Abso>();
            DontDestroyOnLoad(GameObjectHolder);
        }


        public void Unload()
        {
            Clear();
            Destroy(GameObjectHolder);
            Destroy(GameObjectHolder.GetComponent<Abso>());
            Destroy(this);
            DestroyObject(this);
        }

        private void OnDisable()
        {
            Clear();
        }
        private void OnDestroy()
        {
            Clear();
        }


        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.F1))
            {
                Unload();
            }
            if (Input.GetKeyDown(KeyCode.F11))
            {
                _isInfoMenuActive = !_isInfoMenuActive;
            }

        }

        private void OnGUI()
        {
            if (_isInfoMenuActive)
            {
                GUIOverlay();
            }
            if ((_pInfor && Time.time >= _playNextUpdateTime) || ( Time.time >= _playNextUpdateTime))
            {
                _playerInfo = FindObjectsOfType<Player>();
                _playNextUpdateTime = Time.time + _infoUpdateInterval;
            }
            if (_pInfor)
            {
                DrawPlayers();
            }
            if (_showExtractInfo && Time.time >= _evaNextUpdateTime)
            {
                _extractPoints = FindObjectsOfType<ExfiltrationPoint>();
                _evaNextUpdateTime = Time.time + _infoUpdateInterval;
            }

            if (_showExtractInfo)
            {
                DrawExtractInfo();
            }
            if (_showItems)
            {
                if (Time.time >= _itemsNextUpdateTime)
                {
                    _item = FindObjectsOfType<LootItem>();
                    _itemsNextUpdateTime = Time.time + _espUpdateInterval;
                }
                ShowItemESP();
            }
        }
        private void GetLocalPlayer()
        {
            foreach (Player player in FindObjectsOfType<Player>())
            {
                if (player == null) continue;

                if (EPointOfView.FirstPerson == player.PointOfView && player != null)
                {
                    _localPlayer = player;
                }
            }
        }


        private void DrawExtractInfo()
        {
            try
            {
                foreach (var point in _extractPoints)
                {
                    float distanceToObject = Vector3.Distance(Camera.main.transform.position, point.transform.position);
                    var exfilContainerBoundingVector = new Vector3(
                        Camera.main.WorldToScreenPoint(point.transform.position).x,
                        Camera.main.WorldToScreenPoint(point.transform.position).y,
                        Camera.main.WorldToScreenPoint(point.transform.position).z);

                    if (exfilContainerBoundingVector.z > 0.01)
                    {
                        Color guiBackup = GUI.color;
                        GUI.color = Color.green;
                        int distance = (int)distanceToObject;
                        String exfilName = point.name;
                        string boxText = $"{exfilName} - {distance} M";

                        GUI.Label(new Rect(exfilContainerBoundingVector.x - 50f, (float)Screen.height - exfilContainerBoundingVector.y, 100f, 50f), boxText);
                        GUI.color = guiBackup;
                    }
                }
            }
            catch (NullReferenceException ex)
            {
                File.AppendAllText
                (
                   @"C:\EFT\EXITS.txt",
                   $"{ex.Message}\r\n{ex.StackTrace}\r\n"
                );
            }
        }

        private void ShowItemESP()
        {
            foreach (var Item in _item)
            {
                if (Item == null)
                    continue;

                float distance = Vector3.Distance(Camera.main.transform.position, Item.transform.position);
                Vector3 ItemBoundingVector = new Vector3(Camera.main.WorldToScreenPoint(Item.transform.position).x, Camera.main.WorldToScreenPoint(Item.transform.position).y, Camera.main.WorldToScreenPoint(Item.transform.position).z);
                if (ItemBoundingVector.z > 0.01 && Item != null && (Item.name.Contains("key") || Item.name.Contains("tetriz") || Item.name.Contains("prokill") || Item.name.Contains("ssd") || Item.name.Contains("virtexprocessor") || Item.name.Contains("video_card") || Item.name.Contains("transilluminator")) && distance <= _lootItemDistance)
                {
                    name = Item.name;

                    string text = string.Format($"{Item.name} - [{distance}]M");
                    Color guiBackup = GUI.color;
                    GUI.color = Color.cyan;
                    GUI.Label(new Rect(ItemBoundingVector.x - 50f, (float)Screen.height - ItemBoundingVector.y, 100f, 50f), text);
                    GUI.color = guiBackup;
                }
            }
        }


        private void DrawPlayers()
        {
            foreach (var player in _playerInfo)
                {

                try
                {
                    if (player == null)
                    {
                        break;
                    }

                    if (player.Profile.Info.Nickname == string.Empty)
                    {
                        continue;
                    }

                    if (!player.IsVisible) continue;

                    Vector3 playerPos = player.Transform.position;
                    float distanceToObject = Vector3.Distance(camPos, player.Transform.position);
                    Vector3 playerBoundingVector = Camera.main.WorldToScreenPoint(playerPos);
                    if (distanceToObject <= _viewdistance && playerBoundingVector.z > 0.01)
                    {
                        Vector3 playerHeadVector = Camera.main.WorldToScreenPoint(player.PlayerBones.Head.position);
                        Gizmos.DrawCube(playerPos, new Vector3(1, 1, 2));
                        float boxVectorX = playerBoundingVector.x;
                        float boxVectorY = playerHeadVector.y + 10f;
                        float boxHeight = Math.Abs(playerHeadVector.y - playerBoundingVector.y) + 10f;
                        float boxWidth = boxHeight * 0.65f;
                        var IsAI = player.Profile.Info.RegistrationDate <= 0;
                        var playerColor = player.HealthController.IsAlive ? GetPlayerColor(player.Side) : Color.gray;
                        Utility.DrawBox(boxVectorX - boxWidth / 2f, Screen.height - boxVectorY, boxWidth, boxHeight, playerColor);
                        Utility.DrawLine(new Vector2(playerHeadVector.x - 2f, Screen.height - playerHeadVector.y), new Vector2(playerHeadVector.x + 2f, Screen.height - playerHeadVector.y), playerColor);
                        Utility.DrawLine(new Vector2(playerHeadVector.x, Screen.height - playerHeadVector.y - 2f), new Vector2(playerHeadVector.x, Screen.height - playerHeadVector.y + 2f), playerColor);
                        var playerName = IsAI ? "[BOT]" : player.Profile.Info.Nickname;
                        string playerText = player.HealthController.IsAlive ? playerName : (playerName + " [MRTAV]");
                        string playerTextDraw = string.Format("{0} [{1}]", playerText, (int)distanceToObject);
                        var playerTextVector = GUI.skin.GetStyle(playerText).CalcSize(new GUIContent(playerText));
                        GUI.Label(new Rect(playerBoundingVector.x - playerTextVector.x / 2f, Screen.height - boxVectorY - 20f, 300f, 50f), playerTextDraw);
                    }
                }
                catch (NullReferenceException ex)
                {
                    File.AppendAllText
                    (
                       @"C:\EFT\DrawPlay.txt",
                       $"{ex.Message}\r\n{ex.StackTrace}\r\n"
                    );
                }
            }

        }


        private Color GetPlayerColor(EPlayerSide side)
        {
            switch (side)
            {
                case EPlayerSide.Bear:
                    return Color.red;
                case EPlayerSide.Usec:
                    return Color.blue;
                case EPlayerSide.Savage:
                    return Color.yellow;
                default:
                    return Color.yellow;
                    /*
                    case EPlayerSide.Bear:
                        return ColorUtility.TryParseHtmlString(_bearColor);
                    case EPlayerSide.Usec:
                        return ColorUtility.TryParseHtmlString(_usecColor);
                    case EPlayerSide.Savage:
                        return ColorUtility.TryParseHtmlString(_scavColor);
                    default:
                        return Color.white;
                        */
            }
        }

        private void GUIOverlay()
        {
            GUI.color = Color.gray;
            GUI.Box(new Rect(100f, 100f, 400f, 500f), "");
            GUI.color = Color.white;
            GUI.Label(new Rect(180f, 110f, 150f, 20f), "CITESKI MENI");
            _pInfor = GUI.Toggle(new Rect(110f, 140f, 120f, 20f), _pInfor, "IGRACI ESP"); // Display player
            _showExtractInfo = GUI.Toggle(new Rect(110f, 160f, 120f, 20f), _showExtractInfo, "EXSTRAKTI"); //Display  extraction

            _showItems = GUI.Toggle(new Rect(110f, 280f, 120f, 20f), _showItems, "LOOT STVARI"); //Show items on map
            if (_showItems)
            {
                GUI.Label(new Rect(110f, 320f, 150f, 20f), "LOOT DISTANCA");
                _lootItemDistance = GUI.HorizontalSlider(new Rect(210f, 320f, 120f, 20f), _lootItemDistance, 0.0F, 1500.0F);
            }
        }

        private double GetDistance(double x1, double y1, double x2, double y2)
        {
            return Math.Sqrt(Math.Pow(x2 - x1, 2.0) + Math.Pow(y2 - y1, 2.0));
        }

        private void Circle(int X, int Y, int radius)
        {
            float boxXOffset = X;
            float boxYOffset = Y;
            float boxHeight = radius;
            float boxwidth = radius;
            Vlcrpc.DrawBox(boxXOffset - (radius / 2), boxYOffset - (radius / 2), radius, radius, Color.yellow);
            Vlcrpc.DrawLine(new Vector2(960, 1080), new Vector2(960, 0), Color.white);
            Vlcrpc.DrawLine(new Vector2(0, 540), new Vector2(1920, 540), Color.white);
        }

    }

}


What I have tried:

Replace all code
Update GUI
GUI backup color added
null reference added
nothing worked at all
Posted
Updated 5-Jan-20 22:44pm
v2

Any object can be null. Null means it doesn't exist.

AnyClass x = null;

now x.anyProperty will throw an error as x is NOT 'AnyClass' yet, it is null, a placeholder to contain an AnyClass when you make one
 
Share this answer
 
Comments
MRHals Gospodin 5-Jan-20 20:32pm    
But how can i implement that now on my way to now make it normaly working?
MRHals Gospodin 5-Jan-20 20:33pm    
How can am cache this all abov? will that fix my problem?
Christian Graus 5-Jan-20 20:55pm    
Your code is not helpful. Player is obviously not null. I have no clue what is, as you don't say what line has the error
MRHals Gospodin 5-Jan-20 21:08pm    
I have just one error whats reapeate over and over:

System.NullReferenceException: Object reference not set to an instance of an object
at Absolutly.Abso.DrawPlayers () [0x00029] in <9854d861ae0d247c498e2782f3e1d916>:0
Christian Graus 5-Jan-20 22:50pm    
See, a line number would tell me (or you if you look) what is null. Do you know how to use a debugger? Put a try catch around all this and when it catches, step through it again and see what is null
Using the debugger would be the fastest way for you to spot the issue. We cannot debug for you. Do you need help on how to conduct a debug session?

You could also change the way you are logging exceptions; try to change the catch block to
C#
catch (NullReferenceException ex)
{
   File.AppendAllText
   (
      @"C:\exeptionseft\DrawPlay.txt",
      $"{ex.Message}\r\n{ex.StackTrace}\r\n"
   );
}
and you will start getting better informations about what is going on and where it is happening.
 
Share this answer
 
Comments
MRHals Gospodin 6-Jan-20 4:25am    
I will try thank you
MRHals Gospodin 6-Jan-20 4:32am    
Am updated official post abov, for more info if you myb can spot my error will be awsom. this full working code.
phil.o 6-Jan-20 5:15am    
Sorry, but my answer is the same: debug your code to find where and why you get the exception. I cannot do that for you. Please also see answer nb. 3 by Richard MacCutchan.
MRHals Gospodin 6-Jan-20 6:27am    
debug didnt show me any errors.
phil.o 6-Jan-20 6:30am    
But you still get an exception, right? Debugging does not show you any error, it just allows you to spot eventual issues with your code.
Again, do you need help on how to conduct a debug session?
This is the same issue as https://www.codeproject.com/Questions/5255717/Csharp-ESP-drawing-error[^], and you already received a suggestion. The problem is that by capturing the exception you are losing information that the system would produce automatically. Remove the try catch and you will get details of where the exception occurs. From that you should be able to fix it without difficulty.
 
Share this answer
 
Comments
MRHals Gospodin 6-Jan-20 6:25am    
I try that option all ready but not help me alot with Topic and this with removing Catch noup its fine the code but the game make me problems again and am loosing hoppe to fix this and am wanna fix this becauss i invest many hours to create this. ESP

If you can tell me more details what can be wrong will help me alot
Richard MacCutchan 6-Jan-20 6:45am    
As has been explained already, a null reference simply means that you are trying to use a reference that has not been initialised. Unfortunately, the only way to find them is by using the debugger to trace through the code and see exactly where the problem occurs. It may be that you forgot to initialise the reference, or you expect a return from some called method to return it. In almost every case it is simply because you forgot, or missed, something when initially writing your code. And, to be honest, there is not much we can do to help unless we can see an obvious mistake in the code that you show us. But more often than not the mistakes are not obvious. But don't be disheartened, it is a problem that we have all faced from time to time, and sometimes still do. It is just another part of the learning process that all developers go through.
MRHals Gospodin 6-Jan-20 6:47am    
You have right i will keep looking in to code myb am blind so see someting and need to look better, thank you for all help.
Can you tell me how is Cache in c# works to cache drawedplayer and then to loop it again? simple code thank you again

PIC: https://i.imgur.com/JQH8Ibm.png
Richard MacCutchan 6-Jan-20 7:03am    
I am not sure what you mean by "Cache", but see cache image c - Google Search[^], for some suggestions.
MRHals Gospodin 6-Jan-20 7:27am    
I mean can i players what are drawed just Cache and loope it again?

Like player Box 1
save.
And if new players joins in to game
Take Box1 one and put on player 2
Without to need program to draw for each player boxes

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900