Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException , I dont understand this error.  RSS feed

 
Sergio Barcelona
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all , I'm doing one exercise with java , is one simple coder , only for learn , but in coder class have one error that I dont understand

in the debugger all work good , and split_string and word is bound with values (line 46 and 47) but if I run the app the error jump.
This is the code.





Anybody can help me with this error?
cheers.
 
Liutauras Vilda
Marshal
Posts: 4633
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably you invoke word.length() on null reference. Could you please tell the input on which error happens?
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Insert a line before No 46If I remember correctly, the %b tag prints false iff a non‑Boolean reference type as its argument is null. I cannot see why either of those references shou‍ld point to null. It is possible that you have a null somewhere in your array, but I can't see why. The StringBuilder is initialised in the constructor. The Character methods are static, an not therefore susceptible to problems with nulls. Your number is an int, and primitives can't be null.
Is there any possibility that spltiAndShuffle is introducing any nulls into the array?

[edit]Make your StringBulder a local variable in the method and return a String with myStringBuilder.toString(). Or make that String a field.
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Probably you invoke word.length() on null reference. Could you please tell the input on which error happens?
Why does it say line 47 then? And if word is null, why does it wait until line 47 before throwing the Exception?

OP: Are the line numbers in what you posted the same as in your original code?
 
Liutauras Vilda
Marshal
Posts: 4633
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Liutauras Vilda wrote:Probably you invoke word.length() on null reference. Could you please tell the input on which error happens?
Why does it say line 47 then? And if word is null, why does it wait until line 47 before throwing the Exception?

It appears I meant line 45, I'm having problems a bit with line numbering here.

OP, please clarify what Campbell asked. However, exception is indeed thrown because of method invocation on line 45. And your problems of course start the way earlier, in splitAndShuffle method.
Campbell already gave you suggestions to put some print statements in order to get convinced for yourself.

Look at your splitAndShuffle() method.
Line 19 create an array finalString of length 15 (text: "Liutauras Vilda").
Line 22 split ("Liutauras Vilda") into 2 pieces and add to stringShuffle list.
Line 36 you add those 2 words "Liutauras" and "Vilda" into 15 length array finalString.

So you end up with (returning it from splitAndShuffle()):
[Liutauras, Vilda, null, null, null, null, null, null, null, null, null, null, null, null, null]

And iterating it in codeText() method.

Anyway, ignoring all this. I think you need to decompose all that code into a lot multiple methods, so you could follow the code. I was unable to follow until I putted into my IDE environment.



 
Sergio Barcelona
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well , this the new code  , i donĀ“t understand why no error jump , but the out of app  is
[Ljava.lang.String;@12a3a380
[Ljava.lang.String;@12a3a380

The code is.



 
Sergio Barcelona
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is with this parameters in consola
 
Liutauras Vilda
Marshal
Posts: 4633
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have explained what is happening. Please read it above.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you copy the full contents of the console from when you execute the program and paste it here?
It should include the commandline, all of the print out from the program and the error message.
 
Sergio Barcelona
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok , I change



for



I think is correct now, no? , but the out of program , why is this??

 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why is this?? 

That looks like the String returned by the toString() method for a String array.  If you want to see the array's contents try the Arrays class's toString method:
 
Sergio Barcelona
Greenhorn
Posts: 10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well finally the program is funtional , thanks to all for responses .




It Is not the  better program in the world but I learn whith it.
Cheers.
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Our pleasure to help

It shows the hazards of using the new operator to create an array. I was discussing sizes of arrays and array initialisers here. If you use String#split, it returns an array, so simply accept the array and forget all about lengths.
String[] words = myLine.split("...");
 
Sergio Barcelona
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,I can see the problem with arrays, but my first problem is my bad english , I can hardly understand what I read
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!