I found a way to reproduce this, but it takes some fiddling with changing a set back and forth between a gateway and homework set type. I tested this with WeBWorK 2.17 and you are using WeBWorK 2.16 (based on the line number in the message), but the general logic in the code hasn't changed much here.
Basically, the problem is that the code never actually checks to see if a given set is a gateway quiz when determining if the display of scores is allowed for the set. It only checks if the hide_score flag is set for the set. Usually this is only set for gateway quizzes, so it is usually enough. However, if you create a set and change its type to a "gateway/quiz", and then set "Show Scores on Finished Assignments?" to No and save, and then change the set type back to "homework" and save, then the hide_score flag will still be set in the database, and if a student has been assigned the set and tries to open grades the error you show occurs.
To fix this find the offending homework set and open it in the homework sets editor, and save settings one more time. That is enough to reset the hide_score flag back to NULL, and will fix the problem.
If you have access to the database, then execute "select set_id from course_id_set where hide_score='Y' and assignment_type='default';" to list all homework sets that have hide_score incorrectly set. Of course change "course_id" to the actual course id. Then for all of those sets listed open them in the homework sets editor and click save to fix them.