Thread: Autonomous Help
View Single Post
  #67   Spotlight this post!  
Unread 23-01-2015, 13:03
curtis0gj curtis0gj is offline
Registered User
FRC #5033 (Beavertronics)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2015
Location: Canada
Posts: 121
curtis0gj will become famous soon enough
Re: Autonomous Help

Quote:
Originally Posted by notmattlythgoe View Post
A NullPointerException happens when you try to access an object like an encoder, but the object has never been given an initial value.

So what seems to have been happening in your old code was because you had that Encoder part on the one line you were declaring a new local variable called encoder which was only used locally, and when robotInit ended it was removed from memory. What this means is that the field you called encoder was never given a value. Take a look at the old code, I'm going to bold the local variable and underline the field so you can see what I'm talking about.

Code:
package org.usfirst.frc.team5033.robot;

import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.Gyro;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.CounterBase.EncodingType;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

public class Robot extends IterativeRobot {
	
	RobotDrive robot;
    Joystick stick;
    Encoder encoder;	
    Gyro gyro1;
    
    static final double Kp = 0.05;
    
    public void robotInit() {
    	
    	 robot = new RobotDrive(0, 1);
         stick = new Joystick(0);
         gyro1 = new Gyro(0);
         gyro1.reset();
         gyro1.initGyro();
		 
         Encoder encoder = new Encoder(0, 1, false, EncodingType.k4X);
         encoder.setDistancePerPulse(0.001);
         encoder.getDistance();       
    }
   
    public void autonomousPeriodic() {

    	gyro1.reset();
		
    	while(isAutonomous() && isEnabled())  {
    		
    		double angle = gyro1.getAngle();
    		double distance = encoder.getDistance();
    		
    		SmartDashboard.putNumber("Angle", angle);
    		SmartDashboard.putNumber("Distance", distance);
    		
    		robot.drive(-0.25, 1);
    		
    		if(angle > 150) {
    			robot.drive(0,0);
    			gyro1.reset();
    			encoder.reset();
    			
    			Timer.delay(0.5);

    			break; 
    			
    			}
    		
    			Timer.delay(0.1);
    	}
    	
		while (isAutonomous() && isEnabled()) {

			double angle2 = gyro1.getAngle(); 
			double distance2 = encoder.getDistance();
			
			SmartDashboard.putNumber("Distance", distance2);
			
			robot.drive(-0.20, -angle2 * Kp); 
		
			if(encoder.getDistance() < 10)  {
				robot.drive(0,0); 
				break;
			}
			Timer.delay(0.1);	
		}
	}
    

    public void teleopPeriodic() {
    	
    	while (isOperatorControl() && isEnabled()) {
        	
        	SmartDashboard.putNumber("angle", gyro1.getAngle());
        	
        	stick.getThrottle();
            robot.arcadeDrive(stick.getY(), -stick.getX(), false);
            
            Timer.delay(0.1);
    	}
        
    }
    
    
    public void testPeriodic() {
    
    }
    
}
Do you see how the underlined one never has the statement new Encoder(...) called for it? So it was never given an initial value.

Still not getting anything printed to my smartdash board do I need to make a new dashboard or something like that.
Reply With Quote