1 Reply Latest reply: Jul 4, 2007 8:01 PM by 843851 RSS

    DataElement with DATSEQ with zero elements

    843851
      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();
          }
      }
        • 1. Re: DataElement with DATSEQ with zero elements
          843851
          Ok the "temporary" solution I posted also doesn't work.
          See the following code snippet:
          Object object = data.getValue();
          if(object != null)
          {
              Enumeration en = (Enumeration) object ;
              while (en.hasMoreElements())
              {
                  Object object = en.nextElement();
              }
          }
          Apperently data.getValue(), is the one throwing the exception. In the specification it states that this will happen is the type is anything other than DATALT or DATSEQ.

          In my case it really should be DATSEQ, but I guess this information is not sent through Bluetooth if there are no elements within the DataElement. This might cause the ClassCastException.
          The new solution is this:
          When I fill the DataElement, I check if the list contains zero elements. If so, then I do not initalize the DataElement as a DATSEQ but as NULL.

          When extracting the data from it, I check if the datatype is DATSEQ. If so, I extract the data, if not, I just ignore it and assume it is an empty list.

          See the following two code snippets:
          public DataElement toDataElement()
          {
              DataElement dataElement;
              if(myVector.size() > 0)
              {
                  dataElement = new DataElement(DataElement.DATSEQ);
                  Enumeration en = myVector.elements();
                  while (en.hasMoreElements())
                  {
                      dataElement.addElement(en.nextElement());
                  }
              }
              else
              {
                  dataElement = new DataElement(DataElement.NULL);
              }
          }
          
          public MyClass(DataElement data)
          {
              myVector = new Vector();
              if(data.getDataType() = DataElement.DATSEQ)
              {
                  Enumeration en = (Enumeration) data.getValue();
                  while (en.hasMoreElements())
                  {
                      myVector.addElement(en.nextElement());
                  }
              }
          }