this is my code..but I'm encountering an error of null pointer exception whenever I try to access private variables of "Category" class to "Product" class in the applyCoupon() method...
//main class
public class basicjava{
public static void main(String args[]){
Product product=new Product();
Category category=new Category();
category.setCategoryName("book");
product.applyCoupon();
}
}
//second class
public class Product {
private Category category;
Product(){
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public void applyCoupon(){
if (category.getCategoryName()=="book") {
System.out.println("book");
}
else {
System.out.println("page");
}
}
}
//third class
public class Category {
private String categoryName;
Category(){
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
}