Skip to content

Commit 09b6af4

Browse files
committed
Merge pull request stleary#160 from johnjaylward/FixXMLNPE
Fixes possible NullPointerException in XML.toString(object, tagName)
2 parents b0191a6 + 637c1fe commit 09b6af4

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

XML.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.json;
22

33
/*
4-
Copyright (c) 2002 JSON.org
4+
Copyright (c) 2015 JSON.org
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal
@@ -30,7 +30,7 @@ of this software and associated documentation files (the "Software"), to deal
3030
* This provides static methods to convert an XML text into a JSONObject,
3131
* and to covert a JSONObject into an XML text.
3232
* @author JSON.org
33-
* @version 2014-05-03
33+
* @version 2015-10-14
3434
*/
3535
public class XML {
3636

@@ -468,23 +468,25 @@ public static String toString(Object object, String tagName)
468468
// XML does not have good support for arrays. If an array appears in a place
469469
// where XML is lacking, synthesize an <array> element.
470470

471-
} else {
471+
}
472+
if(object!=null){
472473
if (object.getClass().isArray()) {
473474
object = new JSONArray(object);
474475
}
476+
475477
if (object instanceof JSONArray) {
476478
ja = (JSONArray)object;
477479
length = ja.length();
478480
for (i = 0; i < length; i += 1) {
479481
sb.append(toString(ja.opt(i), tagName == null ? "array" : tagName));
480482
}
481483
return sb.toString();
482-
} else {
483-
string = (object == null) ? "null" : escape(object.toString());
484-
return (tagName == null) ? "\"" + string + "\"" :
485-
(string.length() == 0) ? "<" + tagName + "/>" :
486-
"<" + tagName + ">" + string + "</" + tagName + ">";
487484
}
488485
}
486+
string = (object == null) ? "null" : escape(object.toString());
487+
return (tagName == null) ? "\"" + string + "\"" :
488+
(string.length() == 0) ? "<" + tagName + "/>" :
489+
"<" + tagName + ">" + string + "</" + tagName + ">";
490+
489491
}
490492
}

0 commit comments

Comments
 (0)