Shelley Malil Shelley Malil

The 40-Year-Old Virgin actor accused of stabbing his girlfriend 20 times said he thought she was someone else attacking him in the dark.

Testifying in his own defense Thursday in a Vista, Calif., courtroom, Shelley Malil insisted he never intended to kill or hurt Kendra Beebe in her home in August 2008, according to People.

"I'm sorry," Malil said. "I had no idea. I saw the pictures (of her wounds) for the first time, I was stunned. When I look at those pictures, I still can't believe the knife I was holding was responsible for all those injuries."

Check out other celebrity scrapes with the law

Malil, who played one of Steve Carell's electronics store coworkers in Virgin, said when he arrived at Beebe's house, he found her drinking wine with another man, David Maldonado. Malil said he thought Maldonado was about to come after him and he grabbed a knife to defend himself.

After a scuffle, Malil said he thought Maldonado was going to his car to get a weapon. "I'm almost positive this guy's got a gun and coming after me," he said. "I come around the corner. It's eerily quiet. All of a sudden, out of nowhere, I get hit from the back on the side of the head. It felt like [a] cold granite countertop."

At that point, Malil said he started slashing in self-defense until he heard Beebe shouting and realized she was the one he was stabbing.

Malil has pleaded not guilty to charges of premeditated attempted murder, assault with a deadly weapon and residential burglary. He could be sentenced to 21 years to life in prison if convicted. Closing statements in the trial are expected Tuesday.