NullPointerException 오류에 관해 질문드립니다.

package ayayay;
import java.util.Scanner;

public class Market {
    private static int array_length;
    private Scanner sc;
    private Drink[] drink;
    private Beer[] beer;
    private Fruit[] fruit;
    private Snack[] snack;
    private Ramen[] ramen; //클래스 배열 생성


    public Market() {
        sc=new Scanner(System.in);
        array_length=10;
        drink = new Drink[array_length];
        beer = new Beer[array_length];
        fruit = new Fruit[array_length];
        snack = new Snack[array_length];
        ramen = new Ramen[array_length];
    }

    void SetNamePriceProduct(String category, String...name) {
        int a,b,c,d,e; 
        if(category.toLowerCase().equals("drink")) {
            int n=name.length;
            for(int i=0;i<n;i++) {
                System.out.println("name :"+ name[i]);
                System.out.print("price : ");
                a = sc.nextInt();
                if(a>=500&&a<=2500) **drink[i] = new Drink(name[i],a);** //생성자로 값 넣기

                else {
                    while(true) {
                    System.out.println("!!inpute error");
                System.out.println("price :");
                a = sc.nextInt();
                if(a>=500&&a<=2500) {
                    drink[i] = new Drink(name[i],a); break;}
                }
                    }
            }
        }

}//void setname 끝나는 곳.


    public void print(Fruit[] f, Beer[] b, Drink[] d, Ramen[] r, Snack[] s) {
        System.out.println("******************Product************");
        print(f);
        print(b);
        print(d);
        print(r);
        print(s);
        System.out.println("*************************************");
    }

    public Drink[] getD() {

        return drink;
    }

    public Beer[] getB() {

        return beer;
    }

    public Fruit[] getF() {

            return fruit;

        }


    private void print(Fruit[] f) {
        System.out.println("FRUIT :");
        for(int j=0;j<fruit.length;j++) {
        System.out.println("       "+"name :"+fruit[j].getName()+", price :"+fruit[j].getPrice());
        }       
    }
    private void print(Beer[] b) {
        System.out.println("BEER :");
        for(int j=0;j<beer.length;j++) {
            System.out.println("       "+"name :"+beer[j].getName()+", price :"+beer[j].getPrice());
        }
    }
    private void print(Drink[] d) {
        System.out.println("DRINK :");
        for(int j=0;j<drink.length;j++) {
            System.out.println("       "+"name :"+drink[j].getName()+", price :"+drink[j].getPrice());
        }
    }




--------------------------------------------------------------
public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Market m= new Market();
        m.SetNamePriceProduct("DRInk", "Coke","Sprite","welchs","delmonte","cider","fanta");
        m.SetNamePriceProduct("RAmen", "sin","samyang","jin","6gyejang");
        m.SetNamePriceProduct("FrUit", "apple","mango","kiwi","banana","watermelon","melon","orange");
        m.SetNamePriceProduct("beEr", "cloud","hite","cass");
        m.SetNamePriceProduct("sNaCK", "jjanggu");
        m.print(m.getF(), m.getB(), m.getD(), m.getR(), m.getS());
    }

}

다른부분은 내용만 똑같은거라 짤랐고요. 중간에 setnameprice 메소드에서 drink[i] = new Drink(name[i],a); 이 내용이 있는데요 여기서 생성자 만들어서 객체를 생성한 것 아닌가요? 찾아보니까 nullpointerexception이 배열은 있는데 객체가 생성이 안된다고 하더라고요. 근데 객체생성이 안됬다고하니 답답합니다 ㅠㅠㅠ

1답변

  • 좋아요

    1

    싫어요
    채택취소하기

    new Drink(name[i],a); 라는 것은 예측한대로 생성자를 호출하는 것이 맞습니다.

    public Drink(String name, int price){}
    

    정도의 생성자가 있어야 합니다.

    nullpointerexception 라는 것은 아직 객체가 생성이 안되었는데 사용하려고 할 때 발생합니다.

    즉 쉽게 new 을 아직 안한객체를 사용하려고 했을 때 발생합니다.

    생성자 관련하여 필요한 부분만 넣어봤습니다.

    package ayayay;
    
    import java.util.Scanner;
    
    public class Market {
    
        class Drink {
            private String name;
            private int price;
    
            public Drink(String name, int price) {
                this.name = name;
                this.price = price;
            }
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public int getPrice() {
                return price;
            }
    
            public void setPrice(int price) {
                this.price = price;
            }
    
        }
    
        class Beer {
            private String name;
            private int price;
    
            public Beer(String name, int price) {
                super();
                this.name = name;
                this.price = price;
            }
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public int getPrice() {
                return price;
            }
    
            public void setPrice(int price) {
                this.price = price;
            }
        }
    
        class Fruit {
            private String name;
            private int price;
    
            public Fruit(String name, int price) {
                super();
                this.name = name;
                this.price = price;
            }
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public int getPrice() {
                return price;
            }
    
            public void setPrice(int price) {
                this.price = price;
            }
        }
    
        class Snack {
            private String name;
            private int price;
    
            public Snack(String name, int price) {
                super();
                this.name = name;
                this.price = price;
            }
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public int getPrice() {
                return price;
            }
    
            public void setPrice(int price) {
                this.price = price;
            }
        }
    
        class Ramen {
            private String name;
            private int price;
    
            public Ramen(String name, int price) {
                super();
                this.name = name;
                this.price = price;
            }
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public int getPrice() {
                return price;
            }
    
            public void setPrice(int price) {
                this.price = price;
            }
        }
    
        private static int array_length;
        private Scanner sc;
        private Drink[] drink;
        private Beer[] beer;
        private Fruit[] fruit;
        private Snack[] snack;
        private Ramen[] ramen; // 클래스 배열 생성
    
        public Market() {
            sc = new Scanner(System.in);
            array_length = 10;
            drink = new Drink[array_length];
            beer = new Beer[array_length];
            fruit = new Fruit[array_length];
            snack = new Snack[array_length];
            ramen = new Ramen[array_length];
        }
    
        void SetNamePriceProduct(String category, String... name) {
            int a, b, c, d, e;
            if (category.toLowerCase().equals("drink")) {
                int n = name.length;
                for (int i = 0; i < n; i++) {
                    System.out.println("name :" + name[i]);
                    System.out.print("price : ");
                    a = sc.nextInt();
                    if (a >= 500 && a <= 2500)
                        drink[i] = new Drink(name[i], a); // 생성자로 값 넣기
    
                    else {
                        while (true) {
                            System.out.println("!!inpute error");
                            System.out.println("price :");
                            a = sc.nextInt();
                            if (a >= 500 && a <= 2500) {
                                drink[i] = new Drink(name[i], a);
                                break;
                            }
                        }
                    }
                }
            }
    
        }// void setname 끝나는 곳.
    
        public void print(Fruit[] f, Beer[] b, Drink[] d, Ramen[] r, Snack[] s) {
            System.out.println("******************Product************");
            print(f);
            print(b);
            print(d);
            print(r);
            print(s);
            System.out.println("*************************************");
        }
    
        public Drink[] getD() {
    
            return drink;
        }
    
        public Beer[] getB() {
    
            return beer;
        }
    
        public Fruit[] getF() {
    
            return fruit;
    
        }
    
        public Ramen[] getR() {
    
            return ramen;
    
        }
    
        public Snack[] getS() {
    
            return snack;
    
        }
    
        private void print(Fruit[] f) {
            System.out.println("FRUIT :");
            for (int j = 0; j < fruit.length; j++) {
                System.out.println("       " + "name :" + fruit[j].getName() + ", price :" + fruit[j].getPrice());
            }
        }
    
        private void print(Beer[] b) {
            System.out.println("BEER :");
            for (int j = 0; j < beer.length; j++) {
                System.out.println("       " + "name :" + beer[j].getName() + ", price :" + beer[j].getPrice());
            }
        }
    
        private void print(Ramen[] d) {
            System.out.println("Ramen :");
            for (int j = 0; j < drink.length; j++) {
                System.out.println("       " + "name :" + drink[j].getName() + ", price :" + drink[j].getPrice());
            }
        }
    
        private void print(Snack[] d) {
            System.out.println("Snack :");
            for (int j = 0; j < drink.length; j++) {
                System.out.println("       " + "name :" + drink[j].getName() + ", price :" + drink[j].getPrice());
            }
        }
    
        private void print(Drink[] d) {
            System.out.println("DRINK :");
            for (int j = 0; j < drink.length; j++) {
                System.out.println("       " + "name :" + drink[j].getName() + ", price :" + drink[j].getPrice());
            }
        }
    }
    
    
    • 진짜 매번 감사해요ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ 김영태 2018.5.14 11:31

ᕕ( ᐛ )ᕗ
로그인이 필요합니다

작성한 답변에 다른 개발자들이 댓글을 작성하거나 댓글에 좋아요/싫어요를 할 수 있기 때문에 계정을 필요로 합니다.