How do i export a subview of a fragment from another fragment as Png?

**Context**

I'am creating an app, which allows the user to create a personalized CV. The user can submit informations regarding his job-experience and skills. As a result the user is able to export his results as a png and save it to the device. My question aims at the export-functionallity of the application. 

[![enter image description here](https://i.stack.imgur.com/TfnK8.png)](https://i.stack.imgur.com/TfnK8.png)

**What i got so far**

I tried to combine several answers from the site to get a result, but unfortunately the code that i have does not work so far. 

```
public void export(Context context) throws FileNotFoundException {

    View  exportView = getLayoutInflater(getArguments()).inflate(R.layout.fragment_form, null, false);
    RelativeLayout subView = (RelativeLayout) exportView.findViewById(R.id.fragment_form_container_root);

    try {

        subView.setDrawingCacheEnabled(true);
        subView.measure(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        Bitmap b = Bitmap.createBitmap(subView.getDrawingCache(), 0, 0, subView.getMeasuredWidth(), subView.getMeasuredHeight());

        File cachePath = new File(context.getCacheDir(), "images");
        cachePath.mkdirs(); // don't forget to make the directory
        FileOutputStream stream = new FileOutputStream(cachePath + "/image.png"); // overwrites this image every time
        b.compress(Bitmap.CompressFormat.PNG, 100, stream);
        stream.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

    File imagePath = new File(context.getCacheDir(), "images");
    File newFile = new File(imagePath, "image.png");
    Uri contentUri = FileProvider.getUriForFile(context, "com.example.lukas.masterthesis.fileprovider", newFile);

    if (contentUri != null) {

        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
        shareIntent.setDataAndType(contentUri, getActivity().getContentResolver().getType(contentUri));
        shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
        startActivity(Intent.createChooser(shareIntent, "Choose an app"));

    }

}

```

The export functionallity is called within Fragment A after a click event. I'am trying to get the wanted subview of Fragment B (wich seems to work), but creating the Bitmap always results in a *null* Object. Therefore the procedure fails. 

```
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null 

```

Honestly im not even sure this is the proper way to do such a thing, since I haven't found a Best-Practice solution for what im trying. If not, does anybody know a better way to do this? 

If its an "ok" solution, does anybody know how to get my code to work or has some suggestions that can point me into the right direction? 

Thx in advance.