REAM D
10
2021-10-13 18:45:13 작성 2021-10-13 19:00:14 수정됨
1
83

자바 NPE오류 질문드립니다.


구현하고자하는 상황은 이렇습니다.

이때 order안의 orderitem의 arraylist속의 orderitem의 beverage안의 name,size,cost를 불러오고싶은데 null오류가 뜹니다.

왜뜨는진 알겠는데 어떻게 고쳐야할지 감이 안잡혀서 도움을 구합니다. 

package cafe.order;
import cafe.menu.*;
import java.util.ArrayList;
import java.util.List;

public class Order {
    private List<OrderItem> items = new ArrayList<>();

    public void add(OrderItem orderItem) {items.add(orderItem);}

    public int cost(){
        int cost = 0;
        for(OrderItem orderItem : items){
            cost += orderItem.beverage.getBasePrice(); }
        return cost;
    }

    public boolean setSize(String name, String size){
        for(OrderItem orderItem: items){
            System.out.println(orderItem.GetName());
            if(orderItem.GetName().equals(name)){
                orderItem.setSize(size);
            }
        } return false;
    }
package cafe.menu;
public abstract class Beverage {
    public static final int TALL = 0;
    public static final int GRANDE = 1;
    public static final int VENTI = 2;

    String name;
    int basePrice;
    int size;

    public Beverage (String name, int bPrice, int size){
        this.name = name;
        this.basePrice = bPrice;
        this.size = size;
    }

    public String getName() { return name; }

    public int getBasePrice() { return basePrice; }

    public boolean setSize(int size) {
        this.size = size;
        return true;
    }

    @Override
    public String toString() {
        return " name="+ name + ", Price=" + basePrice + ", size=" + size + " " ;
    }

    public boolean setSize(String size){
        if ("TALL".equals(size)){
            setSize(TALL);}
        else if ("GRANDE".equals(size)){
            setSize(GRANDE);}
        else if ("VENTI".equals(size)) {
            setSize(VENTI);}
        return false;
    }
}


package cafe.order;
import cafe.menu.*;

public class OrderItem {
    Beverage beverage;
    int quantity;

    public OrderItem(Beverage ber, int quan) {
        this.beverage = ber;
        this.quantity = quan;
    }

    public int GetCost() {
        int cost = beverage.getBasePrice() * quantity;
        return cost;
    }

    public String GetName() {
        return beverage.getName();
    }

    public boolean setSize(String size) {
        return beverage.setSize(size);
    }

 //   @Override
  //  public String toString() {
  //      return "OrderItem{" +
  //              "beverage=" + beverage.toString() +
  //              ", quantity=" + quantity +
  //              '}';
 //   }
}


package cafe.order;
import cafe.menu.*;

import java.security.KeyStore;
import java.util.ArrayList;
import java.util.Scanner;

enum Command {ADD, COST, SIZE, PRINT, QUIT,INVALID};

public class OrderTest {

    private static Order order = new Order();

    public static void main(String[] args){
        final Scanner scanner = new Scanner(System.in);
        while (true){
            final Command cmd = getCommand(scanner);
            if (cmd == Command.QUIT){
                System.out.println("Bye"); break;
            }
            if (cmd == Command.INVALID){
                System.out.println("Invalid Operation!"); continue;
            }
            switch (cmd){
                case ADD :
                    final OrderItem item = createOderitem(scanner);
                    System.out.println(item);
                    order.add(item); break;
                case COST: System.out.printf("%.d%n", order.cost()); break;
                case SIZE:
                    final String name = scanner.next();
                    final String size = scanner.next();
                    if (!order.setSize(name, size))
                        System.out.println("The size does not supported!");
                    break;
                case PRINT: order.print(); break;
                default: break;
            }
        }
        scanner.close();
    }

    private static Command getCommand(final Scanner scanner){
        System.out.print("Enter Command String! ");
        final String operation = scanner.next();

        Command kind;
        try {
            kind = Command.valueOf(operation.toUpperCase());
        }catch ( IllegalArgumentException e){
            kind = Command.INVALID;
        }
        return kind;
    }

    private static OrderItem createOderitem(final Scanner scanner){
        final String type = scanner.next();
        final String name = scanner.next();
        final int quantity = scanner.nextInt();

        Beverage beverage = null;
        if ("Coffee".equals(type)){
            beverage = new Coffee(name);
        } else if ("Blended".equals(type)) {
            beverage = new Blended(name);
        } else  if ("Teavana".equals(type)) {
            beverage = new Teavana(name);
        }
        return new OrderItem(beverage, quantity);
    }
}


package cafe.menu;
public class Blended extends Beverage {

    private int baseAmount=GRANDE;

    public Blended (String name) {super(name,6300,GRANDE);}

    @Override
    public boolean setSize(int size){
        if(size==baseAmount) {
            super.setSize(size);
            return true;
        }
        return false;
    }
}


0
  • 답변 1

  • Mambo
    6k
    2021-10-13 19:50:35

    우선 createOderitem에서 beverage가 null이 아니란 보장이 없는 것 같네요. 함수 오타는 덤... :)

  • 로그인을 하시면 답변을 등록할 수 있습니다.