You can't possibly know all the laws on the book in even the very small area of the country that you reside in much less all the towns, counties and states that you might travel through. Sure you might know the gun laws, I damn sure do, but the bottom line is there are hundreds if not thousands of bullshit laws even your judges aren't aware of.
If you look hard enough you will find that every single last one of us, including judges who are paid explicitly to know the law, are criminals. You and I both know that the judge should have thrown this shit out regardless of the letter of the law and I'm a "black letter law" type of person. The only other argument in my mind is that part of this laws intent was to get people like in the OP off the streets because they are a danger to society and I refuse to believe that anyone is stupid enough to believe this person was a danger to society. Perhaps I am being naive but I have a sliver of hope left.