Word NullPointerException during program execution.

Catching an error on the products[i].getTitle() in the output method.

Most likely somewhere wrong with the links and the transfer of the array.

Please tell me where the problem is possible.

 import java.util.Scanner; public class Main { public static void main(String[] args) { Deal deal = input(); output(deal); } public static Deal input(){ Scanner sc = new Scanner(System.in); System.out.println("Введите имя покупателя"); String buyerName = sc.next(); Party buyer = new Party(); buyer.setName(buyerName); System.out.println("Введите имя продавца"); String sellerName = sc.next(); Party seller = new Party(); seller.setName(sellerName); Product[] products = new Product[3]; for(Product i : products){ i=inputProduct(); } Deal deal = new Deal(buyer, seller, products); return deal; } public static Product inputProduct(){ Scanner sc = new Scanner(System.in); System.out.println("Введите описание продукта"); String title=sc.next(); System.out.println("Введите цену продукта"); double price=sc.nextDouble(); System.out.println("Введите количество продукта"); int quantity=sc.nextInt(); Product product = new Product(); product.setPrice(price); product.setTitle(title); product.setQuantity(quantity); return product; } public static void output(Deal deal){ String nameOfSeller=deal.getSeller().getName(); String nameOfBuyer=deal.getBuyer().getName(); Product[] products=deal.getProducts(); System.out.println("Deal " +deal.getDate() +" Имя продавца " +nameOfSeller +" Имя покупателя " +nameOfBuyer +":"); for(int i=0; i<products.length; i++){ System.out.println("Product " +products[i].getTitle() +" Цена " +products[i].getPrice() +" Количество " +products[i].getQuantity() +" Общая цена за продукт " +products[i].getCost()); } System.out.println("Общая стоимость сделки "+deal.getSum()); } } public class Party { private String name; public String getName(){ return this.name; } public void setName(String name){ this.name=name; } } public class Product { private String title; private double price; private int quantity; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } public double getCost(){ return this.price*this.quantity; } } import java.util.Date; public class Deal { private Date date; private Party buyer; private Party seller; private Product[] products; public Deal(Party buyer,Party seller,Product[] products){ this.date=new Date(); this.buyer=buyer; this.seller=seller; this.products=products; } public double getSum(){ double rezult=0; for(Product i: this.products){ rezult+=i.getCost(); } return rezult; } public Party getBuyer(){ return this.buyer; } public Party getSeller(){ return this.seller; } public Date getDate(){ return this.date; } public Product[] getProducts(){ return this.products; } } 
  • It is not necessary to put such big programs in questions. Reproduce your problem in a simplified version of your program, discarding from it everything that does not apply to your question, and post it. Read en.stackoverflow.com/help/mcve - m. vokhm
  • And where you caught it, you did not find a single catch in the program. - 0xdb
  • I didn’t say it in terms of using try \ catch constructions, but in general there is an error. A little vaguely formulating the question) - Sergey

2 answers 2

In the input(...) method, and specifically, in this fragment:

 for(Product i : products){ i = inputProduct(); } 

i is a local loop variable, the change of which does not change the variable in the array. for each style loops are not intended to change data.

The solution is to use the usual for loop:

 for(int i=0; i<products.length; i++){ products[i] = inputProduct(); } 
  • Thank you, the problem was solved) - Sergey

for(Product i : products){ i = inputProduct(); }

This code is equivalent to

 for(idx = 0; idx < products.length; idx++){ Product i; i = products[idx]; i = inputProduct(); } 

And it should be like this

 for(idx = 0; idx < products.length; idx++){ Product i; i = inputProduct(); products[idx] = i; } 
  • Thanks, it helped) - Sergey