0

I've created an empty arraylist here:

private ArrayList<OrderItem>  conveyorBelt = new ArrayList<OrderItem>(10);

And in the same class, I've created a method where I add item into the conveyorBelt by inputting the orderNum (which is an int). This is what the method looks like:

public void addToConveyorBelt( int orderNum )
{
OrderItem oi;
conveyorBelt.add(oi.getOrderNum(orderNum)); // line 4
}

This doesn't work. I get a compile error on line 4, saying this: http://i52.tinypic.com/ny8n86.jpg

Anyone know where i'm going wrong with this?

p.s. - the OrderItem contains one variable called theOrderNum and a method that calls it called getOrderNum.

2
  • 1
    why do you need to get the order num from the orderitem if you already have the order num? Surely what you are trying to do is create a new OrderItem from an order num and add that to the list? Commented Dec 25, 2010 at 1:25
  • Yes, that's what i'm trying to do. I apologise for not making myself clear. Commented Dec 25, 2010 at 1:57

4 Answers 4

3

According to the error message, the method signature of "getOrderNo" does not accept a parameter of type "int". From what you've given, I suspect the OrderItem class looks something like this:

public class OrderItem {
    private int theOrderNum;

    OrderItem(int num) {
        theOrderNum = num;
    }

    public int getOrderNum() {
        return theOrderNum;
    }
}

If so, you want to create a new OrderItem from the orderNum parameter, and then add it to conveyerBelt:

public void addToConveyorBelt(int orderNum) {
    OrderItem oi = new OrderItem(orderNum);
    conveyorBelt.add(oi);
}

If not, you need to update the question to include more information about the OrderItem class.

Sign up to request clarification or add additional context in comments.

Comments

1

getOrderNum obviously returns an int. Your arraylist is an arraylist of OrderItems. What are you expecting to happen?

Not only that, but you're guarunteed a null pointer exception there because oi isn't initialised.

2 Comments

Not entirely correct. Local variables in a method are not initialized to null if they are not assigned. Like this the compiler will complain that the variable is not initialized.
Thanks for the clear up Reboot. I was going to reply with something along those lines. Thanks anyways Falmarri.
1

I suspect your method should be something like this:

public void addToConveyorBelt(int orderNum){
    OrderItem oi = getOrderItem(orderNum);
    conveyorBelt.add(oi); 
}

1 Comment

Thanks Robert. But on line three I get a compile error saying "cannot find symbol - method getOrderNum(orderNum)". (even though you said "getOrderItem", I put "getOrderNum" because that's what the method is called in OrderItem). Know what might be the problem?
0

you define arraylist like that

private ArrayList<OrderItem>  conveyorBelt = new ArrayList<OrderItem>(10);

It means you can only add OrderItem to the list

public void addToConveyorBelt( int orderNum )
{
OrderItem oi;
conveyorBelt.add(oi.getOrderNum(orderNum)); // here it fails
}

Change it as follows

public void addToConveyorBelt( int orderNum )
    {
    OrderItem oi = new OrderItem(orderNum);
    conveyorBelt.add(oi); // here it fails
    }

oi.getOrderNum(orderNum) should throw NullPointerException

1 Comment

Thanks hilal. This is also useful (NullPointerException part).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.