user interface - Java reading and writing ArrayList type Contact to GUI -


edit

i've made several corrections , able compile. when attempting add contact file receive null pointer exception @ line 13 of contactscollection class:

for (int = 0; < contactlist.size(); i++) 

and line 93 of contacts (gui) class:

contactlist.add(contact); 

contacts class:

import java.util.*; import java.io.*;  public class contact implements serializable { public static final long serialversionuid = 42l; public string name, number;  contact() {     name = "no name";     number = "no number"; }  contact (string thename, string thenumber) {     this.name = thename;     this.number = thenumber; }  public void setname(string aname) {     this.name = aname; }  public void setnumber(string anumber) {     this.number =anumber; }  public string getname() {     return name; }  public string getnumber() {     return number; }  public string tostring() {     return name + ": " + number; }  public boolean equals(contact other) {    if (name.equals(other.getname()) && number.equals(other.getnumber()))    {       return(true);    }    else    {       return(false);    } }    } 

contacts collection class

import java.io.*; import java.util.*;  class contactscollection { public static arraylist<contact> contactlist;  public static void write() {     try      {         objectoutputstream out = new objectoutputstream(new fileoutputstream("contactlist.dat"));         (int = 0; < contactlist.size(); i++)         {             out.writeobject(contactlist.get(i));         }         out.close();     }      catch(ioexception e)     {         e.printstacktrace();     } }  public static void read() { contactlist = new arraylist<contact>(); try {     objectinputstream in = new objectinputstream(new fileinputstream("contactlist.dat"));     contact temp;     while (in.available()!=0)     {         temp = (contact)in.readobject();         contactlist.add(temp);     }     in.close(); } catch(filenotfoundexception e) {     e.printstacktrace(); } catch(ioexception e) {     e.printstacktrace(); } catch(classnotfoundexception e) {     e.printstacktrace(); }         } } 

contacts (gui) class

import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.timer; import java.util.*;  class contacts extends jframe implements actionlistener, windowlistener { public static final int width = 400;     public static final int height = 600;     public static final int small_width = 200;     public static final int small_height = 100;  private static final dimension stdbtn = new dimension(150, 50);          jpanel centerpanel, northpanel, southpanel; imageicon icon; jlabel picture; jbutton addcontact, displaycontacts; jscrollpane scroll; jtextarea textarea; clock clock; background background; contact contact; arraylist<contact> contactlist;   public contacts() {     super("contacts");     this.setlayout(new borderlayout());     this.setsize(width, height);     this.setdefaultcloseoperation(jframe.do_nothing_on_close);     addwindowlistener(this);     this.setlocationrelativeto(null);      centerpanel = new jpanel();     northpanel = new jpanel();     southpanel = new jpanel();      centerpanel.setbackground(color.black);     southpanel.setbackground(color.black);            clock = new clock();     northpanel.add(clock);      icon = new imageicon("contactsbackground.jpg");     picture = new jlabel(icon);     centerpanel.add(picture);      textarea = new jtextarea("", 10, 30);     textarea.seteditable(false);     jscrollpane scroll = new jscrollpane(textarea, jscrollpane.vertical_scrollbar_always,             jscrollpane.horizontal_scrollbar_as_needed);         centerpanel.add(scroll);          jbutton displaycontacts = new jbutton("display contacts");     displaycontacts.addactionlistener(this);     southpanel.add(displaycontacts);      jbutton addcontact = new jbutton("add contact");     addcontact.addactionlistener(this);     southpanel.add(addcontact);      this.add(northpanel, borderlayout.north);     this.add(centerpanel, borderlayout.center);     this.add(southpanel, borderlayout.south);             setresizable(false);         }  public void actionperformed(actionevent e) {     contactlist = new arraylist<contact>();     jbutton source = (jbutton)e.getsource();     string contactinput = source.gettext();      if (contactinput == "display contacts")     {         contactscollection.read();         (int = 0; < contactlist.size(); i++)         {             contact = (contact)contactlist.get(i);             textarea.settext(contact.getname() + "," + contact.getnumber() + "\n");         }     }     if (contactinput == "add contact")     {         string name = joptionpane.showinputdialog(null, "enter name");         string number = joptionpane.showinputdialog(null, "enter number");         contact = new contact(name, number);         contactlist.add(contact);         contactscollection.write();     } }  public void windowopened(windowevent e) {}  public void windowclosing(windowevent e) {     this.setvisible(false);     background = new background();     background.setvisible(true);         }  public void windowclosed(windowevent e) {}  public void windowiconified(windowevent e) {}  public void windowdeiconified(windowevent e) {}  public void windowactivated(windowevent e) {}  public void windowdeactivated(windowevent e) {}       } 

regarding

the first error i'm running "cannot find symbol method read()" , "cannot find symbol method write()".

you're calling method on arraylist, read(), doesn't exist. need call method on 1 of contents of arraylist, not arraylist itself. it's trying make omelette egg crate -- won't work. need use egg or 2 instead. same thing write().

regarding

the second error in action listener display contacts stating "an array required contact found".

you should tell line causes error.


edit
show:

for (int = 0; < contactlist.size(); i++) {     textarea.settext(contact[i].getname() + "," + contact [i].getnumber() + "\n"); } 

that code makes no sense. need use list variable, contactlist , item via arraylist get(...) method.


your code suggests you're blindly adding bunch of code regardless of errors, , won't work. if can't use ide, should compile often, after each 1-3 lines , fix compilation errors before trying add more code. else you'll end rat's nest of errors. also, read manual/book. don't guess @ stuff.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -