简体   繁体   English

尝试调用方法时出现异常,该方法通过上述 class 的 object 从其他 class 调用方法

[英]Exception while trying to invoke method, which invokes method from other class through object of said class

In a program I made, I have a class canteen and a meal of the day class.在我制作的一个程序中,我有一个 class 食堂和一天的一餐 class。 In my meal of the day class there are several methods which I wanna use in my other class, implementing them in that classes' methods without any kind of inheritence.在我的一天 class 中,我想在我的其他 class 中使用几种方法,在没有任何继承的情况下在该类的方法中实现它们。

Therefore, I create objects of the meal of the day class so I can then use said methods from the canteen class.因此,我创建了当天用餐的对象 class,这样我就可以使用食堂 class 中的上述方法。 However, whenever I try to call those methods in my main method I always get the same exception no matter what method I use saying "Cannot invoke "cantina.PratoDia.METHODxyz()" because "this.p1" is null", despite the fact that I have a constructor attributing values to p1, p2 and p3.但是,每当我尝试在我的主方法中调用这些方法时,无论我使用什么方法说“无法调用”cantina.PratoDia.METHODxyz()”,因为“this.p1”为空”,我总是得到相同的异常,尽管事实上,我有一个构造函数将值分配给 p1、p2 和 p3。 Not sure what is wrong here so help's appreciated.不知道这里出了什么问题,因此感谢您的帮助。

Leaving both the classes below with constructors and a method from each that are supposed to interact with each other and also the main method.为下面的两个类留下构造函数和每个应该相互交互的方法以及主要方法。

public class Cantina {

    public PratoDia p1;
    public PratoDia p2;
    public PratoDia p3;

    
    public Cantina(){
        PratoDia p1 = new PratoDia("Carne", 5, 100);
        PratoDia p2 = new PratoDia("Peixe", 6, 60);
        PratoDia p3 = new PratoDia("Dieta", 4, 40);
    }

    
    public void atualizaPreco(int preço){
        p1.setPreço(preço);
        p2.setPreço(preço);
        p3.setPreço(preço);
    }
    
    public static void main(String[] args) {
        Cantina c = new Cantina();
        c.atualizaPreco(10);
    }
    
}

public class PratoDia {
    int preço;
    String nomePrato;
    int numPratos;
    int incPratos;
    int limitePratos;
    int valorTotal;
    int pagamento;

    public void setPreço(int preço){
        this.preço += preço/100;
    }
    
    public PratoDia(String nomePrato, int preço, int limitePratos){
        this.nomePrato = nomePrato;
        this.preço = preço;
        this.limitePratos = limitePratos;
        System.out.println(nomePrato + ": " + preço);
        numPratos = 0;
        CompraPratoDoDia();
    }
}
    

You defined the variables named p1, p2, and p3 in your global scope.您在全局 scope 中定义了名为 p1、p2 和 p3 的变量。 Inside your Cantina constructor, you create new variables, again with the names p1, p2 and p3.在您的 Cantina 构造函数中,您创建新变量,同样名称为 p1、p2 和 p3。 By writing the type of the variable before the name, you create a new variable with the same name, overwriting the variable in the parent scope.通过在名称前写入变量的类型,您可以创建一个具有相同名称的新变量,覆盖父 scope 中的变量。 Because of this, your global p1 is never actually assigned, and has a value of null.因此,您的全局 p1 从未实际分配过,其值为 null。 Simply remove the type indicators in the constructor, and it should work.只需删除构造函数中的类型指示符,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM