Null object reference JAVA

0

The problem is all happening in a search, when I click and start typing the field it will filter the results, when I click on one of these results to open another screen, this error:

  

Attempt to invoke virtual method 'void android.widget.TextView.setText (java.lang.CharSequence)' on a null object reference

The search code looks like this:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_restaurante, menu);

    MenuItem searchItem = menu.findItem(R.id.action_search);

    SearchManager searchManager = (SearchManager) getActivity()
            .getSystemService(Context.SEARCH_SERVICE);

    searchView = null;
    if (searchItem != null) {
        searchView = (SearchView) searchItem.getActionView();

        if(restaurantes == null || restaurantes.size() == 0){
            searchView.setEnabled(false);
        }
    }

    if (searchView != null) {
        searchView.setSearchableInfo(searchManager
                .getSearchableInfo(getActivity().getComponentName()));

        searchView.setOnQueryTextListener(new OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String arg0) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String arg0) {
                filtrarRestaurante(arg0);
                return false;
            }
        });
    }
    //searchView.setVisibility(View.INVISIBLE);
    super.onCreateOptionsMenu(menu, inflater);
}

The function filtarRestaurante(agr0) is this:

public void filtrarRestaurante(String filtro){
 ArrayList<Restaurante> restaurantesEncontrados = new ArrayList<Restaurante>();

    if(filtro.length() == 0 && restaurantes != null){
        restaurantes.clear();
        restaurantes.addAll(restaurantesBkp);
        configurarAdapter();
        return;
    }

    // Procura pelo restaurante
    restaurantes.clear();
    restaurantes.addAll(restaurantesBkp);
    for(Restaurante restaurante : restaurantes){
        if(restaurante.getNomeRestaurante().toLowerCase().contains(filtro.toLowerCase())){
            restaurantesEncontrados.add(restaurante);
        }
    }
    restaurantes = restaurantesEncontrados;
    configurarAdapter();
}

Here is the init of my fragment:

    @AfterViews
public void init() {
    Globales.setPedido(new Pedido());

    restaurante = (Restaurante) getArguments().getSerializable(RESTAURANTE_CARDAPIO);
    Globales.setRestauranteAtual(restaurante);
    carregarCardapio(restaurante);
}

So I instantiate the restaurant:

Restaurante restaurante;

This error is happening when I click on the restaurant and the error happens here:

@Override
public void onResume() {
    super.onResume();
    try{
        updateCarrinho();
        Globales.setListaOpcionais(null);

        TextView toolbarTitle = (TextView) getActivity().findViewById(
                R.id.toolbar_title);
        toolbarTitle.setText(restaurante.getNomeRestaurante());
    }catch (Exception e){
        MessageUtil.showError(getActivity(), e.getMessage());
    }

}

Exactly on this line

  

toolbarTitle.setText (restaurant.getRestaurantName ());

When I go to name the restaurant the exception, why would I give it?

  • I've noticed that restaurants sometimes double in search results.
  • The cool thing is that this happens only when I look up the restaurant name, otherwise it comes in normally, so if I search for it and re-enter the problem.

    ATTACHMENTS:

Photo of debug in restaurant part = (Restaurant) getArguments (). getSerializable (RESTAURANT_CARDAPIO);

    
asked by anonymous 08.06.2017 / 14:03

2 answers

1

Renan,

The problem is here:

TextView toolbarTitle = (TextView) getActivity().findViewById(R.id.toolbar_title);

When you call getActivity() , you're telling him to go look in the Activity layout that called his fragment.

When you declare the layout of the Fragment, you call there in the onCreateView, for example:

View view = inflater.inflate(R.layout.fragment, container, false);

This view you should call to reference a layout item, in your case it would look like this:

TextView toolbarTitle = (TextView) view.findViewById(R.id.toolbar_title);
    
08.06.2017 / 14:34
0

The error occurs because the findViewById () of this line:

TextView toolbarTitle = (TextView) getActivity().findViewById(R.id.toolbar_title);

is returning null, that is, did not find a view with this id and with that the crashea app because you are trying to set the text in something that does not exist.

As you have not posted the code I will give you some kicks, since it appears to be running inside a Fragment (by the getActivity you are calling):

  • If your TextView with the "toolbar_title" attribute is in the Activity layout, check if the name of the TextView is beating correctly.

  • If your TextView with the "toolbar_title" attribute is in the Fragment layout, check if the name of the TextView is beating correctly, create a global variable in Fragment, and use findViewById () in the OnCreateView method ) of Fragment.

Example:

private TextView mToolBarTitle;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_article_detail, container, false);
    mToolbarTitle = (TextView) rootView.findViewById(R.id.toolbar_title);

    ...

    return rootView;
}

Finally, seven text using this global variable in your method:

try{
    updateCarrinho();
    Globales.setListaOpcionais(null);

    mToolbarTitle.setText(restaurante.getNomeRestaurante());
}catch (Exception e){
    MessageUtil.showError(getActivity(), e.getMessage());
}
    
08.06.2017 / 16:00