我试过搜索,但还没有找到任何可以帮助我的东西。所以这里是:
我正在尝试通过客户 ID 将卡连接到客户,但我似乎无法使其正常工作(NullPointerExceptions)。对象将被放入 ArrayLists。
我很确定问题出在 LiftCard() 的 toString() 中。
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!
}
这是我想链接到卡类的用户类
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));
}
}
这是我试图将它们绑定在一起的地方:
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!");
}
}
这是两个归档类中的第一个。首先是卡片。
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 i 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) {
for (LiftCard c : clist) {
if (c.getCardNumber() == id) {
return c;
}
}
return null; // or empty Card
}
}
第二个是用户存档:
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 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<User> iterator = list.iterator();
while (iterator.hasNext()) {
users += iterator.next().toString() + "\n";
}
return users;
}
}
正如您可能看到的,仅通过查看一些代码,我对 java 还是很陌生。但还是要努力。感谢您提前提供的所有帮助!
编辑:
使用了 maloney 和 bmorris591 答案的组合!我更改的代码如下:
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);
和:
public String toString() {
if (getUser() != null){
return getUser().getCustomerID() + "\t" + getUser().getSurename() + "\t" + cardNumber + "\t" + cardType;
} else {
return null;
}
}
谢谢你们的帮助!