英文:
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");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论