Nullpointerexception с использованием ViewModel и MutableLiveData

Я сталкиваюсь с проблемами в моем NullPointerException как в моем ViewModel, так и с моим классом репозитория.

открытый класс MeuViewModel extends ViewModel { private MutableLiveData> itensMutableLiveData = new MutableLiveData<>(); private MutableLiveData> mercadoriasMutableLiveData = new MutableLiveData<>(); Репозиторий rep = Repository.getInstance();

public MutableLiveData<List<Item>> getItensMutableLiveData() {
    return itensMutableLiveData;
}

public void salvarItem(String codigo, String descricao, float preco, int quantidade) {
    Item item = new Item(codigo, descricao, preco, quantidade);
    salvarItem(item);
}

public void salvarMercadoria(String codigo, String descricao, float preco) {
    Mercadoria mercadoria=new Mercadoria(codigo,descricao,preco);
    salvarMercadoria(mercadoria);
}


public void salvarItem(Item item) {
    MutableLiveData<Item> itemMutableLiveData = new MutableLiveData<>();
    itemMutableLiveData.setValue(item);
    salvarItem(itemMutableLiveData);
}

public void salvarMercadoria(Mercadoria mercadoria) {
    MutableLiveData<Mercadoria> mercadoriaMutableLiveData = new MutableLiveData<>();
    mercadoriaMutableLiveData.setValue(mercadoria);
    salvarMercddoria(mercadoriaMutableLiveData);
}

public void salvarItem(MutableLiveData<Item> itemMutableLiveData) {
    rep.salvarItem(itemMutableLiveData);
}

public void salvarMercddoria(MutableLiveData<Mercadoria> mercadoriaMutableLiveData) {
    rep.salvarMercadoria(mercadoriaMutableLiveData);
}

public void deletarItem(MutableLiveData<Item> itemMutableLiveData) {
    rep.deletarItem(itemMutableLiveData);
}

public void deletarItem(Item item) {
    MutableLiveData<Item> itemMutableLiveData = new MutableLiveData<>();
    itemMutableLiveData.setValue(item);
    deletarItem(itemMutableLiveData);
}

public void deletarItem(String codigo){
    Item item=new Item();
    item.setCodigo(codigo);
    deletarItem(item);
}

public void deletarMercadoria(MutableLiveData<Mercadoria> mercadoriaMutableLiveData){
    rep.deletarMercadoria(mercadoriaMutableLiveData);
}

public void deletarMercadoria(Mercadoria mercadoria){
    MutableLiveData<Mercadoria> mercadoriaMutableLiveData=new MutableLiveData<>();
    mercadoriaMutableLiveData.setValue(mercadoria);
    rep.deletarMercadoria(mercadoriaMutableLiveData);
}

public void deletarMercadoria(String codigo){
    Mercadoria mercadoria=new Mercadoria();
    mercadoria.setCodigo(codigo);
    deletarMercadoria(mercadoria);
}

public void setItensMutableLiveData(MutableLiveData<List<Item>> itensMutableLiveData) {
    this.itensMutableLiveData = itensMutableLiveData;
    rep.setItensMutableLiveData(itensMutableLiveData);
}

public MutableLiveData<List<Mercadoria>> getMercadoriasMutableLiveData() {
    return mercadoriasMutableLiveData;
}

public void setMercadoriasMutableLiveData(){
    this.mercadoriasMutableLiveData= (MutableLiveData<List<Mercadoria>>) rep.getMercadoriasMutableLiveData();
}

public void setItensMutableLiveData(){
    this.itensMutableLiveData= (MutableLiveData<List<Item>>) rep.getItensMutableLiveData();
}

public void setMercadoriasMutableLiveData(MutableLiveData<List<Mercadoria>> mercadoriasMutableLiveData) {
    this.mercadoriasMutableLiveData = mercadoriasMutableLiveData;
    rep.setMercadoriasMutableLiveData(mercadoriasMutableLiveData);
}



public class Repository implements DAO{
    private MutableLiveData<Mercadoria> mercadoriaMutableLiveData;
    private MutableLiveData<List<Mercadoria>> mercadoriasMutableLiveData;
    private MutableLiveData<Item> itemMutableLiveData;
    private MutableLiveData<List<Item>> itensMutableLiveData;
    FirebaseFirestore database;
    private String colecao[];
    static Repository repository;
    DocumentReference reference;

    private Repository(){
        database=FirebaseFirestore.getInstance();
        colecao=new String[2];
        colecao[0]="mercadorias";
        colecao[1]="itens";

    }

    public static Repository getInstance(){
        if(repository==null)
            repository=new Repository();
        return repository;
    }




    public LiveData<Mercadoria> getMercadoriaMutableLiveData() {
        return mercadoriaMutableLiveData;
    }

    public void setMercadoriaMutableLiveData(MutableLiveData<Mercadoria> mercadoriaMutableLiveData) {
        this.mercadoriaMutableLiveData = mercadoriaMutableLiveData;
    }

    public LiveData<List<Mercadoria>> getMercadoriasMutableLiveData() {
        //busca os dados da coleção mercadorias
        database.collection(colecao[0]).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                mercadoriasMutableLiveData.setValue(task.getResult().toObjects(Mercadoria.class));
            }
        });
        return mercadoriasMutableLiveData;
    }

    public void setMercadoriasMutableLiveData(MutableLiveData<List<Mercadoria>> mercadoriasMutableLiveData) {
        this.mercadoriasMutableLiveData = mercadoriasMutableLiveData;
    }

    public LiveData<Item> getItemMutableLiveData() {
        return itemMutableLiveData;
    }

    public void setItemMutableLiveData(MutableLiveData<Item> itemMutableLiveData) {
        this.itemMutableLiveData = itemMutableLiveData;
    }

    public LiveData<List<Item>> getItensMutableLiveData() {
        //busca os dados da coleção Itens
        database.collection(colecao[1]).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                itensMutableLiveData.setValue(task.getResult().toObjects(Item.class));
            }
        });
        return itensMutableLiveData;
    }

    public void setItensMutableLiveData(MutableLiveData<List<Item>> itensMutableLiveData) {
        this.itensMutableLiveData = itensMutableLiveData;
    }

    @Override
    public void salvarMercadoria(MutableLiveData<Mercadoria> mercadoriaMutableLiveData) {
        reference=database.collection(colecao[0]).document(mercadoriaMutableLiveData.getValue().getCodigo());
        reference.set(mercadoriaMutableLiveData.getValue());
    }

    @Override
    public void salvarItem(MutableLiveData<Item> itemMutableLiveData) {
        reference=database.collection(colecao[1]).document(itemMutableLiveData.getValue().getCodigo());
        reference.set(itemMutableLiveData);
    }

    @Override
    public void deletarMercadoria(MutableLiveData<Mercadoria> mercadoriaMutableLiveData) {
        reference=database.collection(colecao[0]).document(mercadoriaMutableLiveData.getValue().getCodigo());
        reference.delete();
    }

    @Override
    public void deletarItem(MutableLiveData<Item> itemMutableLiveData) {
        reference=database.collection(colecao[1]).document(itemMutableLiveData.getValue().getCodigo());
        reference.delete();
    }

public class Ofertas extends Fragment implements FirestoreCallback {
    View v;
    static Ofertas ofertas;
    private TextView precoUnitario, precoSubtotal, quantidade, mais, menos;
    RepositoryItem repositoryItem;
    EditText editTextFiltro;
    Button buttonSalvarLista, buttonMostrarLista;
    ItemViewModel itemViewModel;
    MeuViewModel model;


    public Ofertas() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_ofertas, container, false);
        inicializa(v);
        model=ViewModelProviders.of(getActivity()).get(MeuViewModel.class);
        model.rep.getItensMutableLiveData().observe(this, new Observer<List<Item>>() {
            @Override
            public void onChanged(@Nullable List<Item> items) {
                Toast.makeText(getContext(),items.toString(),Toast.LENGTH_SHORT).show();
            }
        });

        //listar(v);
        return v;
    }

    private void inicializa(View v){
        itemViewModel=ViewModelProviders.of(getActivity()).get(ItemViewModel.class);
        editTextFiltro=v.findViewById(R.id.edit_text_filtro);


    }

    private void setValues(){
        //Item item= (Item) spinner.getSelectedItem();

    }

    public static Ofertas getInstance() {
        if (ofertas == null)
            ofertas = new Ofertas();
        return ofertas;
    }


    public static Ofertas newInstance(Oferta oferta) {
        if (ofertas == null)
            ofertas = new Ofertas();
        Bundle args = new Bundle();
        args.putSerializable("oferta", oferta);
        ofertas.setArguments(args);
        return ofertas;
    }

    @Override
    public void onStart() {
        super.onStart();
        //RepositoryItem.getInstance(getActivity()).readData(this);

    }

    @Override
    public void onCallback(MutableLiveData<List<Item>> itens) {
        //itemViewModel.setItens(itens);

    }
}

0 ответов

Другие вопросы по тегам