serialization - Java ArrayList null pointer exception -


edit

i've cleaned .equals method string equality , changed contactscollection initialization to:

public static arraylist<contact> contactlist = new arraylist<contact>(); 

i've changed action performed method in hopes 'display contacts' show more 1 contact.

    if (contactinput.equals("display contacts"))     {         contactscollection.read();         (int = 0; < contactlist.size(); i++)         {             contact = (contact)contactlist.get(i);             (int j =0; j < contactlist.size(); j++)             {                 textarea.append(contact.getname() + "," + contact.getnumber() + "\n");             }         }     } 

ultimately .dat file written not contain data added through gui.

end edit

i writing mock cellphone gui acts basic contacts manager. there several other classes not deal arraylist working intended.

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); 

i have feeling did wrong when coding contacts , contactscollection classes. i'm hoping program run follows: user clicks add contact , enters information becomes object contact , added contactlist arraylist , written (serialized) file "contactlist.dat". when user clicks display contacts file read in , each contact displayed in gui.

i think there several issues way set arraylist, think i'm close having program run had hoped. appreciated!

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) {}       } 

change

public static arraylist<contact> contactlist; 

to

public static arraylist<contact> contactlist = new arraylist<>(); 

in version contactlist null because never initialize , in write() method trying call size() on it.


also, there serious flaws in gui class (in actionperformed() method), read question fix them: how compare strings in java?


also remember textarea.settext(...) set complete text textarea, because in loop in code, textarea contain output of last iteration of loop only. in case last contact.


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 -