Compile message step 12 of quick start guide for Android

Using the Quick Start Guide for Android.

Step 12 has me enter code to store an instance of the comment class from the registered user.

I entered the code:

Backendless.Persistence.save( new Comment( “I’m in!”, user.getEmail() ), new BackendlessCallback<Comment>()
{
@Override
public void handleResponse( Comment comment )
{
Log.i( “Comments”, "Got new comment from " + user.getEmail() );
}
} );

in the onCreate method of the main activity. The call: user.getEmail() is generating a compile error. It says
“Variable user is accessed from within an inner class, needs to be declared final”. How do I resolve this? Am I putting the code in the correct place?

It sound like you do not put it in the right place. If you could post your code here, it will make it easier for us to suggest what should be changed.

package com.example.ealexander.trip;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.backendless.Backendless;
import com.backendless.BackendlessUser;
import com.backendless.async.callback.BackendlessCallback;

public class MainActivity extends ActionBarActivity {
private final String APP_ID = “key”;
private final String SECRET_KEY = “key”;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

String appVersion = “v1”;
Backendless.initApp(this, APP_ID, SECRET_KEY, appVersion);

BackendlessUser user = new BackendlessUser();
user.setEmail("ealexander@cvsd356.com");
user.setPassword(“xxxx@”);
Backendless.UserService.register(user, new BackendlessCallback<BackendlessUser>()

{
@Override
public void handleResponse(BackendlessUser backendlessUser) {
Log.i(“Registration”, backendlessUser.getEmail() + " successfully registered");
}
});

Backendless.Persistence.save( new Comment( “I’m in!”, user.getEmail() ), new BackendlessCallback<Comment>()
{
@Override
public void handleResponse( Comment comment )
{
Log.i( “Comments”, "Got new comment from " + user.getEmail() );
}
} );

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

Variable “user” is declared inside “MainActivity” class, and you’re trying to access it from anonymous instance of “BackendlessCallback” class. But it can be done only if “user” is final - thats why you receive your exception.

You should declare it final, or avoid using it inside inner classes.

ok, thanks. I made it final.