- RuleBasedCollator* collator = (RuleBasedCollator*)formatter->getCollator();
- CollationElementIterator* strIter = collator->createCollationElementIterator(str);
- CollationElementIterator* prefixIter = collator->createCollationElementIterator(prefix);
+ const RuleBasedCollator* collator = formatter->getCollator();
+ if (collator == NULL) {
+ status = U_MEMORY_ALLOCATION_ERROR;
+ return 0;
+ }
+ LocalPointer<CollationElementIterator> strIter(collator->createCollationElementIterator(str));
+ LocalPointer<CollationElementIterator> prefixIter(collator->createCollationElementIterator(prefix));
+ // Check for memory allocation error.
+ if (strIter.isNull() || prefixIter.isNull()) {
+ status = U_MEMORY_ALLOCATION_ERROR;
+ return 0;
+ }