Announcement

Collapse
No announcement yet.

Attempt to read from... on a null object reference

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Attempt to read from... on a null object reference


    I'm trying to get the joystick positions from another class after having them get them from the joystick.

    I can't find any problems in android studio, but when I run the code on the phones, I get
    Error: User code threw an uncaught exception: NullPointerException - Attempt to read from field 'float com.qualcomm.robotcore.hardware.Gamepad.left_stick _y' on a null object reference





    Here's the joystickcalc class:
    package org.firstinspires.ftc.teamcode; import com.qualcomm.robotcore.hardware.DcMotor; import com.qualcomm.robotcore.hardware.HardwareMap; import com.qualcomm.robotcore.hardware.Servo; import com.qualcomm.robotcore.util.ElapsedTime; import org.firstinspires.ftc.robotcore.external.Telemetry ; import com.qualcomm.robotcore.hardware.DcMotor; import com.qualcomm.robotcore.hardware.Servo; import com.qualcomm.robotcore.hardware.TouchSensor; import com.qualcomm.robotcore.util.ElapsedTime; import com.qualcomm.robotcore.hardware.DigitalChannel; import com.qualcomm.robotcore.eventloop.opmode.Disabled; import com.qualcomm.robotcore.eventloop.opmode.LinearOpMo de; import com.qualcomm.robotcore.eventloop.opmode.TeleOp; import com.qualcomm.robotcore.hardware.DigitalChannel; 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.Servo; import com.qualcomm.robotcore.hardware.TouchSensor; import com.qualcomm.robotcore.util.ElapsedTime; import com.qualcomm.robotcore.hardware.DigitalChannel; import com.qualcomm.robotcore.eventloop.opmode.Disabled; import com.qualcomm.robotcore.eventloop.opmode.LinearOpMo de; import com.qualcomm.robotcore.eventloop.opmode.TeleOp; import com.qualcomm.robotcore.hardware.DigitalChannel; //@Disabled /** * Formatting stolen from Matthew, which was originally created on 9/20/2018. * Created by Isaac on 8/20/2019. */ public class joystickcalc extends OpMode{ private ElapsedTime runtime = new ElapsedTime(); double leftStickY; double leftStickX; double rightStickX; double rightStickY; boolean xButton; boolean yButton; boolean aButton; boolean bButton; private LinearOpMode lin_opmode; @Override public void init() { } public void loop() { } public void calculate () { leftStickY = gamepad1.left_stick_y; leftStickX = gamepad1.left_stick_x; rightStickX = gamepad1.right_stick_x; rightStickY = gamepad1.right_stick_y; xButton = gamepad1.x; yButton = gamepad1.y; bButton = gamepad1.b; aButton = gamepad1.a; } }

    Here

  • #2
    Sorry for the bad formatting, here's pastebin links to the code:
    joystickcalc: https://pastebin.com/raw/dCyBaJNj
    UDCTest: https://pastebin.com/raw/SyGb3Fna

    Comment


    • #3
      Give this a try.

      JoyStickCalc.java
      Code:
      package org.firstinspires.ftc.teamcode;
      
      import com.qualcomm.robotcore.eventloop.opmode.OpMode;
      
      public class JoystickCalc
      {
          private OpMode opmode;
      
          double leftStickY;
          double leftStickX;
          double rightStickX;
          double rightStickY;
          boolean xButton;
          boolean yButton;
          boolean aButton;
          boolean bButton;
      
          public JoystickCalc(OpMode opmode)
          {
              this.opmode = opmode;
          }
      
          public void calculate ()
          {
              leftStickY = opmode.gamepad1.left_stick_y;
              leftStickX = opmode.gamepad1.left_stick_x;
              rightStickX = opmode.gamepad1.right_stick_x;
              rightStickY = opmode.gamepad1.right_stick_y;
              xButton = opmode.gamepad1.x;
              yButton = opmode.gamepad1.y;
              bButton = opmode.gamepad1.b;
              aButton = opmode.gamepad1.a;
          }
      }
      UDCTest.java

      Code:
      package org.firstinspires.ftc.teamcode;
      
      import com.qualcomm.robotcore.eventloop.opmode.OpMode;
      import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
      
      @TeleOp
      public class UDCTest extends OpMode
      {
          JoystickCalc joystickCalc;
          double test1;
      
          @Override
          public void init()
          {
              joystickCalc = new JoystickCalc(this);
          }
      
          @Override
          public void loop() {
      
              joystickCalc.calculate();
              test1 = joystickCalc.leftStickX;
              telemetry.addData("X", test1);
              telemetry.update();
          }
      }

      Comment


      • #4
        Thank you so much, it works now!

        Comment

        Working...
        X