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

Pointer to Scanner won't pass from method  RSS feed

 
Brett Michaels
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating a scanner in one method of a class and then using it in another method. When I try to use it in the method, it's null. I don't understand why since the pointer was defined at the beginning of the class.

I am getting a null pointer exception:



 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 26 you define a new method-local variable with the same name as the class member. This hides the class member named scan, and inside the method you use the method-local variable only, so you never assign anything to the class member named scan.

You should remove the variable definition and simply assign to the class member:
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also remember that Java doesn't know about pointers, you're probably used to that terminology from C/C++.

And welcome to the Ranch!
 
Brett Michaels
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, that was the problem. I didn't realize that a new variable was being creating so hiding didn't occur to me. Now it seems silly when I think about it!

I like that you guys caught my pointer reference can could see I learned OOP w/C++!

Thank you very much for your quick help! This site is great.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!