java.lang.NullPointerException菜单添加系统

时间:2017-12-10 23:25:27

标签: java arraylist methods add

我有一个菜单系统,我想使用addNewSupplier方法创建一个新供应商,然后使用printAllSuppliers方法与其他现有供应商一起打印新供应商的详细信息。但是当我尝试将其打印出来时,只显示现有的供应商,我想添加的新供应商显示了这个java.lang.NullPointerException。

package part01;
import java.util.ArrayList;
import java.util.Scanner;

public class mainMenu {

    private static Scanner mySc = new Scanner(System.in);
    private static ArrayList<Supplier> suppliers = new ArrayList<Supplier>();

    public static void main(String[] args) {
        Supplier s1 = systemData.sysData1();
        Supplier s2 = systemData.sysData2();
        Supplier s3 = systemData.sysData3();
        Supplier s4 = systemData.sysData4();
        suppliers.add(s1);
        suppliers.add(s2);
        suppliers.add(s3);
        suppliers.add(s4);
        runMenu();
    }

    private static void runMenu() {

        System.out.println("---MAIN MENU---");
        System.out.println();
        System.out.println("1: Print all Supplier Infomation");
        System.out.println("2: Add New Supplier");
        System.out.println("3: Add New Product");
        System.out.println("4: Add New Address");
        System.out.println("5: Shutdown Application");
        System.out.println();
        System.out.print("Please Enter Menu Option: ");

        while(!mySc.hasNextInt()) {
            mySc.nextLine();
            System.out.println("Must enter a valid number, please try again.");
            System.out.print("Please Enter Menu Option: ");
            System.out.println();
        }

        int menuChoice = mySc.nextInt(); mySc.nextLine(); 
        switch(menuChoice) {
        case 1: printAllSuppliers(); break;
        case 2: addNewSupplier(); break;
        case 3: addNewProduct(); break;
        case 4: addNewAddress(); break;
        case 5: systemExit(); break;
        default: 
            System.out.println("");
            System.out.println("Invalid input, please enter valid option.");
            System.out.println();
            runMenu();
        }
    }

    private static void printAllSuppliers() {
        System.out.println();
        System.out.println("---PRINT ALL SUPPLIER DETAILS---"); 
        System.out.println();

        for (Supplier eachS : suppliers) {
            eachS.printAllSupDe();  
            eachS.printProductList();
            System.out.println();           
        }
        System.out.print("--To return to the Main Menu please enter any key--");
        if (mySc.hasNextLine()) {
            mySc.nextLine();
            runMenu();
        }
    }

    private static void addNewSupplier() {

        System.out.println();
        System.out.println("---ADD NEW SUPPLIER---"); 
        System.out.println();

        System.out.println("Please enter your supplier name: ");
        String supName = mySc.nextLine();

        System.out.println("What product will you make?");
        String supMake = mySc.nextLine();

        System.out.println("Please enter your supplier code: ");
        double supCode = mySc.nextDouble();     

        mySc.nextLine();
        Address supAdd = addressValidation("Please enter address: ");           
        System.out.println();

        System.out.println("Where will your company be based in?");
        System.out.println();

        SupRegion[] regionVal = SupRegion.values();

        for (int i = 0; i < regionVal.length; i++) {
            System.out.println((i+1) + ": " + regionVal[i]);
        }

        System.out.println();
        System.out.println("Choose a value: ");
        SupRegion areas = null;

        int regionOption = mySc.nextInt()-1; mySc.hasNextLine();
        System.out.println();

        switch (regionOption) {
        case 0: areas = part01.SupRegion.UNITED_KINGDOM; break;
        case 1: areas = part01.SupRegion.OUTSIDE_EU; break;
        case 2: areas = part01.SupRegion.EUROPE; break;
        default:
            System.out.println("Please select a valid option");
        }

        ArrayList<Product> noStock = new ArrayList<Product>();

        Supplier s = new Supplier(supName, supMake, supAdd, areas ,supCode, noStock);
        suppliers.add(s); 

        mySc.nextLine();
        System.out.println("Your details have been successfully recorded!");
        System.out.println("Enter any value to return to the Main Menu: ");
        if (mySc.hasNextLine()) {
            mySc.nextLine();
            runMenu();
        }   
    }

    private static void addNewProduct() {

        System.out.println("");
        System.out.println("---ADD NEW PRODUCT---");
        System.out.println("");

        System.out.println("What type of product are you making?");
        String proMake = mySc.nextLine();

        System.out.println("What is name of the product?");
        String proModel = mySc.nextLine();

        System.out.println("Please enter the product code: ");
        int proCode = mySc.nextInt();

        System.out.println("How many are you selling?");
        int proQuantity = mySc.nextInt();

        System.out.println("Please enter the price of the product: ");
        int proPrice = mySc.nextInt(); mySc.nextLine();
        System.out.println();

        boolean proDiscontinued = false;

        Product p = new Product(proCode, proMake, proModel, proPrice, proQuantity, proDiscontinued);

        System.out.println("Which supplier is your product from?");
        for (int i = 0 ; i < suppliers.size(); i++) {
            System.out.println((i + 1) + ": ");
            suppliers.get(i).printAllSupDe(); 
        }
        System.out.println();
        System.out.println("Enter the supplier number: ");
        int supNo = mySc.nextInt(); mySc.nextLine();
        Supplier S = suppliers.get(supNo-1);
        S.getSupProduct().add(p);

        System.out.println();
        System.out.println("Your details have been successfully recorded!");
        System.out.println("Enter any value to return to the Main Menu: ");
        if (mySc.hasNextLine()) {
        mySc.nextLine();
        runMenu();
    }   
    }

    private static void addNewAddress() {

        System.out.println("");
        System.out.println("---ADD NEW ADDRESS---");
        System.out.println("");

        System.out.println("Please enter your address number: ");
        int addNum = mySc.nextInt();mySc.hasNextLine();

        System.out.println("Please enter your address town: ");
        String addTo = mySc.nextLine();mySc.hasNextLine();

        System.out.println("Please enter your address street: ");
        String addStr = mySc.nextLine();mySc.hasNextLine();

        System.out.println("Please enter your address postcode: ");
        String addPc = mySc.nextLine();mySc.hasNextLine();

        System.out.println("What country are you based in?");
        String addCo = mySc.nextLine();

        Address a = new Address(addNum,addTo,addStr,addPc,addCo);

        System.out.println("Pick a supplier to change address?");
        for (int i = 0 ; i < suppliers.size(); i++) {
            System.out.println((i + 1) + ": ");
            suppliers.get(i).printAllSupDe(); 
        }

        for (int i = 0; i < suppliers.size(); i++) {
            System.out.println("Enter the supplier number: ");
            int dealerNo = mySc.nextInt()-1; mySc.nextLine();
            Supplier S = suppliers.get(dealerNo);
            S.setSupAddress(a);

        System.out.println("");
        System.out.println("Your details have been successfully recorded!");
        System.out.println("------------------------------------------------------");
        System.out.println("Enter any value to return to the Main Menu: ");
        System.out.println();
        if (mySc.hasNextLine()) {
            mySc.nextLine();
            runMenu();
        }
        }
    }

    public static Address addressValidation(String string) {

        String add1;
        boolean addS= false;

        do {
            System.out.print(string);
            add1 = mySc.nextLine(); 
            if (add1.length() == 0) {
                System.out.println("No value has been entered");
            } else if (add1.length() > 7 || add1.length() < 7) {
                System.out.println("");
                System.out.println("Invalid input, input must have 7 characters.");
            } else {
                addS = true;
            }
        } while (!addS);
        return null;
        }

    private static void systemExit() {

        System.out.println();
        System.out.println("The application has been terminated."); 
        System.out.println();       

        System.exit(0);     
    }

}

package part01;

import java.util.ArrayList;

public class Supplier {
    private String supMake;
    private String supName;
    private Address supAddress;
    private SupRegion supRegion;
    private double supCode;
    private ArrayList<Product> supProduct = new ArrayList<Product>();

    public Supplier(String supMake, String supName, Address supAddress, SupRegion supRegion, double supCode,
            ArrayList<Product> supProduct) {
        super();
        this.supMake = supMake;
        this.supName = supName;
        this.supAddress = supAddress;
        this.supRegion = supRegion;
        this.supCode = supCode;
        this.supProduct = supProduct;
    }

    public void printAllSupDe() {
        System.out.println("Supplier information: " + "|" + supMake + "| " + supName + " operated in " + supAddress.getFullAddress());
        System.out.println("--------------------------------------------------------------------------------------------");
    }

    public void printProductList() {
        for(Product eachPro : supProduct) {
            eachPro.getProductDetails();
        }
    }


    public String getSupMake() {
        return supMake;
    }

    public void setSupMake(String supMake) {
        this.supMake = supMake;
    }

    public String getSupName() {
        return supName;
    }

    public void setSupName(String supName) {
        this.supName = supName;
    }

    public Address getSupAddress() {
        return supAddress;
    }

    public void setSupAddress(Address supAddress) {
        this.supAddress = supAddress;
    }

    public SupRegion getSupRegion() {
        return supRegion;
    }

    public void setSupRegion(SupRegion supRegion) {
        this.supRegion = supRegion;
    }

    public double getSupCode() {
        return supCode;
    }

    public void setSupCode(double supCode) {
        this.supCode = supCode;
    }

    public ArrayList<Product> getSupProduct() {
        return supProduct;
    }

    public void setSupProduct(ArrayList<Product> supProduct) {
        this.supProduct = supProduct;
    }

}

0 个答案:

没有答案