Reputation: 797
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
Upvotes: 1
Views: 300
Reputation: 2924
can you post your main.xml layout file? I would guess the
findViewById(R.id.edittext1)
call is returning null, perhaps because of some misidentification issue...
Upvotes: 1
Reputation: 2889
check to see if the mpic returns null. Maybe the id name is incorrect? Maybe you need to call the findViewById method on the correct view for instance
EditText mpic =(EditText)myView.findViewById(R.id.edittext1);
Upvotes: 0