Jelanco
Jelanco

Reputation: 1

Don't know how to solve an "unresolved reference."

I am 2 hours into my first Kotlin, and any coding, lesson and I am following along with a tutorial.

I have come across an "Unresolved reference" and am not sure how to resolve it so that I can continue following along with my lesson. There are several options presented, such as: creating local variable, parameter, property and reference.

As stated, I have 2 hours of experience and no idea what these mean yet. Any insight would be appreciated, thanks you. Screenshot

Upvotes: 0

Views: 987

Answers (1)

cactustictacs
cactustictacs

Reputation: 19622

I'm gonna paste your image in so you can see what I'm talking about - in general, you should always paste code, not screenshots (adding a screenshot as well is fine), but in this case the screenshot is helpful:

enter image description here

See how the else is underlined in red? That's indicating some kind of error, and for a basic language keyword like else, that's a sign you've gone wrong with the syntax - what you've written doesn't make sense, for some reason. Hovering over the red squiggle should give you some info about what the problem could be (it can't always tell for sure, if the code doesn't make sense)

Also, see how everything after the else is greyed out? It's not showing up as code in the normal colours - so it's not that there's code that doesn't make sense, it's not even being recognised as code at all. Again, when a whole section "goes wrong" like that, it's a sign that something's messed up with the syntax, usually something earlier that's causing a problem. And since the else before it is highlighted as a problem, that's a good place to look!


That's just general advice - your issue is this:

if (enteredName == "") { // opening brace for the if block
    // some stuff
} // closing brace for the if block

// we just closed the if block - so what's this closing?
} else {

You'll see braces a lot in Kotlin - in simple terms, they wrap a bunch of code into a single block, so when that block is run, it can do several steps. So your if condition, if it evaluates to true, will run everything in that block that follows it. If it evaluates to false, it won't. And in that case, if there's an else statement following it, it'll run whatever follows that instead.

All your if/else stuff is happening inside that click listener you're defining:

submitButton.setOnClickListener {
    // some code to run when the button is clicked
}

So those braces hold the code that runs when the listener fires. But what you've written is this:

submitButton.setOnClickListener {
    if (something) { // open if
         // stuff
    } // close if
} // close click listener

// this is now outside the click listener block
else {

That else has to follow an if - but it doesn't, it follows that setOnClickListener line, because that brace closed the lambda (the code block) and ended that line. Now the next line is else { which makes no sense to Kotlin, and that's why it's tripping up, you're getting a red error mark, and the code following it is greyed out because Kotlin doesn't know what to do with it. It's extremely confused.


So you need to make sure your braces are paired up, and each closing brace is in the correct place to close its counterpart. They close in reverse order, right? Basically backing out of each block. Making sure each block is indented an extra level helps, because then you can visually see where each block starts and ends (and it draws a vertical line from the start to the end of each one). If you just remove that extra closing brace, you'll be missing one, because it's just in the wrong place. So you'll have to work out where to put it! Where the listener block ends.

That's uh a really long post to say "one of your braces is in the wrong place" but I hope it helps you understand what's going on and how to spot this kind of thing in the future. You'll have to handle these blocks a lot, so it's good to get used to wrangling them. It's pretty simple once you get it!

Upvotes: 2

Related Questions