0

I'm getting NPE when I try to instantiate a class, I already tried using NameOftheClass.this, getApplicationContext(), getApplication(). None of those is working, here is the log:

java.lang.NullPointerException
at br.com.FragmentClientes.<init>(FragmentClientes.java:87) 

line 87 is: mRepositorio = new Repositorio(FragmentClientes.this);

And here is the snippet:

public class FragmentClientes extends ActionBarActivity {


    private boolean searchCheck;
    private List<ClienteModel> clientes = new ArrayList<ClienteModel>();
    private ProgressBar progressBar;
    private ListView lv;
    private ClientViewAdapter ad;
    private ClientViewAdapter ads;
    private SearchView searchView;
    private LinearLayout footerLinearLayout;
    private boolean shouldExecuteOnResume;
    private Repositorio mRepositorio;

    @Override
    public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        // TODO Auto-generated method stub
        setContentView(R.layout.fragment_cliente);

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        SecurePreferences mSessao = new SecurePreferences(FragmentClientes.this, "sessao");
        mSessao.put("menuAtual", "Clientes");

        lv = (ListView) findViewById(R.id.listaClientes);

    }


    public FragmentClientes(Integer idViagem)  {

        clientes = new ArrayList<ClienteModel>();


        try {

            mRepositorio = new Repositorio(FragmentClientes.this);

            List lista = mRepositorio.getClientesViagem(idViagem);

            clientes = lista;


            ad = new ClientViewAdapter(FragmentClientes.this, this, clientes);



            lv.setVerticalFadingEdgeEnabled(true);
            lv.setVerticalScrollBarEnabled(true);


            lv.setAdapter(ad);



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


    }
}
Stack two
  • 511
  • 2
  • 11
  • 27

4 Answers4

2

Dont create Parameteriged Activity Constructor

just init your data in Activity Call back methods like onCreate(),onResume()

Start your Activity Using Intent

For more read Activity doc Activity Documnetation

kalyan pvs
  • 14,495
  • 3
  • 39
  • 57
2

Do not use an activity context inside its constructor, it will now work. Please put all the code from the constructor into a method and call it inside the `onCreate method like this:

@Override
public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    // TODO Auto-generated method stub
    setContentView(R.layout.fragment_cliente);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    SecurePreferences mSessao = new SecurePreferences(FragmentClientes.this, "sessao");
    mSessao.put("menuAtual", "Clientes");

    Integer idViagem = getIntent().getIntExtra(TAG, -1);

    init(idViagem);



}


public void init(Integer mId)  {

    clientes = new ArrayList<ClienteModel>();


    try {

        mRepositorio = new Repositorio(FragmentClientes.this);

        List lista = mRepositorio.getClientesViagem(mId);

        clientes = lista;


        ad = new ClientViewAdapter(FragmentClientes.this, this, clientes);



        lv.setVerticalFadingEdgeEnabled(true);
        lv.setVerticalScrollBarEnabled(true);


        lv.setAdapter(ad);



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


}

Also, you will need to find another way of passing the integer parameter.

For this, use Intent.putExtra(TAG, idViagem) on the intent which starts the activity and then retrieve the value in the constructor with getIntent().getIntExtra(TAG, -1)

For example if you're starting FragmentClientes activity from another activity:

Intent intent = new Intent(this, FragmentClientes.class);
intent.putExtra("idViagem", int_value_of_id_that_you_passed_through_constructor);
startActivity(intent);
kevto
  • 519
  • 6
  • 15
Kelevandos
  • 6,561
  • 24
  • 44
1

FragmentClientes is an Activity. You should not define a custom counstructor, since the method onCreate behaves as constructor and will be called automatically by the system.

so put you code into onCreate method.

Important: you should never, i mean realy never call new FragmentClientes(1);

dieter
  • 7,789
  • 5
  • 40
  • 64
0

It might be because yout ListView is not properly instantiated

private ListView lv;

You never asign (in the code you provided) any instantiation for the object.

Rafael Larios
  • 165
  • 1
  • 9