英文:
How do I pass a class reference from another class?
问题
我正在构建一个基本的初学者Java程序,但我一直遇到NullPointerException错误,似乎无法解决这个问题。
我有两个类 - User(用户)和Cart(购物车)。
User类包括以下代码中的Cart类:
package eCommerceApp;
import java.util.List;
import java.util.Collections;
import java.util.Comparator;
public class User extends Person {
public User(String name, String surname, int budget) {
super(name, surname, budget);
}
public Cart myCart;
public String getReceipt(List<Product> products) {
int sum = 0;
for (Product product : products) {
sum += product.getPrice();
}
return "Your total bill is " + sum + " rsd.";
}
public String getMostExpensiveItem(List<Product> products) {
Product product = Collections.max(products, Comparator.comparing(s -> s.getPrice()));
return "The most expensive item in your shopping cart is " + product.getName() + ".";
}
public String getCheapestItem(List<Product> products) {
Product product = Collections.min(products, Comparator.comparing(s -> s.getPrice()));
return "The cheapest item in your shopping cart is " + product.getName() + ".";
}
}
而Cart类如下:
package eCommerceApp;
import java.util.List;
public class Cart {
List<Product> products;
public Cart(List<Product> products) {
this.products = products;
}
public String getProducts() {
return this.products.toString();
}
public void setProducts(List<Product> products) {
this.products = products;
}
}
现在,当我在主类中输入**User.myCart.setProducts(list)**以将产品列表设置为我的User的购物车时,我会收到NullPointerException错误。我该如何解决这个问题?
英文:
I'm building a basic beginner Java program but I keep getting the NullPointerException error and I can't seem to solve the problem.
I have two classes - User and Cart.
The User class includes the Cart class with the following code :
package eCommerceApp;
import java.util.List;
import java.util.Collections;
import java.util.Comparator;
public class User extends Person {
public User(String name, String surname, int budget) {
super(name, surname, budget);
}
public Cart myCart;
public String getReceipt(List<Product> products) {
int sum = 0;
for (Product product : products) {
sum += product.getPrice();
}
return "Your total bill is " + sum + " rsd.";
}
public String getMostExpensiveItem(List<Product> products) {
Product product = Collections.max(products, Comparator.comparing(s -> s.getPrice()));
return "The most expensive item in your shopping cart is " + product.getName() + ".";
}
public String getCheapestItem(List<Product> products) {
Product product = Collections.min(products, Comparator.comparing(s -> s.getPrice()));
return "The cheapest item in your shopping cart is " + product.getName() + ".";
}
}
And the Cart Class :
import java.util.List;
public class Cart {
List<Product> products;
public Cart(List products) {
this.products = products;
}
public String getProducts() {
return this.products.toString();
}
public void setProducts(List products) {
this.products = products;
}
}
Now, when I want to set a list of products to my User's Cart by typing User.myCart.setProducts(list) in the main class, I get the NullPointerException error. How can I solve this?
答案1
得分: 1
为了在其他类中使用类,您需要像这样创建一个实例:
public Cart myCart = new Cart();
除非您的整个类都是静态的,这是可行的方式。但通常不推荐始终将类设为静态。
英文:
To use classes in other classes, you'll need to make an instance of them like so:
public Cart myCart = new Cart();
Unless your entire class is static, this is the way to go. But it is generally not good practice to make your classes static all the time.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论