空指针错误在声明/初始化Java字符串缓冲区后发生。

huangapple go评论74阅读模式
英文:

Null pointer error after declaring / initializing Java string buffer

问题

我不断收到关于StringBuffer的空指针异常。

以下是示例代码:

StringBuffer buff = new StingBuffer("dummy" + "dummy2" + "dummy3");

我在初始化buff的第一行就收到一个空指针异常

所以我将其更改为

```java
StringBuffer buff = new StringBuffer();
buff.append("dummy" + "dummy1" + "dummy2");

现在我在buff.append这一行收到空指针异常
这是在初始化之后的第一步这非常奇怪

我正在疯狂地思考为什么在我实际初始化它时会收到空指针异常

有人能就此提供建议吗
英文:

I keep getting a Null pointer exception for the StingBuffer .

Below is the sample code ..

StringBuffer buff = new StingBuffer(“dummy”+”dummy2”
+”dummy3”);

I get an null pointer at the first line where buff is initialized .

So I changed this to

StringBuffer buff = new StringBuffer();
buff.append(“dummy”
+”dummy1”
+”dummy2”);

Now I get the null pointer exception at buff.append line.
Which is right after its initialization and it is very strange.

I am going crazy as to why I am getting a null pointer exception when I have actually initialized it .

Can anyone advice on this !!!

答案1

得分: 2

在将您的代码复制粘贴到集成开发环境中后,结果显示您正在使用错误的字符串常量分隔符。
您使用的是 而不是 "
这样代码应该可以正常工作。

英文:

After copy/ pasting your code into an IDE it turns out that you are using the wrong String constant delimiter.
you are using instead of " .
This way the code should work correctly.

答案2

得分: 0

看起来有些奇怪的NPE,为什么你应该因为<br/>和不正确的而在编译时错误。看看这个:

StringBuffer buff = new StringBuffer();
buff.append("dummy" + "dummy1" + "dummy2");
英文:

Looks strange with NPE, whey you should get a compile time error because of <br/> and incorrect . Check this out:

StringBuffer buff = new StringBuffer();
buff.append("dummy" + "dummy1" + "dummy2");

huangapple
  • 本文由 发表于 2020年10月24日 03:12:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64506031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定