0

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 !!!

hous
  • 92
  • 1
  • 11
dsreddy
  • 9
  • 3
  • 1
    From a first glance, your code looks correct. So please provide a REAL [mcve] that we can run to repro the problem, and include the stack trace in your question too. – GhostCat Oct 23 '20 at 19:21
  • And of course, study https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it ... as sooner or later, your question will almost inadvertently be closed as duplicate of that one. – GhostCat Oct 23 '20 at 19:22

2 Answers2

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
hous
  • 92
  • 1
  • 11
  • I was typing the sample using IPhone . But the point is why would I get a null pointer exception when I have initialized it. I dont want to share the exact code as it is company code. – dsreddy Oct 26 '20 at 13:50
  • I ran your code and it worked fine. Please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – hous Oct 26 '20 at 20:50
0

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");
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35