The gun of an off-duty NYPD officer discharged as the officer engaged in a struggle with a man who was trying to rob him in Patchogue Friday night, police said.

No one was struck by the gunfire, but the officer and the man accused of attacking him, 21-year-old Timothy Frederique, were treated at Brookhaven Memorial Hospital Medical Center for minor injuries suffered in the struggle, police said. Police said the officer was walking home from the Patchogue train station on Jennings Avenue when Frederique attacked him from behind at about 10:15 p.m. Frederique, of Brentwood, attempted to steal the officer's backpack, police said.

The officer identified himself as a cop, reached for his weapon and at that point, Frederique also went to grab the officer's gun, police said. That's when the struggle ensued and the gun was discharged, police said.

The officer was held overnight at the hospital. Frederique was treated and released. Frederique is charged with second degree robbery and second degree assault. He will be arraigned in First District Court in Central Islip on Sunday.