0

I am working on a system that receives transactions from customer. The program actually runs but when I am about to enter a new transaction, the error below occurs:

Exception in thread "main" java.lang.NullPointerException at machine.problem.pkg2.design.bakery(design.java:180) at Java Result: 1

My code is something like this:

    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();
                }
            }

It seems like the dilemma are those with the code <arrayname>[i]. How am I able to solve this issue?

  • 1
    http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – user2864740 Apr 03 '15 at 05:01
  • Neither "self-declared" or "array" change the problem/solution. Please read through the linked question. – user2864740 Apr 03 '15 at 05:02
  • As a wild guess "br.readLine()" may be causing this. Have you initialised br? something like br = new BufferedReader(); –  Apr 03 '15 at 05:07
  • Please show us all of the code. Not just a few random lines. The problem may well be somewhere other than here. Also, identifying which line the error occurred on - which one line 180 is - is basically mandatory. – Dawood ibn Kareem Apr 03 '15 at 05:09
  • I'm sorry I forgot to include that line 180 is tcode[i] = tnumber; Yes, I already initialized BufferedReader. – Kenneth Gutierrez Apr 03 '15 at 05:10
  • There you go, sir @DavidWallace. I placed the whole code – Kenneth Gutierrez Apr 03 '15 at 05:16
  • 1
    @DavidWallace did you have to ask for ALL of the code? Ken please cut that down. It's so big I can't even add the proper indentation for code. – candied_orange Apr 03 '15 at 05:16
  • Initialise tcode[]. tcode = new int[someNumber]; –  Apr 03 '15 at 05:16
  • Sorry for that @CandiedOrange. There, I shortened the code :3 – Kenneth Gutierrez Apr 03 '15 at 05:19
  • @CandiedOrange What's the problem exactly? At least this way, Kenneth got his answer quickly - thanks to stacksonstacks, who got there before I did. – Dawood ibn Kareem Apr 03 '15 at 05:19
  • http://stackoverflow.com/questions/1938101/how-to-initialize-an-array-in-java – Naman Gala Apr 03 '15 at 05:19
  • Kenneth, as well as initialising `tcode` as @stacksonstacks suggested, you'll also need to initialise `tdate`, `ttype` and possibly others. In general, you can't just go assigning the elements of an array that you haven't created yet. – Dawood ibn Kareem Apr 03 '15 at 05:22
  • @user2864740 Agree. The linked post explains how someone can fix their NullPointerException themselves. This is a skill you need to learn if you want to be a Java developer. StackOverflow can't solve every individual run-of-the-mill NullPointerException, it's not the place for it. – Erwin Bolwidt Apr 03 '15 at 05:22
  • @DavidWallace the problem is StackOverflow would like this question to help more people then Kenneth Gutierrez. So it's really nice when the question is readable and focused on the real problem. – candied_orange Apr 03 '15 at 05:26
  • 1
    I would suggest to use `List` instead of `Array` in your code. At-least you can dynamically add new elements and will not have to worry about size. – Naman Gala Apr 03 '15 at 05:27
  • If we're tossing out random suggestions: read *clean code* by Robert Martin. He'll show you how to keep your methods short and debugable. – candied_orange Apr 03 '15 at 05:31
  • Thanks everyone especially @stacksonstacks & Sir @DavidWallace! My program worked well now :) – Kenneth Gutierrez Apr 03 '15 at 05:52

0 Answers0