Correct way to use ScreenToWorldPoint?

I have a problem with ScreenToWorldPoint because it just spews out
“NullReferenceException: Object reference not set to an instance of an object.”"

1st way i used it:

    Vector3 objectPos = Camera.main.WorldToScreenPoint(transform.position);

2nd way i used it:

Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

Can you please help me with this problem?
Thanks!

2nd way is correct.

You get Nullref because Camera.main is null

Nullref is basically when you trying to use something from non-existing object, in your case Camera.main

Camera.main is automatically assigned by unity. Just make sure there is a camera gameObject in your scene with a tag “MainCamera”