0

I could assign objects (ingredients) to an array in the main class, like this:

Ingredient[] ingredient = new Ingredient[5];
Ingredient potato = new Ingredient("Potatoes");
ingredient[0] = potato;

but what I really want to do is put the array inside another object (food), so I can access it like this:

fries.ingredient[0] = potato;

so that each food has its own collection of ingredients. However, everything I've tried leads to 'NullPointerException' or 'cannot find symbol'. How do I resolve this?

Edit:
Sorry for taking a while. I don't know how to indent inside blockquotes, but here goes. This was my (failed) attempt that causes NullPointerException.

Main.java:

public class Main {
public static void main (String[] args) {
Ingredient potato = new Ingredient("Potatoes");
Food fries = new Food("Fries");
fries.ingredient[0] = potato;
} }

Food.java:

public class Food {
Ingredient[] ingredient;
String name;
public Food(String name) {
this.name = name;
Ingredient[] ingredient = new Ingredient[5];
} }

Ingredient.java

public class Ingredient {
String name;
public Ingredient(String name) {
this.name = name;
} }

4
  • 2
    Could you show us code that generate this exception/error? Commented Dec 22, 2013 at 12:36
  • We can't tell you how to fix your code without seeing neither the the code, nor the error message. Commented Dec 22, 2013 at 12:37
  • A NullPointerException is thrown when an application attempts to use null in a case where an object is required. You are trying to access something that doesn't exist, post your code it will help to help you. Commented Dec 22, 2013 at 12:40
  • You most likely didn't instantiate Ingredient. You'd have to post more code for us to see. Commented Dec 22, 2013 at 12:41

4 Answers 4

1

In your constructor, you have this:

Ingredient[] ingredient = new Ingredient[5];

You have declared a local variable named ingredient which shadows your instance variable of the same name. Change that line to

this.ingredient = new Ingredient[5];

As a next step in your learning, consider using a List<Ingredient> instead of the array. Arrays are non-resizable, among other inconveniences. Basically, their primary use is in the internals of an implementation, not in client code.

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

1 Comment

leads to 'cannot find symbol' for me edit: nvm, works now. thanks!
0

create a class:

public class Food{
   public Object[] ingredient;
   public food(int numOfIng){
        this.ingredients=new Object[numOfIng]
   }
}

now just do:

Food fries=new Food(5);
Ingredient potato = new Ingredient("Potatoes");
fries.ingredient[0] = potato;

Comments

0

You can try to do though interface, so as I think something like:

interface Food {

   void takeInfo();
}

public class FoodSelection{

private List<Food> listOfFood = new ArrayList<>();

public void selectFries(){
  initFood(new Fries());
}

// other food...

public void initFood(Food food){
food.takeInfo();
listOfFood.add(food);
}

}

Now it's time to create fries class..

class Fries implements Food{

private String[] ingredients;

public void takeInfo(){
   // declare Fries ingredients.
}

}

So, to call it do something like this:

FoodSelection takeFood = new FoodSelection();

takeFood.selectFries();

2 Comments

I'll try, but I'm not yet familiar with those keywords
It will works. And btw the question in your post is answered by Marko Topolnik
0

you should do that as the following code:

Ingredient potato = new Ingredient("Potatoes");
fries = new ClassName(); //Replace with your class Constructor method
fries.ingredient = new Ingredient[5];
fries.ingredient[0] = potato;

Edit:

Your Food class must be like this:

public class Food {
    Ingredient[] ingredient;
    String name;

    public Food(String name) {
        this.name = name;
        this.ingredient = new Ingredient[5];
    }
}

and Main class:

public class Main {
    public static void main(String[] args) {
        Ingredient potato = new Ingredient("Potatoes");
        Food fries = new Food("Fries");
        fries.ingredient[0] = potato;
    }
}

and Ingredient class

public class Ingredient {
    String name;

    public Ingredient(String name) {
        this.name = name;
    }
}

Comments

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.