Change value of getAttribute when null

I am creating a document that contains the selected answers from a quiz.

I am running into trouble when some questions are skipped based on previous answers. When this occurs, I receive,

Uncaught TypeError: Cannot read properties of null (reading
‘getAttribute’)

How can I amend my code to pass a value of “N/A” when the field is skipped? I have tried the following with no avail.

var oldValue = document.querySelector(".quiz_selected_answer input[name='question-2']").getAttribute('data-value');
            var newValue; 
            if (!oldValue) { 
                newValue =  
                "N/A"; 
            } else { 
                newValue = oldValue; 
            }

Thank you community for your help. It is appreciated.