×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    6

    RecordEnumeration throwing null pointer exception

    I'm trying to get a RecordEnumeration from an existing recordstore on a n-gage S60 phone. I keep geting a null pointer exception when assigning the enumeration. I cannot see what I'm doing wrong, so if anyone can shead some light I'd be greatful.

    I've narrowed down the line that's throwing the exception to the assigning of RecordEnumeration re by surrounding each line within Try/Catch blocks

    What I'm trying to do is build a Displayable List consiting of the records within the recordstore, using the RecordEnumeration.

    // This code assumes that RecordStore rs is already open
    // assumes Display display has been set
    // assumes Displayable prevDisplay has been set

    public List buildList( String title )
    {
    List rList = null;
    int count = 0;
    RecordEnumeration re = null;
    rList = new List( title, List.IMPLICIT );

    // get number of Types in RecordStore
    try {
    count = rs.getNumRecords();
    // next line causes Null Pointer Exception
    re = rs.enumerateRecords( null, new AlphaComparator(), false );

    // build list if there are items to use
    if ( count > 0 ) {
    for ( int i=0; i < count; i++ ) {
    rList.append( new String( rs.getRecord( re.nextRecordId())), null );
    }
    }
    }
    catch ( Exception e ) {
    String errStr = "count:" + count;
    if ( re == null ) errStr += " re:NULL";
    errStr += e.toString();
    Alert alert = new Alert( "Error", errStr, null, AlertType.ALARM );
    alert.setTimeOut( Alert.FOREVER );
    display.setCurrent( alert, prevDisplay );
    }

    return rList;
    }

    The Alert displays the expected number of records within rs, so the rs exists and is open.

    Any light on this subject would be helpful.

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    6

    Re: RecordEnumeration throwing null pointer exception

    I've found what is throwing the null pointer exception. My AlphaComparator() that implements RecordComparator is comparing two strings. It is the strings that are throwing the null pointer exception.

    Now my problem is how to read strings from the RecordStore and comparing them.

    currently I'm doing:

    public int compare( byte[] rec1, byte[] rec2 ) {
    int rValue = EQUIVALENT;
    int t_strCmp;
    String s1,s2;

    s1 = new String( rec1 );
    s2 = new String( rec2 );
    t_strCmp = s1.compareTo( s2 );

    if ( t_strCmp < 0 ) rValue = PRECEDES;
    if ( t_strCmp == 0 ) rValue = EQUIVALENT;
    if ( t_strCmp > 0 ) rValue = FOLLOWS;

    return rValue;
    }
    If anyone can shead some light as to why the strings are not being created on the Series 60 I'd be greatful.

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    6

    Re: RecordEnumeration throwing null pointer exception

    I solved the problem. The problem was the way I was handling the RecordComparator's compare( byte[] rec1, byte[] rec2 ) function.

    I had wanted to do an Alpha sort on records but have a specific string, "misc" to be the last record in the enumeration.

    Old code, would work standard java but throws a Null Pointer Exception because rec1 would be null after a few itterations
    Code:
    public int compare( byte[] rec1, byte[] rec2 ) {
      int rValue = EQUIVALENT;
      int t_strCmp = 0;
      String s1,s2;
    		
      s1 = new String( rec1 );
      s2 = new String( rec2 );
    		
      t_strCmp = s1.compareTo( s2 );
      if ( t_strCmp < 0 ) rValue = PRECEDES;
      if ( t_strCmp == 0 ) rValue = EQUIVALENT;
      if ( t_strCmp > 0 ) rValue = FOLLOWS;
    	
      if ( s1.compareTo( "misc" ) == 0 ) rValue = FOLLOWS;
      if ( s2.compareTo( "misc" ) == 0 ) rValue = PRECEDES;
    		
      return rValue;
    }

    fixed code, sorts all records and will return PROCEDES if rec1 has "misc" forcing the "misc" record to be the last in the enumeration.
    Code:
    public int compare( byte[] rec1, byte[] rec2 ) {
      int rValue = EQUIVALENT;
      int t_strCmp = 0;
      String s1,s2;
    		
      s1 = new String( rec1 );
      s2 = new String( rec2 );
    		
      // put record 'misc' at end of list
      if ( s1.compareTo( "misc" ) == 0 ) {
        rValue = FOLLOWS;
      }
      else {
        t_strCmp = s1.compareTo( s2 );
        if ( t_strCmp < 0 ) rValue = PRECEDES;
        if ( t_strCmp == 0 ) rValue = EQUIVALENT;
        if ( t_strCmp > 0 ) rValue = FOLLOWS;
      }
    	
      return rValue;
    }

Similar Threads

  1. getNumRecords/ null pointer exception
    By cjmarsha in forum Mobile Java General
    Replies: 3
    Last Post: 2005-07-30, 13:09
  2. Series 60 null pointer exception
    By JohnnyC1969 in forum Mobile Java General
    Replies: 5
    Last Post: 2005-03-30, 13:26
  3. another Null pointer exception
    By kvchen1985 in forum Mobile Java Tools & SDKs
    Replies: 1
    Last Post: 2004-06-28, 15:33
  4. 7650 null pointer exception
    By gwhistance in forum Mobile Java General
    Replies: 0
    Last Post: 2003-04-04, 08:56
  5. can not successfully link any sample using .NET
    By lobotomat in forum Symbian Tools & SDKs
    Replies: 2
    Last Post: 2002-08-20, 00:29

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •