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

时间:2013-04-18 11:19:37

标签: java arraylist nullpointerexception

我试过搜索,但还没找到任何可以帮助我的东西。所以这就是:

我正在尝试通过他们的customerID将卡连接到客户,我似乎无法让它正常工作(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;
    }
}

感谢帮助人员!

3 个答案:

答案 0 :(得分:0)

尝试:

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

答案 1 :(得分:0)

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

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

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

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

答案 2 :(得分:0)

从您的代码中看来,您使用user方法LiftCard中的toString字段:

public String toString() {
    return getUser().getCustomerID() //etc

但是当你创建LiftCard时,我没有看到你在注册之前在任何地方设置user

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),否则user中的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 we set the `User` on the `LiftCard`.
                cardA.regLiftCard(l); //etc