Nullreferenceexception: Object Reference Not Set To An Instance Of An Object C#

Hello I am new to coding in unity and i was trying to make the health bar go down when the player hits an object. When i use a debug.log it prints what i want it to print when it collides with the object however when i try to make the health go down when it hits the object it gives me this error
“NullReferenceException: Object reference not set to an instance of an object
DamagePlayer.OnCollisionEnter2D (UnityEngine.Collision2D collision) (at Assets/Scripts/DamagePlayer.cs:30)”
here is my code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DamagePlayer : MonoBehaviour
{
    public BarScript bar;
    public int playerHealth = 100;
    int damage = 10;
   
    // Start is called before the first frame update
    void Start()
    {
        print(playerHealth);
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
      
       if(collision.collider.tag =="enemy")
        {

            Debug.Log("enemy");
            bar.math(damage);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BarScript : MonoBehaviour
{
    private float fillAmount;

    [SerializeField]
    private Image content;


    // Start is called before the first frame update
    void Start()
    {
        fillAmount = 1f;
       
    }

    // Update is called once per frame
    void Update()
    {
     
        content.fillAmount = fillAmount;
    }

    public float math(float value)
    {
        return fillAmount =(value / 100);
    }

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    private Rigidbody2D rb;

    [SerializeField]
    private float speed = 300f;
    private float jump = 400f;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        Movement();
    }

    void Movement()
    {
        if (Input.GetKeyDown("d")){
            rb.velocity = new Vector2(speed * Time.fixedDeltaTime, rb.velocity.y);
        }else if (Input.GetKeyDown("a"))
        {
            rb.velocity = new Vector2(-speed * Time.fixedDeltaTime, rb.velocity.y);
        }else if (Input.GetKeyDown("space"))
        {
            rb.velocity = new Vector2(rb.velocity.x, jump * Time.fixedDeltaTime);
        }
    }

}

“NullReferenceException” means you’re trying reference a non-existent object, and the error message points you to which line on which script is throwing the error:
DamagePlayer.cs:30
DamagePlayer is the script, and 30 is the line in the script:

bar.math(damage);

“bar” doesn’t exist in this context.

Since you’re caching a BarScript component as a public reference, you most likely just forgot to drag a BarScript component into your DamagePlayer component from the inspector window.