In Mac:
In your terminal run
ECHO $JAVA_HOME
This will bring up the current path of java home.Pay close attention to the path that the terminal outputs.In my case i had System/Library instead of /Library/.../... typo as my path.So i simply corrected this in bash_profile.
If your path is correct but the error persists,
Right Click On Finder
Go To Folder

Bash_Profile

Add or Edit the Java Home Path
The path you add here should be the one your terminal output when you ran Echo $Java_Home..As long as this path that was echoed was/is correct..the below steps should fix the issue.


Then open your zshrc in a similar manner as you did with bash_profile and add the home path to the file.

You can also edit the bash_profile and zshrc files in your text editor.These files can be displayed using cmd + shift + . in your home folder.

After that,save,exit the terminal, close android studio and restart your machine.
Read this Article to learn more.