• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pointer to Scanner won't pass from method

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:



 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
"To do good, you actually have to do something." -- Yvon Chouinard
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic