How to delete entry in an Address Book program using Java? -


i having trouble logic of deleting entry in address book... saving entries using array.

i try make array[i] = null, if array[i] equals entered name of user. after delete entry , try view entries again, nothing shows.. , output says :

exception in thread "main" java.lang.nullpointerexception
@ addressbook.viewall(addressbook.java:61)
@ addressbook.main(addressbook.java:35)
java result: 1

this code in deleting entry:

public void deleteentry() {         sname = joptionpane.showinputdialog("enter name delete: ");         (int = 0; < counter; i++) {             if (entry[i].getname().equals(sname)) {                 //joptionpane.showmessagedialog(null, "found!");                 entry[i] = null;             }         }     } 

can me figure out wrong code... or logical error?

if have suggestion or better way delete entry big help..

please help...

if (entry[i].getname().equals(sname)) { 

if on 1 pass make

entry[i] = null 

then how getname() afterwords?

try adding null check if statement

if (entry[i] != null && entry[i].getname().equals(sname)) { 

edit: benjamin brings point. should prepared null result showinputdialog(). example, there's cancel button right? if press that, you'll null believe. here's better code case:

public void deleteentry() {     /* input */     sname = joptionpane.showinputdialog("enter name delete: ");     /* if no input, nothing delete */     if(sname == null) return;     /* find name */     (int = 0; < counter; i++) {         /* make sure have entry*/                                 /* know sname not null */         if (entry[i] != null && sname.equals(entry[i].getname())) {             /* null out deleted entry */             entry[i] = null;             // break; /* if know have unique names, can leave loop */         } /* end if */     } /* end i*/ } 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -