Java 我的for循环中出现空指针异常

Java 我的for循环中出现空指针异常,java,loops,for-loop,nullpointerexception,Java,Loops,For Loop,Nullpointerexception,在我的代码中有一个“空指针异常”,但我似乎不明白为什么。我已将对象添加到构造函数中的ArrayList中。当我尝试运行该方法时,我得到一个空指针异常。任何帮助都将不胜感激 私人ArrayList产品 //PRODUCTS CLASS CONSTRUCTOR Products(){ ArrayList<ProductsClass> products = new ArrayList<ProductsClass>(); //Declaration and ins

在我的代码中有一个“空指针异常”,但我似乎不明白为什么。我已将对象添加到构造函数中的ArrayList中。当我尝试运行该方法时,我得到一个空指针异常。任何帮助都将不胜感激

私人ArrayList产品

//PRODUCTS CLASS CONSTRUCTOR

Products(){ 


    ArrayList<ProductsClass> products = new ArrayList<ProductsClass>(); //Declaration and instantiation of ArrayList

    products.add(new ProductsClass("Bread", 0.65));
    products.add(new ProductsClass("Milk", 2.00));
    products.add(new ProductsClass("Pasta", 3.35));
    products.add(new ProductsClass("Milk chocolate bar", 0.75));
    products.add(new ProductsClass("Dark Rum", 25.00));

    ProductsClass p = products.get(3);
    System.out.print(p.getItemName() + ""); //Testing to see if works#
    System.out.print("");
    System.out.print(" " + p.getItemPrice());

}

public void findMatchingItem() {

    for (ProductsClass product : products) {

    System.out.print(product);

    }
}
//产品类构造函数
产品(){
ArrayList products=new ArrayList();//ArrayList的声明和实例化
添加(新产品类别(“面包”,0.65));
添加(新产品类别(“牛奶”,2.00));
添加(新产品类别(“意大利面”,3.35));
添加(新产品类别(“牛奶巧克力”,0.75));
添加(新产品类别(“深色朗姆酒”,25.00));
ProductsClass p=products.get(3);
System.out.print(p.getItemName()+“”);//测试是否有效#
系统输出打印(“”);
System.out.print(“+p.getItemPrice());
}
public void findMatchingItem(){
对于(产品类别产品:产品){
系统输出打印(产品);
}
}

您需要在构造函数外部声明products ArrayList,使其成为实例变量

private ArrayList<ProductsClass> products

Products(){ 


    products = new ArrayList<ProductsClass>(); //Declaration and instantiation of ArrayList

    products.add(new ProductsClass("Bread", 0.65));
    products.add(new ProductsClass("Milk", 2.00));
    products.add(new ProductsClass("Pasta", 3.35));
    products.add(new ProductsClass("Milk chocolate bar", 0.75));
    products.add(new ProductsClass("Dark Rum", 25.00));

    ProductsClass p = products.get(3);
    System.out.print(p.getItemName() + ""); //Testing to see if works#
    System.out.print("");
    System.out.print(" " + p.getItemPrice());

}
私有ArrayList产品
产品(){
products=new ArrayList();//ArrayList的声明和实例化
添加(新产品类别(“面包”,0.65));
添加(新产品类别(“牛奶”,2.00));
添加(新产品类别(“意大利面”,3.35));
添加(新产品类别(“牛奶巧克力”,0.75));
添加(新产品类别(“深色朗姆酒”,25.00));
ProductsClass p=products.get(3);
System.out.print(p.getItemName()+“”);//测试是否有效#
系统输出打印(“”);
System.out.print(“+p.getItemPrice());
}

您在构造函数中定义了ArrayList。但是您正在访问一个方法内部。在此方法内访问不是全局的。将其作为全局变量移动

ArrayList<ProductsClass> products;

Products(){ 


    products = new ArrayList<ProductsClass>(); //Declaration and instantiation of ArrayList

    products.add(new ProductsClass("Bread", 0.65));
    products.add(new ProductsClass("Milk", 2.00));
    products.add(new ProductsClass("Pasta", 3.35));
    products.add(new ProductsClass("Milk chocolate bar", 0.75));
    products.add(new ProductsClass("Dark Rum", 25.00));

    ProductsClass p = products.get(3);
    System.out.print(p.getItemName() + ""); //Testing to see if works#
    System.out.print("");
    System.out.print(" " + p.getItemPrice());

}

public void findMatchingItem() {

    for (ProductsClass product : products) {

    System.out.print(product);

    }
}
ArrayList产品;
产品(){
products=new ArrayList();//ArrayList的声明和实例化
添加(新产品类别(“面包”,0.65));
添加(新产品类别(“牛奶”,2.00));
添加(新产品类别(“意大利面”,3.35));
添加(新产品类别(“牛奶巧克力”,0.75));
添加(新产品类别(“深色朗姆酒”,25.00));
ProductsClass p=products.get(3);
System.out.print(p.getItemName()+“”);//测试是否有效#
系统输出打印(“”);
System.out.print(“+p.getItemPrice());
}
public void findMatchingItem(){
对于(产品类别产品:产品){
系统输出打印(产品);
}
}
中的


你的代码是如何编译的?您是否已将构造函数外部的成员变量产品声明为:

    ArrayList<ProductsClass> products;
ArrayList产品;
在方法/循环内创建的任何引用都是活动的/只能在方法/循环内访问。所以发生的事情是,即使products对象被创建、初始化和赋值,这和成员变量products是两件不同的事情。成员变量未初始化,因此引发空指针异常


您需要将产品列表声明为成员变量。并在构造函数中初始化它。

它不是一个全局变量,它只有局部作用域。请包括
NullPointerException
的堆栈跟踪。您有一个名为
Products
的类和另一个名为
ProductsClass
?这不是一个好的命名方法。而且,如果他不希望对象是全局的,他将不得不将其传递给其他人。然而,这只是一个假设,根本没有写下你的答案。我假设他的
findMatchingItem()
方法也在
Products
类中,如果是这样的话,他就不需要传递它了。哦,是的,这是真的,完全忘记了你把它放在private中。所以没有其他类的全局访问权限。
ArrayList<ProductsClass> products = new ArrayList<ProductsClass>();

Products() {
    ... //rest here
}
ArrayList<ProductsClass> products;

Products() {
    products = new ArrayList<ProductsClass>();
    ... //rest here
}
public Products() {
    ...
}
    ArrayList<ProductsClass> products;