Java 尝试将两个ArrayList对象链接在一起

Java 尝试将两个ArrayList对象链接在一起,java,arraylist,nullpointerexception,Java,Arraylist,Nullpointerexception,我试过搜索,但还没有找到任何可以帮助我的东西。下面是: 我试图通过客户ID将卡连接到客户,但似乎无法使其正常工作(NullPointerExceptions)。对象将放在ArrayList中 我很确定问题出在LiftCard()的toString()中 这是我想要链接到card类的用户类 import java.io.Serializable; public class User implements Serializable { private String surename, firstN

我试过搜索,但还没有找到任何可以帮助我的东西。下面是:

我试图通过客户ID将卡连接到客户,但似乎无法使其正常工作(NullPointerExceptions)。对象将放在ArrayList中

我很确定问题出在LiftCard()的toString()中

这是我想要链接到card类的用户类

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
}


}
以及:

谢谢你们的帮助

试试看:

public String toString() {
    return (getUser() == null ? null : getUser().getCustomerID()) + "\t" + cardNumber + "\t" + cardType; 
}

您不需要初始化LiftCard类中的
user
对象。您可以在构造函数中执行此操作,或者在调用
getUser()
之前调用
setUser(user)
方法,它应该会解决您的问题

您还可以对代码的该部分进行空检查:

public String toString() {
    if (getUser() != null){
        return getUser().getCustomerID() + "\t" + cardNumber + "\t" + cardType; 
    } else {
       return null;
    }
}

这将停止抛出异常,但如果希望代码正常工作,您仍需要初始化用户。

从您的代码中,您似乎正在使用
LiftCard
中的
toString
方法中的
user
字段:

public String toString() {
    return getUser().getCustomerID() //etc
但是,当您创建
LiftCard
时,我看不到您在注册之前在任何地方设置
用户

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
您需要在某个时候调用
l.setUser(u)
,否则
LiftCard
中的
user
字段为
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
LiftCard l=新的LiftCard(卡号、卡类型);
如果(u!=null){
如果(u.getLiftCard()!=null){
JOptionPane.showMessageDialog(此,
“布鲁克伦·哈尔·阿莱德·科特!”;
}否则

l、 setUser(u);//相关问题:显然,我们无法帮助您……因为您的问题中没有包含stacktrace。谢谢您的帮助!现在可以使用了。很抱歉没有发布stacktrace。
public String toString() {
    if (getUser() != null){
        return getUser().getCustomerID() + "\t" + cardNumber + "\t" + cardType; 
    } else {
       return null;
    }
}
public String toString() {
    return getUser().getCustomerID() //etc
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
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