2013-12-22 214 views
0

我可以分配对象(成分)的阵列在主类的数组,像这样:对象包含对象

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

,但我真正想要的要做的是把数组放在另一个对象(食物)中,所以我可以像这样访问它:

fries.ingredient[0] = potato;

这样每个食物都有自己的成分集合。但是,我尝试过的所有内容都会导致'NullPointerException'或'找不到符号'。我该如何解决这个问题?

编辑:
对不起,花一点时间。我不知道如何在块引号内缩进,但是在这里。这是我的(失败)尝试导致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];
} }

成分。java的

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

+2

你能告诉我们产生这种异常/错误的代码吗? – Pshemo

+0

我们不能告诉你如何修正你的代码而不会看到代码和错误信息。 –

+0

'如果应用程序尝试在需要对象的情况下尝试使用null,则会引发NullPointerException。# 您试图访问不存在的内容,请发布您的代码以帮助您。 –

回答

1

在构造函数中,你有这样的:

Ingredient[] ingredient = new Ingredient[5]; 

您已经声明了一个局部变量命名ingredient其阴影同名的实例变量。该行更改为

this.ingredient = new Ingredient[5]; 

在你学习下一个步骤,可以考虑使用List<Ingredient>而非阵列。阵列不可调整大小,以及其他不便之处。基本上,它们的主要用途是在实现的内部,而不是在客户端代码中。

+0

建议你把完整的代码... – tgkprog

+0

导致'找不到符号' 编辑:nvm,现在工作。谢谢! – user3127055

0

你应该做的是如下面的代码:

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

编辑:

你的食品类必须是这样的:

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

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

和Main类:

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

和原料类

public class Ingredient { 
    String name; 

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

导致'找不到符号' 编辑:nvm! – user3127055

0

创建一个类:

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

现在只是做:

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

你可以尝试这样做,虽然接口,这样我觉得是这样的:

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); 
} 

} 

现在是时候创建薯条类..

class Fries implements Food{ 

private String[] ingredients; 

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

} 

所以,打电话给它做这样的事情:

FoodSelection takeFood = new FoodSelection(); 

takeFood.selectFries(); 
+0

我会尝试,但我还不熟悉这些关键字 – user3127055

+0

它会起作用。顺便说一句在你的文章中的问题是由马科托普托尼克 – solvator