List<> of classes Object ref. exeption

I am trying to save game data in multiple instances of a class: but I get an object reference exeption on this:

data.CarpartsList[CurrentCar].currBodykit=currBodykit;

Where “data” is the serializable save data class, “CarpartsList” supposed to be a the list of instances of the serializable class called “CarParts” that contains a bunch of integers.

/// Saving and loading game data
    
    	public void Save(){
    		
    		
    	BinaryFormatter bf= new BinaryFormatter();
    	FileStream file = File.Create(Application.persistentDataPath+"/"+profileName+".dat");
    	
    	ProfileData data= new ProfileData();
    		
    		data.currentCar=CurrentCar;
    		data.transmissionMode=TransmissionMode;
    		data.speedometerUnit=SpeedometerUnit;
    		
    		
data.CarpartsList[CurrentCar].currBodykit=currBodykit;
data.CarpartsList[CurrentCar].currRim=currRim;	data.CarpartsList[CurrentCar].currSpoiler=currSpoiler;	data.CarpartsList[CurrentCar].currPaint=currPaint;	data.CarpartsList[CurrentCar].currNeon=currNeon;	data.CarpartsList[CurrentCar].currVinyl=currVinyl;	data.CarpartsList[CurrentCar].selectedColdAirIntake=selectedColdAirIntake;	data.CarpartsList[CurrentCar].selectedExhaustHeaders=selectedExhaustHeaders;	data.CarpartsList[CurrentCar].selectedFuelInjectors=selectedFuelInjectors;	data.CarpartsList[CurrentCar].selectedTurboKit=selectedTurboKit;	data.CarpartsList[CurrentCar].selectedGearbox=selectedGearbox;
    		
    	bf.Serialize(file,data);
    	file.Close();	
    	}
    
    [Serializable] public class CarParts
    {
    public int currBodykit;
    public int currSpoiler;
    public int currRim;
    public int currNeon;
    public int currPaint;
    public int currVinyl;
    public int selectedColdAirIntake;
    public int  selectedExhaustHeaders;
    public int  selectedCamshaftsAndCamgears;
    public int  selectedExhaustSystem;
    public int  selectedHighflowIntakeManifold;
    public int  selectedLargerDownpipe;
    
    // Etc...	
    }
    
    [Serializable] public class ProfileData
    {
    	public List<CarParts> CarpartsList= new List<CarParts>( new CarParts[3] );
    	public int currentCar;
    	public int CareerProgress;
    	public int Cash;
    	public int transmissionMode;
    	public int speedometerUnit;
    }

I guess you are trying to access a null reference on your list. See, when you call: ProfileData data= new ProfileData();

You create an instance of ProfileData, but the list itself has no values assigned to it. So you have a list of empty values. When you call data.CarpartsList[CurrentCar].currBodykit=currBodykit;

You are trying to access the property CarpartsList, which is a list with null values. So CarpartsList[CurrentCarr] equals null, and has no currBodykit property.

In order to correct this, first you need to create new instances of CarParts and add to your list. Then you can access them and change values.