I am working with the Microsoft Face API to detect attributes of faces such as age, gender, and emotion. The following code is working for me: faces[position].faceAttributes.age and I am able to get the estimated age. (faces[] is an array of the type Face )
However, when I try to get the probability that the face is happy, I am running into the following error:
Attempt to read from field 'double com.microsoft.projectoxford.face.contract.Emotion.happiness' on a null object reference.
This is how I am getting the probability that the person is happy:faces[position].faceAttributes.emotion.happiness
Similarly, when I try: faces[position].faceAttributes.emotion, it returns null.
I know that faces[position].faceAttributes is not null because it works for other attributes like age and gender but I am unable to figure out why it is not working for emotions. Does anyone know why this is occurring and what I can do to get it to work?
Update:
For those who are experiencing the same problem, in the AsnycTask where you are processing the faces, you must include the attributes you wish to detect otherwise it says that it is a null object reference when you refer to them later. Initially, I had FaceServiceClient.FaceAttributeType.Smile and that was why it was giving me an error when trying to determine emotions. The following code goes in the doInBackground method:
FaceServiceClient.FaceAttributeType[] faceAttr = new FaceServiceClient.FaceAttributeType[]{
FaceServiceClient.FaceAttributeType.HeadPose,
FaceServiceClient.FaceAttributeType.Age,
FaceServiceClient.FaceAttributeType.Gender,
FaceServiceClient.FaceAttributeType.Emotion,
FaceServiceClient.FaceAttributeType.FacialHair
};