Need help with Local variables

So I am trying to make several different names for DcMotors, and Android Studio is telling me to make a Local Variable.

Here is the code:

package org.firstinspires.ftc.teamcode;

import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.Gamepad;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.util.ElapsedTime;
import com.qualcomm.robotcore.util.Range;

@TeleOp(name=“HyperOpMode_Iterative”, group=“Iterative Opmode”)
@Disabled
public class HyperOpMode_Iterative extends OpMode
{

// Declare OpMode members.
private ElapsedTime runtime = new ElapsedTime();
private DcMotor ForeArm = null;
private DcMotor UpperArm = null;
private DcMotor LeftWheel = null;
private DcMotor RightWheel = null

The original code would have only two ‘private DcMotor’ variables called left wheel and right wheel.

I’m not sure if I need to do a ‘public class ForeArm () {}’ thing before the ‘public class HyperOpMode_Iterative extends OpMode {’. If anyone could help, that would be great.

Hello!

Consider posting the entire class, and putting the code between the

 tags.  That will allow you to post everything without taking up a lot of space.  It will also allow us to see that line that Android Studio is complaining about, and make recommendations from there.

A 'local' variable is a somewhat ambiguous term, in that it may mean local scope to the method you're trying to use it in, or local scope to the class.  The DcMotor variables you've listed above are scoped to the instance of the class, which is also known as a member variable.

To make a 'local' variable, you'd have to declare a DcMotor inside a method.  Yet since creating something like a new DcMotor object for every iterative cycle is a bad idea (you'd much rather use the one you've already created), we need the rest of the code to understand what the problem actually is.

Okay as mentioned we need to see all of your code to help you properly, but I am guessing that you mis-typed the name of one of your field variables, and Android Studio, not seeing that, considers it undefined and is asking you to create a local variable.

You definitely do not need to create a new class named ForeArm. Post all of the code and we can help pinpoint the issue.

Sorry it took a bit:

@TeleOp(name=“HyperOpMode_Iterative”, group=“Iterative Opmode”)
@Disabled
public class HyperOpMode_Iterative extends OpMode
{

// Declare OpMode members.
private ElapsedTime runtime = new ElapsedTime();
private DcMotor ForeArm = null;
private DcMotor UpperArm = null;
private DcMotor LeftWheel = null;
private DcMotor RightWheel = null;
private Servo 

@Override
public void init() {
    telemetry.addData("Status", "Initialized");

   
    // step (using the FTC Robot Controller app on the phone).
    ForeArm  = hardwareMap.get(DcMotor.class, "ForeArm");
    UpperArm = hardwareMap.get(DcMotor.class, "UpperArm");
    LeftWheel = hardwareMap.get(DcMotor.class, "LeftWheel");
    RightWheel = hardwareMap.get(DcMotor.class, "RightWheel");
   
    LeftWheel.setDirection(DcMotor.Direction.FORWARD);
    RightWheel.setDirection(DcMotor.Direction.FORWARD);

    
    telemetry.addData("Status", "Initialized");
}

You appear to have a mangled Servo declaration (no name).

Aside from that, the original problem is not a problem at all.

Because you are only using the DcMotor objects in init(), Android Studio detects that you could optimize your code by declaring them within init(), instead of outside it. This is not necessary, and the hint will go away if you use them elsewhere.

This inspection searches for redundant class fields that can be replaced with local variables. If all local usages of a field are preceded by assignments to that field, the field can be removed and its usages replaced with local variables.