title: Retrieve all Managed Devices using Java (with Microsoft Graph API SDK)
body: <p>I would like to retrieve all devices managed by Intune (managed devices) using the Microsoft Graph Java SDK. I have created the app in Microsoft Azure and given the appropriate API permissions:</p>
  <p><a href="https://i.stack.imgur.com/i17BO.png" rel="nofollow noreferrer">API Permissions</a></p>
  <p>The following code creates a graphClient object and a method that retrieves all managed devices.</p>
  <pre><code>    @Service
  public class AzureServiceDefault implements AzureService
      {
          private static final String CLIENT_ID = &quot;XXXXXXXXXXXXXXXXXXXXXXXX&quot;;
          private static final List&lt;String&gt; SCOPES = Arrays.asList(new String[]{&quot;https://graph.microsoft.com/.default&quot;});
          private static final String TENANT = &quot;XXXXXXXXXXXXXXXXXXXXXXXX&quot;;
          private static final String CLIENT_SECRET = &quot;XXXXXXXXXXXXXXXXXXXXXXXX&quot;;
          ClientCredentialProvider authProvider = new ClientCredentialProvider(CLIENT_ID, SCOPES, CLIENT_SECRET, TENANT, NationalCloud.Global);
          IGraphServiceClient graphClient;
      
          public AzureServiceDefault()
          {
              graphClient = GraphServiceClient.builder().authenticationProvider(authProvider).buildClient();
      
          } 
  
      @Override
          public List&lt;IntuneDevice&gt; getManagedDevices()
          {
              IManagedDeviceCollectionRequestBuilder managedDeviceRequestBuilder;
              IDeviceManagementRequestBuilder builder = graphClient.deviceManagement();
              IDeviceManagementRequest managedDevicesRequest = builder.buildRequest();
              List&lt;ManagedDevice&gt; managedDevices = new ArrayList&lt;&gt;();
              List&lt;IntuneDevice&gt; allManagedDevices = new ArrayList&lt;&gt;();
              
              do {
                  try {
                      DeviceManagement deviceManagement = managedDevicesRequest.get();
                      ManagedDeviceCollectionPage managedDevicesCollectionPage = deviceManagement.managedDevices;
                      
                      //Process items in the response
                      managedDevices.addAll(managedDevicesCollectionPage.getCurrentPage());
                      managedDevices.stream().forEach((device) -&gt; allManagedDevices.add(new IntuneDevice(device.id, 
                                                                                                 device.userId,
                                                                                                 device.deviceName,
                                                                                                 device.managedDeviceOwnerType.toString(),
                                                                                                 device.operatingSystem,
                                                                                                 device.osVersion,
                                                                                                 device.complianceState.toString(),
                                                                                                 device.azureADRegistered,
                                                                                                 device.azureADDeviceId,
                                                                                                 device.userPrincipalName,
                                                                                                 device.model,
                                                                                                 device.manufacturer,
                                                                                                 device.serialNumber)));
                  
                  
                  
                      //Build the request for the next page, if there is one
                      managedDeviceRequestBuilder = managedDevicesCollectionPage.getNextPage();
                      if (managedDeviceRequestBuilder == null)
                      {
                          managedDevicesRequest = null;
                      }
                      else
                      {
                          managedDevicesRequest = (IDeviceManagementRequest) managedDeviceRequestBuilder.buildRequest();
                      }
                  }
                  catch(ClientException ex)
                  {
                      ex.printStackTrace();
                      managedDevicesRequest = null;
                  }
      
              } while (managedDevicesRequest != null);
              
              return allManagedDevices;
   
  
             }
      }
  </code></pre>
  <p>The problem is that the variable managedDevices turns out to be null and this is the error message:</p>
  <pre><code>SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/] threw exception [Request processing failed; nested exception is java.lang.NullPointerException: Cannot invoke &quot;com.microsoft.graph.requests.extensions.ManagedDeviceCollectionPage.getCurrentPage()&quot; because &quot;managedDevicesCollectionPage&quot; is null] with root cause
  java.lang.NullPointerException: Cannot invoke &quot;com.microsoft.graph.requests.extensions.ManagedDeviceCollectionPage.getCurrentPage()&quot; because &quot;managedDevicesCollectionPage&quot; is null
  </code></pre>
  <p>What do I need to change to make this code work? I am succesfully able to retrieve all users in Azure AD, but I am having difficulties getting data from Intune/Endpoint Manager. Do I need to make changes to the SCOPES?</p>
  <p>It should be possible to retrieve all managed devices as the REST API for it is <a href="https://graph.microsoft.com/v1.0/deviceManagement/managedDevices" rel="nofollow noreferrer">https://graph.microsoft.com/v1.0/deviceManagement/managedDevices</a></p>
  <p>Thanks for your help</p>