Nightmares are helpful to our survival or else they probably would have been done away with by evolution, said Deirdre Barrett, a psychologist at Harvard University. Barrett theorizes that nightmares act as the brain’s way of focusing a person’s attention on issues they need to address.
http://www.livescience.com/32730-why-do-we-have-nightmares.html