I've tried searching, but haven't found anything that could help me yet. So here it goes:
I'm trying to connect a card to a customer via their customerID, I can't seem to be getting it to work properly (NullPointerExceptions). The objects will be put in ArrayLists.
I'm pretty sure the problem is in the 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 the problem is here!
}
This is the User-class which i want to link to the 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 is where i'm trying to 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 i nummerformat!");
}
}
Here is the first of two archive-classes. First the one for the Cards.
import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;
public class CardArchive implements Serializable {
ArrayList clist = new ArrayList();
// Setter inn nytt LiftCard-objekt bakerst i lista
public void regLiftCard(LiftCard c) {
clist.add(c);
}
public String toString() {
String cards = "";
Iterator iterator = clist.iterator();
while (iterator.hasNext()) {
cards += iterator.next().toString() + "\n";
}
return cards;
}
public LiftCard findByCardNumber(int id) {
for (LiftCard c : clist) {
if (c.getCardNumber() == id) {
return c;
}
}
return null; // or empty Card
}
}
And the second is the User-Archive:
import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;
public class UserArchive implements Serializable {
ArrayList list = new ArrayList();
private static int idCounter = 1000;
// Setter inn nytt User-objekt bakerst i 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) {
for (User u : list) {
if (u.getCustomerID() == id) {
return u;
}
}
return null; // or empty User
}
public String toString() {
sorter();
String users = "";
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
users += iterator.next().toString() + "\n";
}
return users;
}
}
As you can probably see, just by looking at some of the code, I'm pretty new at java. But trying none the less. Thanks for any and all help in advance!
EDIT:
Used a combination of both maloney and bmorris591 answers!
Code i changed is as 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 for the help guys!
Answer
From you code it looks like you are using the user
field from the LiftCard
in its toString
method:
public String toString() {
return getUser().getCustomerID() //etc
But when you create the LiftCard
I do not see you setting the 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 to at some point call l.setUser(u)
otherwise the user
field in the LiftCard
is 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 we set the `User` on the `LiftCard`.
cardA.regLiftCard(l); //etc
No comments:
Post a Comment