diff --git a/newrelic-agent/src/main/java/com/newrelic/agent/attributes/AgentAttributeSender.java b/newrelic-agent/src/main/java/com/newrelic/agent/attributes/AgentAttributeSender.java index 7a9773e489..cca52466d6 100644 --- a/newrelic-agent/src/main/java/com/newrelic/agent/attributes/AgentAttributeSender.java +++ b/newrelic-agent/src/main/java/com/newrelic/agent/attributes/AgentAttributeSender.java @@ -34,7 +34,11 @@ protected Map getAttributeMap() { } public void removeAttribute(String key) { - getAttributeMap().remove(key); + Map attributeMap = getAttributeMap(); + if (attributeMap == null) { + return; + } + attributeMap.remove(key); } } \ No newline at end of file diff --git a/newrelic-agent/src/test/java/com/newrelic/api/agent/NewRelicApiImplementationTest.java b/newrelic-agent/src/test/java/com/newrelic/api/agent/NewRelicApiImplementationTest.java index 2428c0f1ef..a036f1af44 100644 --- a/newrelic-agent/src/test/java/com/newrelic/api/agent/NewRelicApiImplementationTest.java +++ b/newrelic-agent/src/test/java/com/newrelic/api/agent/NewRelicApiImplementationTest.java @@ -274,6 +274,21 @@ public void setUserId_withInvalidId_doesNotSetAttribute() { Mockito.verify(mockAgentSender, Mockito.times(0)).addAttribute("enduser.id", "123", "setUserId"); } + @Test + public void setUserId_withoutActiveTxn_isNoOp() { + mockOutServices(); + AttributeSender mockSender = Mockito.mock(AttributeSender.class); + AgentAttributeSender mockAgentSender = new AgentAttributeSender(); + NewRelicApiImplementation target = new NewRelicApiImplementation(mockSender, mockAgentSender); + + try(MockedStatic mockTxn = Mockito.mockStatic(Transaction.class)) { + mockTxn.when(() -> Transaction.getTransaction(false)).thenReturn(null); + //These used to throw an NPE when there was no active transaction so the "assertion" is that these method calls execute without an exception + target.setUserId(null); + target.setUserId(""); + } + } + @Test public void setUserName_withValidName_setsNameAttribute() { mockOutServices();