值未分配给字节

时间:2016-05-21 11:00:10

标签: java arrays rabbitmq

我遇到了有关我的代码的问题,但我似乎无法找到它。我的问题是我保存在数组中的值没有保存在我的对象中。代码是(请注意@Override部分在run()方法中,链表定义在类的顶部:

private LinkedList<Order> orders = new LinkedList<Order>();

            @Override
            public void handleDelivery(String tag, Envelope env, AMQP.BasicProperties props, byte[] body)
                    throws IOException {
                byte[] tmp = new byte[5];
                tmp[0] = nextID;
                nextID++;
                channel.basicPublish("", "toClient", null, new byte[]{tmp[0]});
                for (int i = 1; i < tmp.length; i++)
                    tmp[i] = body[i-1];
                orders.add(new Order(tmp));
                processOrders(tmp);

我的订单类很简单

private byte id;
/**
 * Byte containing the amount of cups of coffee. Amount is represented as a
 * number (Integer)
 */
private byte coffee;
/**
 * Byte indicating the amount of cafe Latté that has been ordered. Amount
 * is represented as a number (Integer)
 */
private byte cafeLatte;
/**
 * Byte indicating the amount of smoothies that has been ordered. Amount
 * is represented as a number (Integer)
 * 
 */
private byte smoothie;
/**
 * Byte indicating the amount of Ice coffee that has been ordered. Amount
 * is represented as a number (Integer)
 */
private byte iceCoffee;
/**
 * Boolean showing if hot drinks has been served or not.
 */
private boolean hotServed;
/**
 * Boolean showing if cold drinks has been served or not.
 */
private boolean coldServed;

public Order(byte[] orders) {
    id = orders[0];
    coffee = orders[1];
    cafeLatte = orders[2];
    smoothie = orders[3];
    iceCoffee = orders[4];
}

public byte getId() {
    return id;
}

public boolean coldServed() {
    return coldServed;
}

public void serveCold(boolean coldServed) {
    this.coldServed = coldServed;
}

public void serveHot(boolean served) {
    hotServed = served;
}

public boolean hotServed() {
    return hotServed;
}

/**
 * Method for converting the order into a byte array.
 * @return a 5 columns byte array containing id, coffee, cafe latté,
 * smooties, ice coffees.
 */
public byte[] toByteArray() {
    return new byte[] {id, coffee, cafeLatte, smoothie, iceCoffee};
}

/**
 * Method for retrieving all the ordered cold drinks with id first.
 * @return An array with numbers indicating the amount of smoothies and
 * Ice coffee to make. Id of the order is first, smoothies next and last
 * Ice coffee.
 */
public byte[] getColdDrinks() {
    return new byte[] {id, smoothie, iceCoffee};
}

/**
 * Method for retrieving all the ordered hot drinks with id first.
 * @return An array with numbers indicating the amount of coffee and
 *  cafelatte to brew. Id of the order is first, coffee next and last
 *  cafeLatte.
 */
public byte[] getHotDrinks() {
    return new byte[] {id, coffee, cafeLatte};
}

当我在Eclipse中运行调试时,我看到handleDelivery方法中的tmp数组从body数组中检索正确的值,orders.add(new Order(tmp));将一个元素添加到我的linkedList,但只有值id, 0,0,0,0(注意)这个id是从rabbitmq收到的正确的id),但是如果我订购一杯咖啡,当我以某种方式调用orders.add(tmp));时,值1会丢失。

1 个答案:

答案 0 :(得分:0)

对象不应该由类(常量除外)实例化,而是由构造函数或运行方法实例化。从课堂上的private LinkedList<Order> orders = new LinkedList<Order>();更改为private LinkedList<Order> orders;并在orders = new LinkedList<Order>();中添加了run()解决了这个问题。