1. Nathan 3

    Nathan 3 Wood Hoarder

    Hello,

    I am trying to figure out what is causing the NullReference Exception in this part of code -

    Code (Text):
    void OnEntityDeath(BaseCombatEntity entity, HitInfo hitInfo) {
                if(entity.lastAttacker != null && entity.lastAttacker is BasePlayer) {
                    Vector3 pos = entity.lastAttacker.GetEstimatedWorldPosition();
                    if(entity is BuildingBlock) {
                        executeQuery("INSERT INTO player_destroy_building (player, building, pos_x, pos_y, pos_z, date) VALUES (@0,@1,@2,@3,@4,@5)", ((BasePlayer)entity.lastAttacker).userID, ((BuildingBlock)entity).blockDefinition.info.name.english, pos.x, pos.y, pos.z, getDateTime());
                    } else if(entity is BaseNPC) {
                        executeQuery("INSERT INTO player_animal_kill (player, animal, pos_x, pos_y, pos_z, date) VALUES (@0,@1,@2,@3,@4,@5)", ((BasePlayer)entity.lastAttacker).userID, UppercaseFirst(entity.ShortPrefabName), pos.x, pos.y, pos.z, getDateTime());
                    } else if(entity is BasePlayer && entity != entity.lastAttacker) {
                        executeQuery("INSERT INTO player_kill (killer, victim, pos_x, pos_y, pos_z, weapon, date) VALUES (@0,@1,@2,@3,@4,@5,@6)", ((BasePlayer)entity.lastAttacker).userID, ((BasePlayer)entity).userID, pos.x, pos.y, pos.z, ((BasePlayer)entity.lastAttacker).GetActiveItem().info.displayName.english, getDateTime());
                    }
                }

                if(entity is BasePlayer) {
                    Vector3 pos = entity.GetEstimatedWorldPosition();
                    string cause = entity.lastDamage.ToString();
                    executeQuery("INSERT INTO player_death (player, cause, pos_x, pos_y, pos_z, date) VALUES (@0,@1,@2,@3,@4,@5)", ((BasePlayer)entity).userID, cause, pos.x, pos.y, pos.z, getDateTime());
                }
            }
    I see Null checks at the start but it still occasionally calls NullReferences, It's probably 1 in 50 deaths.
     
  2. Wulf

    Wulf Community Admin Community Admin Oxide Developer

    Most likely the info you are checking for is null in that 1 case, so I'd check info for null.