Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7026433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:07:00+00:00 2026-05-28T00:07:00+00:00

I am working on a contact list project and the last thing i need

  • 0

I am working on a contact list project and the last thing i need to do is display images for each of my contact.
In code from below i want to show my image by using the path wich is saved in my edittext1:
but i get NUll pointer exception on this line:

 String username = mpic.getText().toString(); 



        private void ShowImage()
  {
      //string username created from edit text field to a string
      EditText mpic =(EditText)findViewById(R.id.edittext1);
      String username = mpic.getText().toString(); 
        //bitmap will decode the string to a image (bitmap)
        Bitmap myBitmap = BitmapFactory.decodeFile(username);
        //Image view used to set the bitmap
        ImageView myImage = (ImageView) findViewById(R.id.image);
        //setting the image to the image view
        myImage.setImageBitmap(myBitmap);

  } 

The whole class is :

 public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main); 
     ShowImage();
      showDatabaseContent();
      lv1 = getListView();

      lv1.setTextFilterEnabled(true);

       lv1.setOnItemClickListener(new OnItemClickListener() {

       public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
           cursor = (Cursor) a.getItemAtPosition(position);
           itemId = cursor.getString(6);
           openOptionsMenu();
           }
       });

       lv1.setOnItemSelectedListener(new OnItemSelectedListener() {

           public void onItemSelected(AdapterView<?> parent, View view, int position, long id){

        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }

       });
   }

   //selected item index from ListView
   public void showDialogItemId(long itemId){
       Toast.makeText(this, "Menu item selected index is" + Long.toString(itemId), Toast.LENGTH_LONG).show();
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu){
       MenuInflater inflater = getMenuInflater();
       inflater.inflate(R.menu.menu, menu);
       return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item){
       switch (item.getItemId()){
           case R.id.modifyitem:
               if(null != itemId){
                   Bundle contactToModify = new Bundle();
                   contactToModify.putString("cFirstName", cursor.getString(0));
                   contactToModify.putString("cMobilePhone", cursor.getString(5));
                   contactToModify.putString("cEmail", cursor.getString(2));
                   contactToModify.putString("curl", cursor.getString(3));
                   contactToModify.putString("cAdress", cursor.getString(4));
                  contactToModify.putString ("cphoto", cursor.getString(1));
                   contactToModify.putString("mod_type", "modifyPerson");
                   Intent intent = new Intent(this, ContactDetails.class);
                   intent.setClass(this, ContactDetails.class);
                   intent.putExtras(contactToModify);
                   startActivityForResult(intent, CONTACT_MODIFIED);
               }else{
                   Toast.makeText(this, "Select Contact to modify", Toast.LENGTH_LONG).show();
               }
               break;
           case R.id.additem:

               Intent i = new Intent(this, ContactDetails.class);
               Bundle bun = new Bundle();
               bun.putString("mod_type", "addPerson");
               i.setClass(this, ContactDetails.class);
               i.putExtras(bun);
               startActivityForResult(i, CONTACT_ADDED);
               break;

           case R.id.removeitem:
               if(null != itemId){
                   removeContact(itemId);
                   showDatabaseContent();
               }
               else{
                   Toast.makeText(this, "Select Contact to delete", Toast.LENGTH_LONG).show();
               }
               break;
           case R.id.search:
               Intent j =new Intent(this,Search.class);
               j.setClass(this, Search.class);
               startActivity(j);
                break;

       }
       return true;
   }

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent intent){
       // See which child activity is calling us back.
       switch (resultCode) {
           case CONTACT_ADDED:
               // This is the standard resultCode that is sent back if the
               // activity crashed or didn't doesn't supply an explicit result.
               if (resultCode == RESULT_FIRST_USER){
                   Bundle bundle = new Bundle();
                   bundle = intent.getBundleExtra("contactData");
                   addContact(bundle);
                   showDatabaseContent();
               } 
               else{
                   Toast.makeText(this, "CANCEL CONTACT BUTTON PRESSED", Toast.LENGTH_LONG).show();
               }
               break;
           case CONTACT_MODIFIED:
               if (resultCode == 2){
                   Bundle bundle = new Bundle();
                   bundle = intent.getBundleExtra("contactData");
                   modifyContact(bundle);
                   showDatabaseContent();
               }
               else{
                   Toast.makeText(this, "MODIFY CONTACT FAILED", Toast.LENGTH_LONG).show();
               }
               break;
           default:
               break;
       }
   }

 //method removes item from database
   private void removeContact(String itemId){
       db = contacts.getWritableDatabase();
       db.delete(DbConstants.TABLE_NAME, "_ID=" + itemId, null);

   }

   private void addContact(Bundle bundle) {
          // Insert a new record into the Events data source.
          // You would do something similar for delete and update.
          db = contacts.getWritableDatabase();
          ContentValues vals = new ContentValues();
          vals.put(DbConstants.NAME, bundle.getString("contactFirstName"));
          vals.put(DbConstants.PHONE, bundle.getString("contactMobilePhone"));
          vals.put(DbConstants.EMAIL, bundle.getString("contactEmail"));
          vals.put(DbConstants.URL_STRING,bundle.getString("contactUrl"));
          vals.put(DbConstants.ADRESS,bundle.getString("contactadress"));
          vals.put(DbConstants.PHOTO,bundle.getString("contactphoto"));
          db.insertOrThrow(DbConstants.TABLE_NAME, null, vals);
       }

 //method should modify existing Contact
   private void modifyContact(Bundle bundle){
       db = contacts.getWritableDatabase();
       ContentValues vals = new ContentValues();
       vals.put(DbConstants.NAME, bundle.getString("contactFirstName"));
       vals.put(DbConstants.PHONE, bundle.getString("contactMobilePhone"));
       vals.put(DbConstants.EMAIL, bundle.getString("contactEmail"));
       vals.put(DbConstants.URL_STRING,bundle.getString("contactUrl"));
    vals.put(DbConstants.ADRESS,bundle.getString("contactadress"));
    vals.put(DbConstants.PHOTO,bundle.getString("contactphoto"));
       db.update(DbConstants.TABLE_NAME, vals, _ID+"="+itemId, null);
   }

   private Cursor getContacts() {
          db = contacts.getReadableDatabase();
          cursor = db.query(DbConstants.TABLE_NAME, FROM, null, null, null,
                null, null);
          startManagingCursor(cursor);
          return cursor;
   }

   public void showDatabaseContent(){
       contacts = new DbCreate(this); 
       try {
           cursor = getContacts(); 
           showContacts(cursor); 
       } finally {
           contacts.close();
           db.close();

       }
   }


   private void showContacts(Cursor cursor) {
       //set up data binding
       SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, FROM, TO);
       setListAdapter(adapter);
   }
  private void ShowImage()
  {
      //string username created from edit text field to a string
      EditText mpic =(EditText)findViewById(R.id.edittext1);
      String username = mpic.getText().toString(); 
        //bitmap will decode the string to a image (bitmap)
        Bitmap myBitmap = BitmapFactory.decodeFile(username);
        //Image view used to set the bitmap
        ImageView myImage = (ImageView) findViewById(R.id.image);
        //setting the image to the image view
        myImage.setImageBitmap(myBitmap);

  } 

Whole error:

 12-30 20:33:23.292: E/AndroidRuntime(4297): FATAL EXCEPTION: main
 12-30 20:33:23.292: E/AndroidRuntime(4297): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.dbcontactconsole/org.example.dbcontactconsole.DbContactConsole}: java.lang.NullPointerException
 12-30 20:33:23.292: E/AndroidRuntime(4297): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
 12-30 20:33:23.292: E/AndroidRuntime(4297): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
 12-30 20:33:23.292: E/AndroidRuntime(4297): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
 12-30 20:33:23.292: E/AndroidRuntime(4297): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
 12-30 20:33:23.292: E/AndroidRuntime(4297): at android.os.Handler.dispatchMessage(Handler.java:99)
 12-30 20:33:23.292: E/AndroidRuntime(4297): at android.os.Looper.loop(Looper.java:123)
 12-30 20:33:23.292: E/AndroidRuntime(4297): at android.app.ActivityThread.main(ActivityThread.java:4627)
 12-30 20:33:23.292: E/AndroidRuntime(4297): at java.lang.reflect.Method.invokeNative(Native Method)
 12-30 20:33:23.292: E/AndroidRuntime(4297): at java.lang.reflect.Method.invoke(Method.java:521)
 12-30 20:33:23.292: E/AndroidRuntime(4297): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 12-30 20:33:23.292: E/AndroidRuntime(4297): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
 12-30 20:33:23.292: E/AndroidRuntime(4297): at dalvik.system.NativeStart.main(Native Method)
 12-30 20:33:23.292: E/AndroidRuntime(4297): Caused by: java.lang.NullPointerException
 12-30 20:33:23.292: E/AndroidRuntime(4297): at org.example.dbcontactconsole.DbContactConsole.ShowImage(DbContactConsole.java:234)
 12-30 20:33:23.292: E/AndroidRuntime(4297): at org.example.dbcontactconsole.DbContactConsole.onCreate(DbContactConsole.java:46)
 12-30 20:33:23.292: E/AndroidRuntime(4297): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 12-30 20:33:23.292: E/AndroidRuntime(4297): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
 12-30 20:33:23.292: E/AndroidRuntime(4297): ... 11 more
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:07:01+00:00Added an answer on May 28, 2026 at 12:07 am

    The most likely possibility is that mpic is null because no view could be found by that id.

    A secondary possibility is that the result of calling the .getText() method on mpic is null, so that calling the toString() method on the result fails.

    You should add an explicit check to verify that the objects are not null before you try to call their methods, or else surround the attempt with a try/catch, and do something informative if you detect a problem.

    You can also use the android logging facility to print a representation of the objects to the logs, and manually check which is null – though that won’t help you with a future problem that might only arise due to some condition occurring on an end-user’s device but not on your development/test devices.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have referred this code to display contact list. and it is working fine
I am working on a contact list project in android and i want to
I am using Simple Listview to display Email addresses from Contact List. Its working
Working on a project, one of the webpages will display a list of people
I am working on a project where I need to list down all the
Iam working on a contact list application i need to do 2 table because
I am working on a contact list project and so far i can add,modify
I am working on a contact list application I need to create 2 tables
I am working on an app that reads the contact names from the mobile
I am working on android apps. I want to add a contact in android

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.