DataElement with DATSEQ with zero elements
Hi,
I have this problem.
I am putting some stuff inside a DataElement to then store it inside my ServiceRecord.
The information I want to store is a List of items. This list is not always the same length, it depends on the situation. The problem is, the list can also be of length 0. This means that addElement will never be called as there will be no elements to add.
See the following code snippet:
DataElement dataElement =new DataElement(DataElement.DATSEQ);
Enumeration en = myVector.elements();
while (en.hasMoreElements())
{
dataElement.addElement(en.nextElement());
}
As you can see, depending on how many items in the Vector, elements are added into the DataElement. Incase of zero elements, nothing will be added.
My problem is that I receive a ClassCastException "wrong data type: 0" on my phone when I retreive the information from the ServiceRecord.
See the following code snippet:
Enumeration en = (Enumeration) data.getValue();
while (en.hasMoreElements())
{
Object object = en.nextElement();
}
The first line is the line I suspect of causing the error. The error doesn't occur in the emulator, but it does on my Nokia 6021. By putting debug messages in my code I narrowed the Exception down to that line.
My guess is that data.getValue() returns null if no elements are added to the DataElement. This would cause the ClassCastException. Although, then I would expect this same error in the emulator.
I can't find in the specification what getValue() should return in such a case. In my opinion, it should return an empty Enumeration.
Anyone has any idea?
If there is no other solution, I will solve it by adding a check. But I don't like this.
See the following code snippet:
Object object = data.getValue();
if(object !=null)
{
Enumeration en = (Enumeration) object ;
while (en.hasMoreElements())
{
Object object = en.nextElement();
}
}

