Android Studio error message only occurs from time to time seemingly for no reason when opening a fragment?

I have an Android Studio project where I am making a school-project Android app which transfers files between two devices, and while moving some methods and variables from my ChooseFile.java to my TransferFragment.java (i also changed the fragment’s xml layout file with the right context) all seemed to go well, until I encountered this error after launching my app once on an emulator device and opening the Transfer Fragment:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

Now the strange thing is it only happens from time to time only when I open this fragment. I have attached the TransferFragment.java code below, I tried removing everything inside the onCreateView method (except for the rootview of course) and the app would still crash from time to time with the same error message.

TransferFragment.java:

package com.example.myproject;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.OnProgressListener;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;

import java.util.Calendar;
import java.util.Date;

public class TransferFragment extends Fragment implements View.OnClickListener {
    Dialog d;
    TextView btnOk, btnSettings;
    LinearLayout wifi_direct, nfc;
    private DataPassListener mListener;
    EditText name, receiver;
    TextView upload;
    ImageView add, iv_img;
    StorageReference storageReference;
    DatabaseReference databaseReference;
    int SELECT_PICTURE = 200;
    Uri image;
    Intent intent;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_transfer, container, false);
        passData("transfer");
        databaseReference = FirebaseDatabase.getInstance().getReference("Uploads");
        storageReference = FirebaseStorage.getInstance().getReference();
        name = (rootView).findViewById(R.id.name);
        receiver = (rootView).findViewById(R.id.receiver);
        iv_img = (rootView).findViewById(R.id.image);
        upload = (rootView).findViewById(R.id.upload);
        upload.setOnClickListener(this);
        add = (rootView).findViewById(R.id.add);
        add.setOnClickListener(this);
        intent = new Intent(getActivity(), MainScreen.class);
        return rootView;
    }

    @Override
    public void onClick(View v) {
        if(v==btnOk)
        {
            d.dismiss();
        }
        if(v==btnSettings)
        {
            startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
            d.dismiss();
        }
        if(v==wifi_direct)
        {
            Intent intent = new Intent(getActivity(), ChooseFile.class);
            startActivity(intent);
            intent.putExtra("mode", "wifi");
        }
        if(v==nfc)
        {
            Intent intent = new Intent(getActivity(), ChooseFile.class);
            startActivity(intent);
            intent.putExtra("mode", "nfc");
        }
        if(v==add)
        {
            selectFiles();
        }
        if(v==upload)
        {
            uploadFiles(image);
        }

    }
    public interface DataPassListener {
        void onDataPass(String data);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        try {
            mListener = (DataPassListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() + " must implement DataPassListener");
        }
    }

    public void passData(String data) {
        if (mListener != null) {
            mListener.onDataPass(data);
        }
    }
    public void selectFiles() {
        Intent intent = new Intent();
        intent.setType("*/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select files:"), SELECT_PICTURE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==SELECT_PICTURE && resultCode==-1 && data!=null && data.getData()!=null)
        {
            image = data.getData();
            iv_img.setImageURI(image);
        }
        else
        {
            Log.e("ChooseFile", "Invalid file selection data");
        }
    }
    public void uploadFiles(Uri data) {
        final ProgressDialog progressDialog = new ProgressDialog(getActivity());
        progressDialog.setTitle("Uploading...");
        progressDialog.show();
        StorageReference reference = storageReference.child("Uploads/"+String.valueOf(name.getText()));
        if(data!=null)
        {
            reference.putFile(data).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Task<Uri> uriTask = taskSnapshot.getStorage().getDownloadUrl();
                    uriTask.addOnCompleteListener(new OnCompleteListener<Uri>() {
                        @Override
                        public void onComplete(@NonNull Task<Uri> task) {
                            if (task.isSuccessful()) {
                                Uri url = task.getResult();
                                Date currentTime = Calendar.getInstance().getTime();
                                FirebaseAuth mAuth = FirebaseAuth.getInstance();
                                FirebaseUser currentUser = mAuth.getCurrentUser();
                                Transfer transfer = new Transfer(String.valueOf(name.getText()), currentTime, currentUser.getDisplayName(), url.toString(), String.valueOf(receiver.getText()), true);
                                databaseReference.child(transfer.getName()).setValue(transfer);
                                progressDialog.dismiss();
                            } else {
                            }
                        }
                    });
                }
            }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onProgress(@NonNull UploadTask.TaskSnapshot snapshot) {
                    double progress = (100.0*snapshot.getBytesTransferred())/snapshot.getTotalByteCount();
                    progressDialog.setMessage("Uploaded "+(int)progress+"%");
                }
            });
        }
    }
}

I have never encountered something like this, and while it is not critical for my project I would still love to hear any opinions from much more experienced people who might know what could be the cause for this.

In case it matters, the Fragment is opened inside of another activity and is contained within a FrameLayout in said activity, here is the code which opens it


getSupportFragmentManager().beginTransaction().replace(R.id.container, new TransferFragment())
                    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();

Sorry in advance for my messy code.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật