-1

I'm new to java and android

MainActivity retrieves the ID of the item from my list and I would like to send it to a class to create actions but this error appears:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference

thank you

ShowContact :

public class ShowContactActivity extends AppCompatActivity {
    private ContactDbAdapter db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_contact);

        Intent intent = getIntent();
        final Long id = intent.getExtras().getLong("Id");


        final ActionsContacts actions = new ActionsContacts(id);

        FloatingActionButton floatTelephone = findViewById(R.id.floatTelephone);
        floatTelephone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                actions.call();
            }
        });


        fillData(id);
    }

ActionsContact :

`public class ActionsContacts extends AppCompatActivity{

private ContactDbAdapter db;

private String SelectedTel,
        SelectedEmail,
        SelectedAdresse,
        SelectedComplement,
        SelectedCodePostale,
        SelectedVille;

ActionsContacts(long id){


    db = new ContactDbAdapter(this);
    db.open();

    /**
     * Récupération des données en bdd car non affichées dans la ligne du listView
     */
    //Intent intent = getIntent();
    //final Long id = intent.getExtras().getLong("Id");

    Cursor c = db.fetchContact(id);
    startManagingCursor(c);



    this.SelectedTel = c.getString(c.getColumnIndex("new_telephone"));
    this.SelectedEmail = c.getString(c.getColumnIndex("new_email"));

    this.SelectedAdresse = c.getString(c.getColumnIndex("new_adresse"));
    this.SelectedComplement = c.getString(c.getColumnIndex("new_complement"));
    this.SelectedCodePostale = c.getString(c.getColumnIndex("new_codepostale"));
    this.SelectedVille = c.getString(c.getColumnIndex("new_ville"));
}

public void call(){
    Uri number = Uri.parse("tel:" + SelectedTel);
    Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
    startActivity(callIntent);
}`

MainActivity :

   final ListView list_view_contacts = (ListView) findViewById(R.id.list_view_contacts);

    db = new ContactDbAdapter(this);
    db.open();

    fillData();

    registerForContextMenu(list_view_contacts);


    list_view_contacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            /**
             * permet de faire passer l'ID de cette activité (MainActivity) vers l'activité ShowContactActivity
             */
            Intent i = new Intent(MainActivity.this, ShowContactActivity.class);
            i.putExtra("Id", id);
            startActivity(i);

        }
    });

}



/**
 * Redirection vers l'activité : CreateContact
 */
public void openActivityCreateContact(){
    Intent intent = new Intent(this, CreateContactActivity.class);
    startActivity(intent);
}



@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    /** appeller sms email localiser supprimer */
    /**
     * Menu contextuel qui seront affichés lors d'un appui long
     */
    menu.add(0, v.getId(), 0, "Appeler");
    menu.add(0, v.getId(), 0, "Envoyer un SMS");
    menu.add(0, v.getId(), 0, "Envoyer un Email");
    menu.add(0, v.getId(), 0, "Voir l'adresse du contact");
    menu.add(0, v.getId(), 0, "Supprimer le contact");
}

@Override
public boolean onContextItemSelected(MenuItem item) {

    /**
     * Récupération de l'ID en fonction de la position de la ligne dans la listView
     */
    final ListView list_view_contacts = (ListView) findViewById(R.id.list_view_contacts);

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    Cursor SelectedCursor = (Cursor) list_view_contacts.getItemAtPosition(info.position);

    final long id = SelectedCursor.getLong(SelectedCursor.getColumnIndex("_id"));


    /**
     * Actions menu contextuel
     */

    ActionsContacts actions = new ActionsContacts(id);

    if (item.getTitle() == "Supprimer le contact"){
        alertDelete(id);
        fillData();
    }

    if (item.getTitle() == "Appeler"){
        actions.call();
    }

    if (item.getTitle() == "Envoyer un SMS"){
        actions.message();
    }

    if (item.getTitle() == "Envoyer un Email"){
        actions.email();
    }

    if (item.getTitle() == "Voir l'adresse du contact"){
        actions.map();
    }
    return true;
}
459MR
  • 1
  • 2

1 Answers1

0

If you get the crash error after clicking at item list, try to get the id at your ShowContact for something like below:

final Long id = getIntent().getExtras().getLong("Id");

or maybe:

Bundle bun = getIntent();
final Long id = bun.getExtras().getLong("Id");
GusCorreia
  • 75
  • 7
  • Give a feedback if it works please :) – GusCorreia Feb 05 '20 at 11:14
  • I can't use this to send to my ActionsContact class? I want to use the functions of ActionsContact in MainActivity AND in ShowContact but for ShowContact is an activity which receives the ID from MainActivity as long as said ActionsContact (simple class) does not receive and returns this error to me ... – 459MR Feb 05 '20 at 12:32