It's possible (though I haven't done any research) that the law may require a uniformed officer to make the stop and the hat is part of the uniform. No hat -> not uniformed -> not a legal stop.
Yes, that's the only explanation which would make any sense -- but it seems like a rather extraordinary combination of legislators and judge would be required to (a) write the law that way and (b) interpret a law so literally as to throw out a ticket over a missing hat.
This is interesting actually. I kind of "took his word for it" and never put too much thought into it. After about 20+ minutes of Googling I have yet to find a definitive answer. I guess it just depends on jurisdiction. I'm not sure if I would have the guts to say, "Judge Officer here wasn't wearing his hat when he gave me this ticket." So I don't know, I read a few things that supported it and a few that didn't. I should ask my director friend if it's true. Ha