I have encountered a strange NullPointerException when invoking method AppendLine() on a StringBuilder at .NET Framework 4.5.2.
The exception trace is shown as below:
System.NullReferenceException: NullReferenceException Object reference not set to an instance of an object.
at System.Text.StringBuilder.Append(String value)
at System.Text.StringBuilder.AppendLine(String value)
......
And the dump of application is shown as follow:
It is clearly that the exception is caused due to field m_ChunkChars of StringBuilder.
Also, there is NO concurrent or reflection operations in relevant context.
I have attempted to analysis the source code of StringBuilder, but eventually failed to find any possible reasons.
Could anyone give any thoughts about possible conditions that may lead to such situation?
public void SomeLocalMethod()
{
var log = new StringBuilder();
//some operations
log.AppendLine("Some log text.")
.Append("Some log")
.AppendLine("Some log");
}
The code is quite simple. We constructed a StringBuilder in a method to log several operations.
The reason why I think it is not a thread-safe problem is that this StringBuilder is a local variable and no reference is passed outside of its method.
Also, we found on system logs that such errors are continuous and frequent on multiply nodes, which I think implies it is possibly not caused by unexpected access during object initialization.
The code is run at .NET framework 4.5.2 again.