Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Preferences table Null Pointer Exception (http://www.chiefdelphi.com/forums/showthread.php?t=135982)

L1TH1UM 21-03-2015 14:49

Preferences table Null Pointer Exception
 
Hello,

We are having some issues using the preferences table utility on SmartDashboard. We are supposed to be getting a table from which we can edit robot preferences such as potentiometer setpoints and encoder setpoints.

However, when we run the code it throws a Null Pointer Exception because we declared the preferences varable (Preferences prefs;) but we didn't create and object and we are trying to use an operator (prefs.getDouble). Using "prefs = new Preferences();" does not work, and there's nothing on the wpilibj documentation that tells us how to fix this (create an object for preferences).

Here's the page we're referencing: https://wpilib.screenstepslive.com/s...smartdashboard

Our code is almost exactly the same as that just minor differences such as names and values. We are programming in Java.

Any help would be appreciated, thanks.

RufflesRidge 21-03-2015 15:27

Re: Preferences table Null Pointer Exception
 
The sample on that page seems to be missing a line:

prefs = Preferences.getInstance();

Arhowk 21-03-2015 21:33

Re: Preferences table Null Pointer Exception
 
Quote:

Originally Posted by RufflesRidge (Post 1460343)
The sample on that page seems to be missing a line:

prefs = Preferences.getInstance();

You're right! Thats quite funny. Here's the actual code for reference

Code:

package org.usfirst.frc.team40.robot;

import edu.wpi.first.wpilibj.SampleRobot;
import edu.wpi.first.wpilibj.Preferences;

public class Robot extends SampleRobot{

    Preferences prefs;
    double armUpPosition;
    double armDownPosition;
    public void robotInit(){
        prefs = Preferences.getInstance();
        armUpPosition = prefs.getDouble("ArmUpPosition", 1.0);
        armDownPosition = prefs.getDouble("ArmDownPosition", 4.0);
    }
}

On a side note, to follow NetworkTable conventions the method name should be "getNumber" but that is trivial.

L1TH1UM 22-03-2015 00:05

Re: Preferences table Null Pointer Exception
 
Thank you, I appreciate it. This fixed our problem.

MrRoboSteve 22-03-2015 10:57

Re: Preferences table Null Pointer Exception
 
If you didn't do so already, please use the form on the page to report the error.


All times are GMT -5. The time now is 01:49.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi