vb.net - Getting a null reference exception trying to push a zero to a stack -
well on last day of school trying finish program , can't life of me past null reference exception. code supposed average [count] numbers entered user using stack of integers. throwing null reference exception because attempted count of stack empty, added line push 0 on it. program stops @ line same error (nullreferenceexception). if change stack accept strings works fine, need accept integers. can :)
module module1 sub main() dim count integer = 16 dim stack new stack(of integer) stack.push(0) console.writeline("please enter " & count & " numbers") while stack.count - 1 = count stack.push(console.readline) if not isnumeric(stack.peek) console.writeline(stack.pop & " not number please try again.") end if loop end sub end module
you're declaring variable named stack
, not creating stack
object. instead (with new
keyword):
dim stack new stack(of integer)
without new
, variable contain null reference, why getting nullreferenceexception
whenever attempt variable.
Comments
Post a Comment