• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't understand where the NullPointerException is coming from  RSS feed

 
Debdeep Ganguly
Greenhorn
Posts: 17
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote this piece of code in which if i enter "countries" as a choice, i'm getting a NullPointerException sometimes. I need to understand where my problem is. I know the code looks very ugly, full of exceptions. I'm at a very beginner level hence feel free to point out anything. B.t.w please don't worry about that CustomMessageException.
I've arranged three files at root directory - "Animal.txt", "Country.txt" (containing AUSTRIA CANADA BAHRAIN JAPAN SYRIA) , "Fruit.txt" and "Master.txt" (for writing the score)



 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Debdeep Ganguly wrote:I wrote this piece of code in which if i enter "countries" as a choice, i'm getting a NullPointerException sometimes. I need to understand where my problem is.

Hi Debdeep, and welcome to JavaRanch.

Could you send us a copy of your NullPointerException stacktrace? (an exact copy please; no "interpretation")

That'll help us no end.

Winston
 
Debdeep Ganguly
Greenhorn
Posts: 17
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
The stack trace rather exception message is:

Type the category name below and press 'Enter'
countries
Guessing country name game: You need to guess the country name as the blanks appear, a random name shall be picked out for you
java.lang.NullPointerException
java.lang.StringIndexOutOfBoundsException: String index out of range: 6

Process finished with exit code 0
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Debdeep Ganguly wrote:The stack trace rather exception message is:

That doesn't look like the exact message, because it doesn't have any class names or offsets.

Please send us a verbatim copy of the entire stacktrace - ie, just use cut + paste.

Winston
 
Debdeep Ganguly
Greenhorn
Posts: 17
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Debdeep Ganguly wrote:[code=java]java.lang.NullPointerException
at FillInTheBlanksGame.Countries(FillInTheBlanksGame.java:133)

OK, well that says that the NPE is occurring at line 133 of your FillInTheBlanksGame class, and that that line is inside your Countries() method (which, BTW, should be 'countries()').

Now from the rest of the message, I think that is line 132 of what you've supplied us, ie:
  while (i <= random.length()) {
which suggests that 'random' is null.

And I can certainly see how that might happen, because at line 130 you set it to
  a[r.nextInt(a.length)]
when you don't know if you filled the array 'a' completely or not.

I suspect it should be:
  String random = a[r.nextInt(count)];
however, that whole method is extremely tortuous.

Why not just read all the "countries" in countries.txt into a List<String> once at the start rather than every time you call the Countries() method? It'll be much quicker and far less complicated.

HIH

Winston
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also, as a side note, from the stack trace, there is a bit of recursion happening. Recursion is fine ... if ... that is what you intended. And it is a technique that you understand and feel comfortable using.

Otherwise, for beginners to the technique, it just creates a mess, that may need to be fixed later.

Henry
 
Debdeep Ganguly
Greenhorn
Posts: 17
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Winston Gutkowski

Thank you very much.
I got what are trying to explain , so i guess i may also use
Sorry for the mistakes i did by messing up the line numbers.
And thanks for that List suggestion too. I will certainly change that.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!