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

Variable might not have been initialized?  RSS feed

 
Carly Griffin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm creating an infix calculator, and am currently working on the buildExpression method, which should use postfix expression to build an expression object (but essentially this resembles an evaluate postfix method).

For clarification, I have the following classes completed: Expression, BinaryExpression, Operator, and Token.
I'm simply having an issue with the return value, which should be the last expression on the stack. The error I'm running into is "variable r might not have been initialized", however I thought I did initialize towards the beginning of the code? Sorry if this is an obvious question; I'm just starting out in Java. Thank you in advance!

 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37222
519
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You set r inside the loop. What happens if you pass in an empty list? It doesn't have a value then.
 
Carly Griffin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, I don't know how I missed that!!
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sort if thing is very easy to miss, so don't worry about it.
And welcome to the Ranch
 
Carly Griffin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've since developed another issue when working with the same program and I would be EXTREMELY thankful for any help, as I am in a bit of a time crunch for getting this done. I am in the process of writing the class infixToPostfix, whose output will be a list of tokens representing the equivalent postfix expression. I'm encountering one error, "cannot find symbol
symbol : method getPriority()
location: interface Token"

I'm confused as to why I am unable to use my getPriority() method in this instance. I'll include the code that seems relevant here.
This is the class I'm currently working on:


This is the operator class where the getPriority() method comes from:


and this is the interface token:
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stack2 is a type List<Token> and Token doesn't have getPriority().
 
Carly Griffin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So would there be a simple way to get around this? I tried adding getPriority to Token, but I receive the error "Number is not abstract and does not override abstract method getPriority() in Token"
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simple way is to add getPriority() to your class Number. (BTW, "Number" is maybe not the best name, simply because it is the same name as java.lang.Number.)
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!