This error can occur after upgrading to drupal 9.
Call to undefined method DrupalfileEntityFile::url()
Update
The following code is working on Drupal 8 but if you are upgrading drupal 8 to drupal 9 then Change your code from:
$url = $file->url();
to
$url = $file->createFileUrl();