Skip to content

Commit ececb62

Browse files
stevensegersfpoyer
authored andcommitted
Fix NullPointerException in importData
NPE happened when Odoo returns an error that is not linked to a specific field, while trying to parse the returned message. Fix cherry-picked from 88bc017, migrated to Java 8 lambdas/streams for efficiency. Fixes issue #24, replaces PR #25.
1 parent 568b63d commit ececb62

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/main/java/com/debortoliwines/odoo/api/ObjectAdapter.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121

2222
import java.text.SimpleDateFormat;
2323
import java.util.ArrayList;
24+
import java.util.Arrays;
2425
import java.util.Date;
2526
import java.util.HashMap;
2627
import java.util.List;
2728
import java.util.Map;
2829
import java.util.TimeZone;
2930
import java.util.concurrent.ConcurrentHashMap;
31+
import java.util.stream.Collectors;
3032
import java.util.stream.Stream;
3133

3234
import org.apache.xmlrpc.XmlRpcException;
@@ -646,17 +648,14 @@ private void importDataV7(RowCollection rows, Object[][] importRows) throws XmlR
646648

647649
HashMap<String, Object> results = command.Load(modelName, targetFieldList, importRows);
648650

649-
// There was an error. ids is false and not an Object[]
650651
if (results.get("ids") instanceof Boolean) {
651-
StringBuilder errorString = new StringBuilder();
652-
Object[] messages = (Object[]) results.get("messages");
653-
for (Object mes : messages) {
654-
HashMap<String, Object> messageHash = (HashMap<String, Object>) mes;
655-
errorString.append("Row: " + messageHash.get("record").toString() + " field: "
656-
+ messageHash.get("field").toString() + " ERROR: " + messageHash.get("message").toString()
657-
+ "\n");
658-
}
659-
throw new OpeneERPApiException(errorString.toString());
652+
// There was an error. ids is false and not an Object[]
653+
Map<String, Object>[] messages = (Map<String, Object>[]) results.get("messages");
654+
String errorString = Arrays.stream(messages)
655+
.flatMap(m -> m.entrySet().stream())
656+
.map(e -> String.join(":", e.getKey(), e.getValue().toString()))
657+
.collect(Collectors.joining("\n"));
658+
throw new OpeneERPApiException(errorString);
660659
}
661660

662661
// Should be in the same order as it was passed in

0 commit comments

Comments
 (0)