JAVA:自声明变量作为数组的索引

时间:2015-04-03 04:54:04

标签: java arrays netbeans indexing

我正在开发一个从客户接收交易的系统。程序实际运行但当我即将进入新事务时,会出现以下错误:

  

线程中的异常" main" java.lang.NullPointerException at   Java的机器.problem.pkg2.design.bakery(design.java:180)结果:1

我的代码是这样的:

    public static int choice1,choice2,counter,type,tnumber=1000,i=0,x=0,tno=0;
                public static char exit='y';
                public static String code;
                public static int []tcode;
                public static Integer []codet;
                public static String []tdate;
                public static int []ttype;
                public static String [][]order;
                public static int []total;
                public static String []timedelivery;
                public static float []payment;
                public static int []tablenumber;
                public static DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
                public static Date date = new Date();
                public static DateFormat timeFormat = new SimpleDateFormat("HH:mm");
                public static Date time = new Date();
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                void bakery()throws IOException{
                    mainmenu MM = new mainmenu();
                    System.out.println("\n+------------------------------------------------------+");
                    System.out.println("\t\tBu's Bakeshop\n[1] Product Menu\n[2] Transaction Menu\n[3] Exit\nEnter a number: ");
                    choice1 = Integer.parseInt(br.readLine());
                    if(choice1==1){
                        do{
                        System.out.println("\n+------------------------------------------------------+");
                        System.out.println("\t\tProduct Menu\n[1] View Product List\n[2] Search for a Product by Product Code\n[3] Back to Main Menu\nEnter a number: ");
                        choice2 = Integer.parseInt(br.readLine());
                        System.out.println("\n+------------------------------------------------------+");
                        if(choice2==1){
                            System.out.println("Product Code\tProduct Description\t\tAmount");
                            ...
        ...
                            System.out.println("   C1010\tChocolate Mousse\t\t290.00");
                        }
                        else if(choice2==2){
                            counter=0;
                            do{
                            System.out.println("Enter Product Code: ");
                            code = br.readLine();
                            if(code.length() == 5){
                            char code0 = code.charAt(0);
                            char code1 = code.charAt(1);
                            char code2 = code.charAt(2);
                            char code3 = code.charAt(3);
                            char code4 = code.charAt(4);
                            if(code0=='C' || code0=='c'){
                                if(code1=='1'){
                                    if(code2=='0'){
                                        if(code3=='0'){
                                            if(code4=='1'){
                                                System.out.println("\n+------------------------------------------------------+");
                                                ...
    ...
                            else{
                                System.out.println("\n+------------------------------------------------------+");
                                System.out.println("Product Code not found.");
                            }
                            System.out.println("\n+------------------------------------------------------+");
                            System.out.println("Try searching for Product Code again? [Y/N] ");
                            exit = br.readLine().charAt(0);
                        }while(exit=='Y' || exit=='y');
                            MM.menu();
                        }
                        else if(choice2==3){
                            MM.menu();
                        }
                        else{
                            System.out.println("Invalid input.");
                            counter = 1;
                        }
                    }while(counter==1);
                    }
                    else if(choice1==2){
                        System.out.println("\n+------------------------------------------------------+");
                        System.out.println("\t\tTransaction Menu\n[1] New Transaction\n[2] View Transaction\n[3] Search for a Transaction by Transaction Code\n[4] Back to Main Menu\nEnter a number: ");
                        choice2 = Integer.parseInt(br.readLine());
                        if(choice2==1){
                            int sum=0,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10;
                            counter=0;
                            tcode[i] = tnumber;
                            tdate[i] = dateFormat.format(date);
                            do{
                                System.out.println("Transaction Type:\n[1] Dine In\n[2] Take Out\n[3] Delivery");
                                ttype[i] = Integer.parseInt(br.readLine());
                                if(ttype[i]<1 && ttype[i]>3){
                                    System.out.println("Invalid input.\nPlease input [1] Dine In\n[2] Take Out\n[3] Delivery");
                                    counter=1;
                                }
                                else if(ttype[i]==1){
                                    tno++;
                                    tablenumber[i] = tno;
                                }
                                else if(ttype[i]==3){
                                    timedelivery[i] = timeFormat.format(time);
                                }
                            }while(counter==1);
                            do{
                            System.out.println("Product Code\tProduct Description\t\tAmount");
                            ...
...
                            System.out.println("Enter Product Code: ");
                            code = br.readLine();
                            if(code.length() == 5){
                            char code0 = code.charAt(0);
                            char code1 = code.charAt(1);
                            char code2 = code.charAt(2);
                            char code3 = code.charAt(3);
                            char code4 = code.charAt(4);
                            if(code0=='C' || code0=='c'){
                                if(code1=='1'){
                                    if(code2=='0'){
                                        if(code3=='0'){
                                            if(code4=='1'){
                                                order[i][x] = "Belgian Chocolate Bliss";
                                                x++;
                                                do{
                                                    System.out.println("How many Belgian Chocolate Bliss? ");
                                                    c1 = Integer.parseInt(br.readLine());
                                                    if(c1<0){
                                                        System.out.println("Input only positive numbers.");
                                                        counter=1;
                                                    }
                                                }while(counter==1);
                                                int price = 430;
                                                System.out.println("You bought "+c1+" Belgian Chocolate Bliss for "+(price*c1)+".00");
                                                total[i] = total[i] + (price*c1);
                                            }
                                            ...
...
                            else{
                                System.out.println("\n+------------------------------------------------------+");
                                System.out.println("Product Code not found.");
                            }
                            System.out.println("\n+------------------------------------------------------+");
                            System.out.println("Try searching for Product Code again? [Y/N] ");
                            exit = br.readLine().charAt(0);
                        }while(exit=='Y' || exit=='y');
                        do{
                            System.out.println("Total amount: "+total[i]+"\nAmount of Payment: ");
                            payment[i] = Float.parseFloat(br.readLine());
                            if(payment[i]==total[i]){
                                System.out.println("Received the exact amount. Thank you!");
                            }
                            else if(payment[i]>total[i]){
                                System.out.println("Change: "+(payment[i]-total[i])+". Thank you!");
                            }
                            else if(payment[i]<total[i]){
                                System.out.println("Insufficient Amount.");
                            }
                        }while(payment[i]<total[i]);
                        i++;
                        tnumber++;
                        }
                        else if(choice2==2){
                            if(i>0){
                            for(int h=0;h<i;h++){
                                for(int j=0;j<tcode.length;j++){
                                    System.out.println("Transaction Code: "+tcode[i]);
                                    for(j=0;j<tdate.length;j++){
                                        System.out.println("Transaction Date: "+tdate[i]);
                                        for(j=0;j<ttype.length;j++){
                                            if(ttype[i]==1){
                                                System.out.println("Transaction Type: Dine In\nTable Number: "+tablenumber[i]);
                                            }
                                            else if(ttype[i]==2){
                                                System.out.println("Transaction Type: Take Out");
                                            }
                                            else if(ttype[i]==3){
                                                System.out.println("Transaction Type: Delivery\nTime Called: "+timedelivery[i]);
                                            }
                                            for(j=0;j<order.length;j++){
                                                System.out.println("Products Bought: ");
                                                for(j=0;j<order[0].length;j++){
                                                    System.out.println(order[i][j]+"\n");
                                                }
                                                for(j=0;j<total.length;j++){
                                                    System.out.println("Total Amount: "+total[i]);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                            else{
                                System.out.println("No Transactions.");
                            }
                        }
                    }
                    else if(choice2==3){
                        System.out.println("Enter Transaction Code: ");
                        int tobesearched = Integer.parseInt(br.readLine());
                        List list = Arrays.asList(tcode);
                        if(list.contains(tobesearched)) {
                            int index = Arrays.asList(tcode).indexOf(tobesearched);
                            System.out.println("Transaction Number: "+tobesearched);
                            System.out.println("Transaction Date: "+tdate[index]);
                            if(ttype[index]==1){
                                System.out.println("Transaction Type: Dine In\nTable Number: "+tablenumber[index]);
                            }
                            else if(ttype[index]==2){
                                System.out.println("Transaction Type: Take Out");
                            }
                            else if(ttype[index]==3){
                                System.out.println("Transaction Type: Delivery\nTime Called: "+timedelivery[index]);
                            }
                            System.out.println("Products Bought: ");
                            for(int j=0;j<order[0].length;j++){
                                System.out.println(order[i][j]+"\n");
                            }
                            System.out.println("Total Amount: "+total[index]);
                        }
                        else{
                            System.out.println("Transaction Code not found.");
                        }
                    }
                    else{
                        System.out.println("\n+------------------------------------------------------+");
                        System.out.println("Invalid input.");
                    }
                    MM.menu();
                }
            }

似乎困境是代码<arrayname>[i]的困境。我怎么能解决这个问题?

0 个答案:

没有答案