This is a thing that can be quite difficult for foreign learners. Basically, the masculine gender in Accusative divides into animate* and inanimate. The animate nouns take the -a ending, thus being the same as Genitive. The inanimate look the same as Nominative.
And here's the catch: theoretically, you'd only need simple logic to figure out what is animate and inanimate. But there are a looot of exceptions, things that are (or at least 'can be') treated as grammatically animate without a real, logical reason. A lot of them are names of foods, especially fruit and vegetables, but it can go further. So unfortunately, sometimes you will just have to remember that it's an exception. Anyway, "kapelusz" is definitely not among them - it's 100% inanimate both logically and grammatically. Therefore "Mam ten kapelusz" is the only correct form.
*Actually animate is divided further: to masculine personal and masculine non-personal animate, but the difference will only show in plural: Widzę tego chłopca, widzę tego psa vs Widzę tych chłopców, widzę te psy. As in plural we usually talk about masculine-personal and not masculine-personal, there's no much point focusing on masculine non-personal animate because it will be treated the same way as any not-masculine noun, like "kobieta" or "dziecko".
You can read more about the problem of animacy here.