-1

how to set attributes for array variable in java(OOP)? attributes of o[] could not be set within the for loop, why? Im trying to set name for o[0], but o[0].name doesnt work. why this happened? Also setter and getter doesnt work either, can you help me explain why can not set attributes in o[]?

import java.util.Scanner;

public class Orders {
    private String name;
    private double price;
    private int quantity;

    public void setName(String name){
        this.name= name;
    }
    public String getName(){
        return name;
    }
    public void setPrice(double price){
        this.price=price;
    }
    public double getPrice(){
        return price;
    }
    public int getQuantity(){
        return quantity;
    }
    public void setQuantity(int quantity){
        this.quantity=quantity;
    }

    public static void main(String[] args) {
        int num = 0;
        double sum=0;
        Scanner sc = new Scanner(System.in);
        System.out.println("how many rows of order: ");
        num = sc.nextInt();
        Orders[] o = new Orders[num];

        sc.nextLine();

        for(int i = 0;i<=o.length;i++){

            System.out.println("The name of the product: ");
            o[0].name=sc.nextLine();
            o[i].setName(sc.nextLine());
            System.out.println("Price of product: ");
            o[i].setPrice(sc.nextDouble());
            System.out.println("Quantity of product: ");
            o[i].setQuantity(sc.nextInt());
        }
        for(Orders a: o){
            System.out.println("Name: "+a.getName()+". Price: "+a.getPrice()
            +". Quantity :"+a.getQuantity());
            double totalprice= a.getQuantity()*a.getPrice();
            sum = sum + totalprice;
        }

        System.out.println("total price: "+sum);
    }
}
Komal12
  • 3,340
  • 4
  • 16
  • 25
J. Doe
  • 11
  • 4
  • Could you share the stacktrace or errors that you get from this code execution? – Daniel C. Aug 21 '17 at 00:15
  • 1
    "Why doesn't this work?" Because you did something wrong. If you would like more specific information about what you did wrong, please provide more specific information about how it's not working--such as the error message or incorrect output you're seeing. – ajb Aug 21 '17 at 00:24
  • Hi Daniel, Exception in thread "main" java.lang.NullPointerException, there is no problem find in eclipse but when i run it, it has some error, i dont understand why can not set value for array variable, like array[i].something – J. Doe Aug 21 '17 at 00:25
  • Hi ajb, Exception in thread "main" java.lang.NullPointerException, when i run in eclipse it shows this line, this is my first time to ask question by using stackoverflow, i dont know how to reply, can you see me? – J. Doe Aug 21 '17 at 00:31

1 Answers1

0

You need to initialize your o[i] object before assigning it to its attributes.
Your loop should terminate before reaching the length of the array since you start your iterator form ZERO. Your for loop should look like this:

for(int i = 0;i<o.length;i++){
        o[i] = new Orders();
        System.out.println("The name of the product: ");
        String name = sc.nextLine();
        o[i].setName(name);
        System.out.println("Price of product: ");
        double price = sc.nextDouble();
        o[i].setPrice(price);
        System.out.println("Quantity of product: ");
        int quantity = sc.nextInt();
        o[i].setQuantity(quantity);
        sc.nextLine();
        System.out.println("moveing to the next Order:");
    }

One more final note is to have useful names for your varaibles. This is extremely helpful when the code gets bigger and a good practice to have as a programmer.
Hope the code snippet works!

Maged Saeed
  • 1,784
  • 2
  • 16
  • 35
  • Hi Maged, after i add o[i] = new Orders(); inside the for loop and run in eclipse, it shows error message like this ----Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException; i dont know how to fix it – J. Doe Aug 21 '17 at 00:37
  • for(int i = 0;i – Maged Saeed Aug 21 '17 at 00:39
  • 1
    Hi Maged, THANK YOU!!!!! it works ,thanks a lot, im a freshman in programming, hahaha ill work hard! – J. Doe Aug 21 '17 at 00:43
  • Oh, that is really nice. @ J. Doe if this answer works for you, then please accept it. It is a good practice for both of us. Thanks in advance,, and welcome to the beautiful world of programming :) – Maged Saeed Aug 21 '17 at 00:45
  • 1
    yeah :DD have a nice day – J. Doe Aug 21 '17 at 00:47