code: private static String getStringValueFromRow(QuerySolution resultRow, String variableName) {
          // Check the input and exit immediately if null
          if (resultRow == null) {
              return null;
          }
  
          String result = null;
  
          Resource res = resultRow.getResource(variableName);
          if (res != null && res.isLiteral()) {
              result = res.asLiteral().getString();
          }
  
          return result;
      }
docs: Given a query result from a SPARQL query, obtain the given variable value
  as a String
  
  @param resultRow    the result from a SPARQL query
  @param variableName the name of the variable to obtain
  @return the value or null if it could not be found