Thread: Autonomous Help
View Single Post
  #66   Spotlight this post!  
Unread 23-01-2015, 12:57
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is online now
Flywheel Police
AKA: Matthew Lythgoe
FRC #2363 (Triple Helix)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: Newport News, VA
Posts: 1,728
notmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond repute
Re: Autonomous Help

Quote:
Originally Posted by curtis0gj View Post
Hmm I've no idea what NullPointerExceptions but I don't think want any they sound pretty bad.
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.
Reply With Quote