if statement - How to check and Entry before adding and store it in an Array in Java? -


good day.. have address book program... runs doesn't check if user input stored in array.. want want is... after getting user input compare stored entry in array , when unique... allow user add new entry...

here's old code in addentry():

public void addentry() {

entry[counter] = new addressbookentry(); entry[counter].setname(joptionpane.showinputdialog("enter name: ")); entry[counter].setadd(joptionpane.showinputdialog("enter add: ")); entry[counter].setphoneno(joptionpane.showinputdialog("enter phone no.: ")); entry[counter].setemail(joptionpane.showinputdialog("enter e-mail: ")); counter++; 

}

and here's planning turns out error:

public void addentry() {

    entry[counter] = new addressbookentry();     sname = joptionpane.showinputdialog("enter name: ");//<-- asks user name     if (!entry[counter].getname().equals(sname)) {//<--compare         entry[counter].setname(joptionpane.showinputdialog("enter name: "));         entry[counter].setadd(joptionpane.showinputdialog("enter add: "));         entry[counter].setphoneno(joptionpane.showinputdialog("enter phone no.: "));         entry[counter].setemail(joptionpane.showinputdialog("enter e-mail: "));         counter++;     } } 

this error: exception in thread "main" java.lang.nullpointerexception @ addressbook.addentry(addressbook.java:57) @ addressbook.main(addressbook.java:28) java result: 1 build successful (total time: 4 seconds)

the output ask user input name , exit automatically... might logical error... don't know possible solution

need please thanks

here's complete code:

import javax.swing.joptionpane; import javax.swing.jtextarea;  public class addressbook {      private addressbookentry entry[];     private int counter;     private string sname;     private int notfound = 0;      public static void main(string[] args) {         addressbook = new addressbook();         a.entry = new addressbookentry[100];         int option = 0;         try {             while (option != 5) {                 string content = "choose option\n\n"                         + "[1] add entry\n"                         + "[2] delete entry\n"                         + "[3] update entry\n"                         + "[4] view entries\n"                         + "[5] view specific entry\n"                         + "[6] exit";                 option = integer.parseint(joptionpane.showinputdialog(content));                 switch (option) {                     case 1:                         a.addentry();                         break;                     case 2:                         a.deleteentry();                         break;                     case 3:                         a.editentry();                         break;                     case 4:                         a.viewall();                         break;                     case 5:                         a.searchentry();                         break;                     case 6:                         system.exit(1);                         break;                     default:                         joptionpane.showmessagedialog(null, "invalid choice!");                 }             }         } catch (numberformatexception e) {             joptionpane.showmessagedialog(null, "please choose number in displayed menu");         }     }      public void addentry() {         entry[counter] = new addressbookentry();         sname = joptionpane.showinputdialog("enter name: ");         if (entry[counter] == null &&  entry[counter].getname() == null                 && !entry[counter].getname().equals(sname)) {             entry[counter].setname(joptionpane.showinputdialog("enter name: "));             entry[counter].setadd(joptionpane.showinputdialog("enter add: "));             entry[counter].setphoneno(joptionpane.showinputdialog("enter phone no.: "));             entry[counter].setemail(joptionpane.showinputdialog("enter e-mail: "));             counter++;         }     }     /*public void addentry() {     entry[counter] = new addressbookentry();     entry[counter].setname(joptionpane.showinputdialog("enter name: "));     entry[counter].setadd(joptionpane.showinputdialog("enter add: "));     entry[counter].setphoneno(joptionpane.showinputdialog("enter phone no.: "));     entry[counter].setemail(joptionpane.showinputdialog("enter e-mail: "));     counter++;     }*/      public void viewall() {         string addtext = "  name\taddress\tphone no.\te-mail add\n\n";         int nonnull = 0;         (int = 0; < entry.length; i++) {             if (entry[i] != null) {                 addtext = addtext + entry[i].getinfo() + "\n";                 nonnull++;             }             if (nonnull == counter) {                 break;             }         }         joptionpane.showmessagedialog(null, new jtextarea(addtext));     }      public void searchentry() {         sname = joptionpane.showinputdialog("enter name find: ");         searchmethod();     }      public void searchmethod() {         (int = 0; < counter; i++) {             if (entry[i].getname().equals(sname)) {                 joptionpane.showmessagedialog(null, entry[i].getinfo2());                 notfound = 0;                 break;             } else {                 notfound++;             }         }         if (notfound != 0) {             joptionpane.showmessagedialog(null, "name not found!");         }     }      public void editentry() {         sname = joptionpane.showinputdialog("enter name edit: ");         (int = 0; < counter; i++) {             if (entry[i].getname().equals(sname)) {                 entry[i] = new addressbookentry();                 entry[i].setname(joptionpane.showinputdialog("enter new name: "));                 entry[i].setadd(joptionpane.showinputdialog("enter new add: "));                 entry[i].setphoneno(joptionpane.showinputdialog("enter new phone no.: "));                 entry[i].setemail(joptionpane.showinputdialog("enter new e-mail: "));                 notfound = 0;                 break;             } else {                 notfound++;             }         }         if (notfound != 0) {             joptionpane.showmessagedialog(null, "name not found!");         }     }      public void deleteentry() {         sname = joptionpane.showinputdialog("enter name delete: ");         if (sname == null) {             return;         }         (int = 0; < counter; i++) {             if (entry[i] != null && sname.equals(entry[i].getname())) {                 entry[i] = null;                 joptionpane.showmessagedialog(null, "found!");                 break;             }         }     } } 

hope can me... because still dont understand do. show complete code... patience guys...

check make sure entry isn't null before try doing getname() it.

if (!entry[counter].getname().equals(sname)) 

turns into:

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

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? -