From: Bryan Duxbury Date: Fri, 12 Feb 2010 22:49:40 +0000 (+0000) Subject: THRIFT-702. TUnion's toString method throws NPE if the union is unset X-Git-Tag: 0.3.0~127 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=f94f008d80bf858e4ad635fda61f8cd2bd1be0a2;p=common%2Fthrift.git THRIFT-702. TUnion's toString method throws NPE if the union is unset git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@909645 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/java/src/org/apache/thrift/TUnion.java b/lib/java/src/org/apache/thrift/TUnion.java index 353e11c9..625b00d9 100644 --- a/lib/java/src/org/apache/thrift/TUnion.java +++ b/lib/java/src/org/apache/thrift/TUnion.java @@ -195,14 +195,20 @@ public abstract class TUnion implements TBase { @Override public String toString() { - Object v = getFieldValue(); - String vStr = null; - if (v instanceof byte[]) { - vStr = bytesToStr((byte[])v); - } else { - vStr = v.toString(); + String result = "<" + this.getClass().getSimpleName() + " "; + + if (getSetField() != null) { + Object v = getFieldValue(); + String vStr = null; + if (v instanceof byte[]) { + vStr = bytesToStr((byte[])v); + } else { + vStr = v.toString(); + } + result += getFieldDesc(getSetField()).name + ":" + vStr; } - return "<" + this.getClass().getSimpleName() + " " + getFieldDesc(getSetField()).name + ":" + vStr + ">"; + + return result + ">"; } private static String bytesToStr(byte[] bytes) { diff --git a/lib/java/test/org/apache/thrift/test/UnionTest.java b/lib/java/test/org/apache/thrift/test/UnionTest.java index 2527f4a7..1fc1522b 100644 --- a/lib/java/test/org/apache/thrift/test/UnionTest.java +++ b/lib/java/test/org/apache/thrift/test/UnionTest.java @@ -95,6 +95,10 @@ public class UnionTest { union = TestUnion.enum_field(SomeEnum.ONE); union.hashCode(); + + union = new TestUnion(); + // should not throw an exception + union.toString(); }