This error is most probably because of corrosion inside the charging port. Observe closely for any physical damage or color change.
You can try cleaning the port gently with an anti static brush or try blowing in some air into the port.
You can diagnose the root causal of your case by setting the device to the safe mode as some third party apps might cause a bug in your device.
You can activate the Safe Mode option through the following steps:
- Restart your device
- When it shows Samsung logo, you have to press and hold the volume down key till you reach Safe Mode
- You can try to observe the device for Two or Three hours
If the device is working fine in the safe mode, it means that this issue caused by some Third party apps you already installed on your device in the normal mode. Kindly check your Third party apps. and remove them one by one until resolving the issue.