Clear calls:
undoStack.Clear();
redoStack.Clear();
So, these objects must not have been created at the point you are trying to call Clear. You can force their creation by just referencing their public property analogs, as they are created on demand as your access the public properties, UndoStack and RedoStack.
So, this code may work.
if(CommandStack.UndoStack.Count > 0 ||
CommandStack.RedoStack.Count > 0)
CommandStack.Clear();