1

Good day to everyone

I'm new here and in Java, and this is one of first programs with 4 classes and simple methods.

In this prog we put our deal from keybord(we put buyer, seller names, title, price and quantity of products buyed). And so, after I input 2 deals and program must give output I get NullPointerException.

Application.java

package ua.lviv.my;
import java.util.Scanner;
public class Application {
  private static Deal [] deal = new Deal[2];
  public static void main(String[] args) {
    new Application().allActions();
  }
  void allActions(){
    input();
    System.out.println("======================");
    output();
  }
  public  void output(){
    for(int i=0; i<deal.length; i++){
      System.out.println("Buyer :" +deal[i].getBuyer().getName());
      System.out.println("Seller :" +deal[i].getSeller().getName());
      for (int j = 0; j < deal[i].getProducts().length; j++) {
        System.out.println("Buys " +deal[i].getProducts()[j].getTitle() +"for " +deal[i].getProducts()[j].getPrice() + " in quantity " +deal[i].getProducts()[j].getQuantity());
      }
    }
  }
  public void input(){
    for (int i=0; i<deal.length; i++){
      deal[i]=inputDeal();
    }
  }
  public Members inputMember(String msg){
    Members members = new Members();
    String memberName = keybordIn(msg);
    members.setName(memberName);
    return members;
  }
  public Product inputProduct(){
    Product product =new Product();
    String titleStrng = keybordIn("Enter product title");
    String priceStrng = keybordIn("Enter product price");
    String quantityStrng = keybordIn("Enter quantity");
    double price=Double.parseDouble(priceStrng);
    int quantity = Integer.parseInt(quantityStrng);
    product.setTitle(titleStrng);
    product.setPrice(price);
    product.setQuantity(quantity);
    product.getCost(price, quantity);
    return product;
  }
  public  Deal inputDeal(){
    Members buyer = inputMember("Enter buyer name :");
    Members seller =inputMember("Enter seller name :");
    Product [] products = new Product[2];
    for(int i=0; i<products.length; i++){
      products [i]=inputProduct();
    }
    Deal deal = new Deal(buyer, seller, products);
    return deal;
  }
  public  String keybordIn(String msg){
    System.out.println(msg);
    Scanner scan = new Scanner(System.in);
    String in = scan.next();
    return in;
  }
}

Deal.java

package ua.lviv.my;
import java.util.Date;
public class Deal {
  private Date date = new Date();
  private Members buyer;
  private Members seller;
  private Product[] products = new Product[2];
  public Deal(Members buyer, Members seller, Product[] products) {
  }
  public Date getDate() {
    return date;
  }
  public Members getBuyer() {
    return buyer;
  }
  public Members getSeller() {
    return seller;
  }
  public Product[] getProducts() {
    return products;
  }
  public double inTotal() {
    double summ = 0;
    for (int i = 0; i < products.length; i++) {
      summ += products[i].getCost(products[i].getPrice(),
        products[i].getQuantity());
    }
    return summ;
  }
}

Members.java

package ua.lviv.my;
public class Members {
  String name;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
}

package ua.lviv.my;
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(double price, int quantity){
    double cost = price*quantity;
    return cost;
  }
}
Constantin
  • 8,721
  • 13
  • 75
  • 126
Anaxagor
  • 44
  • 7
  • 3
    It would be help to know where abouts the exception occurs and see the stack trace as well... – MadProgrammer Jun 11 '14 at 08:09
  • Add stacktrace please. Without information we can`t help you. – Jens Jun 11 '14 at 08:10
  • 1
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Oleg Estekhin Jun 11 '14 at 08:15
  • Exception in thread "main" java.lang.NullPointerException at ua.lviv.my.Application.output(Application.java:22) at ua.lviv.my.Application.allActions(Application.java:14) at ua.lviv.my.Application.main(Application.java:8) – Anaxagor Jun 11 '14 at 08:43
  • sory for not adding it on start – Anaxagor Jun 11 '14 at 08:43
  • @Anaxagor which line is line 22 in Application.java ? – Jens Jun 11 '14 at 08:49

1 Answers1

1

Start by taking a look at the constructor for Deal...

public Deal(Members buyer, Members seller, Product[] products) {

}

You never assign any of the values passed via the constructor to the member fields, for example...

public Deal(Members buyer, Members seller, Product[] products) {
    this.buyer = buyer;
    this.seller = seller;
    this.products = products;
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366