java - Trying to link two ArrayList objects together -
i've tried searching, haven't found me yet. here goes:
i'm trying connect card customer via customerid, can't seem getting work (nullpointerexceptions). objects put in arraylists.
i'm pretty sure problem in tostring() of liftcard().
import java.io.serializable; public class liftcard implements serializable { private int cardnumber, cardtype, cardprice, cardpassing; int cardbalance; private user user; liftcard next; public liftcard(int ct, int cn) { cardnumber = cn; cardtype = ct; next = null; }// end of konstruktqr public int getcardnumber() { return cardnumber; } public int getcardtype() { return cardtype; } public user getuser(){ return user; } public void setuser(user u){ user = u; } public string tostring() { return getuser().getcustomerid() + "\t" + cardnumber + "\t" + cardtype; //pretty sure problem here! }
this user-class want link card-class
import java.io.serializable; public class user implements serializable { private string surename, firstname, phone, adress, birth; private int customerid; public liftcard liftcard; user next; public user( int cid, string fn, string sn, string p, string a, string b) { firstname = fn; surename = sn; phone = p; adress = a; birth = b; customerid = cid; liftcard = null; next = null; } public string getsurename() { return surename; } public string getfirstname() { return firstname; } public string getphone() { return phone; } public string getadress(){ return adress; } public string getbirth() { return birth; } public int getcustomerid() { return customerid; } public liftcard getliftcard(){ return liftcard; } public void setliftcard(liftcard liftc){ liftcard = liftc; } public string tostring() { return customerid + "\t" + surename + "\t" + firstname + "\t" + phone + "\t" + adress + "\t" + birth; } public boolean equals(user u) { return (u.getsurename().equals(surename) && u.getfirstname().equals( firstname)); } }
here i'm trying bind them together:
public void regliftcard() { int cardtype = integer.parseint(cardtypefield.gettext()); int cardnumber = integer.parseint(cardnumberfield.gettext()); int customerid = integer.parseint(findcustomerfield.gettext()); if (cardtype != 1 && cardtype != 2 && cardtype != 3) { joptionpane.showmessagedialog(this, "kortet må være 1,2 eller 3!"); return; } try { string firstname = firstnamefield.gettext(); string surename = surenamefield.gettext(); string phone = phonefield.gettext(); string adress = adressfield.gettext(); string birth = birthfield.gettext(); user u = usera.findbyid(customerid); liftcard l = new liftcard(cardnumber, cardtype); if (u != null) { if (u.getliftcard() != null) { joptionpane.showmessagedialog(this, "brukeren har allerede kort!"); } else carda.regliftcard(l); joptionpane.showmessagedialog(this, "1kort registrert!"); return; } else u = new user(customerid++, firstname, surename, phone, adress, birth); joptionpane.showmessagedialog(this, "bruker registrert!"); cardtypefield.settext(""); } catch (numberformatexception e) { joptionpane.showmessagedialog(this, "feil nummerformat!"); } }
here first of 2 archive-classes. first 1 cards.
import java.util.*; import java.io.*; import javax.swing.joptionpane; public class cardarchive implements serializable { arraylist<liftcard> clist = new arraylist<liftcard>(); // setter inn nytt liftcard-objekt bakerst lista public void regliftcard(liftcard c) { clist.add(c); } public string tostring() { string cards = ""; iterator<liftcard> iterator = clist.iterator(); while (iterator.hasnext()) { cards += iterator.next().tostring() + "\n"; } return cards; } public liftcard findbycardnumber(int id) { (liftcard c : clist) { if (c.getcardnumber() == id) { return c; } } return null; // or empty card } }
and second user-archive:
import java.util.*; import java.io.*; import javax.swing.joptionpane; public class userarchive implements serializable { arraylist<user> list = new arraylist<user>(); private static int idcounter = 1000; // setter inn nytt user-objekt bakerst lista public void regcustomer(user u) { list.add(u); } // sorterer user-objektene alfabetisk på surename og first name public void sorter() { collections.sort(list, new usercomp()); } public user findbyid(int id) { (user u : list) { if (u.getcustomerid() == id) { return u; } } return null; // or empty user } public string tostring() { sorter(); string users = ""; iterator<user> iterator = list.iterator(); while (iterator.hasnext()) { users += iterator.next().tostring() + "\n"; } return users; } }
as can see, looking @ of code, i'm pretty new @ java. trying none less. , in advance!
edit:
used combination of both maloney , bmorris591 answers! code changed follows:
user u = usera.findbyid(customerid); liftcard l = new liftcard(cardnumber, cardtype); if (u != null) { if (u.getliftcard() != null) { joptionpane.showmessagedialog(this, "brukeren har allerede kort!"); } else l.setuser(u); carda.regliftcard(l);
and:
public string tostring() { if (getuser() != null){ return getuser().getcustomerid() + "\t" + getuser().getsurename() + "\t" + cardnumber + "\t" + cardtype; } else { return null; } }
thanks guys!
from code looks using user
field liftcard
in tostring
method:
public string tostring() { return getuser().getcustomerid() //etc
but when create liftcard
not see setting user
anywhere before registering it:
liftcard l = new liftcard(cardnumber, cardtype); if (u != null) { if (u.getliftcard() != null) { joptionpane.showmessagedialog(this, "brukeren har allerede kort!"); } else carda.regliftcard(l); //etc
you need @ point call l.setuser(u)
otherwise user
field in liftcard
null
.
liftcard l = new liftcard(cardnumber, cardtype); if (u != null) { if (u.getliftcard() != null) { joptionpane.showmessagedialog(this, "brukeren har allerede kort!"); } else l.setuser(u); // <-- here set `user` on `liftcard`. carda.regliftcard(l); //etc
Comments
Post a Comment