Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Customer.getName()" because "cust[i]" is null

Viewed 27

I'm making a modified function in the main function. When I wanting retrieve all the data from the array object from the customer class to edit the selected details by declaring cust[i]. There seems to be an issue where something said cust[i] is null even using for loop to retrieve all array of object, What methods/tools can be used to determine the cause so that you stop the exception from causing the program to terminate prematurely?. How do I fix it?

         else if(choice == 2){
            int count = Customer.getCustomerCount();

            if(cust[0] !=null){
                System.out.println("================================");
                System.out.println("=           CUSTOMER MENU      =");
                System.out.println("================================");
                
                for(int i = 0;i < count ;  i++){
                    if(cust[i] != null)
                        System.out.println(i+1 + "\n" + cust[i] + "\n");
                }

                System.out.println("======================================");
                System.out.print("Please enter the name you wish to modify: ");
                String mname = scan.nextLine();

                for(int i = 0;i < count ;i++){
                    if(cust[i].getName() == mname){
                        System.out.println("===============================================");
                        System.out.println("= 1.Name                                      =");
                        System.out.println("= 2.Gender                                    =");
                        System.out.println("= 3.DOB                                       =");
                        System.out.println("= 4.Tel                                       =");
                        System.out.println("= 5.Email                                     =");
                        System.out.println("= 6.Password                                  =");
                        System.out.println("= 7.Exit modify menu                          =");
                        System.out.println("===============================================");

                        System.out.print("Please enter the category number you wish to modify: ");
                        int cNo = scan.nextInt();
                        scan.nextLine();

                        while(cNo < 1 || cNo>7){
                            System.out.print("Invalid input, please try again (1-7) > ");
                            cNo = scan.nextInt();
                            scan.nextLine();
                        }

                        if(cNo == 1){
                            System.out.println("\nOld name: " + cust[i].getName());
                            System.out.print("Please enter the new name: ");
                            String nName = scan.nextLine();
                            
                                cust[i].setName(nName);
                                
                                System.out.println("Customer's name successfully updated...\n");
                        }
                        
                        else if(cNo == 2){
                            System.out.println("\nOld Gender: " + cust[i].getGender());
                            System.out.print("Please enter the new gender: ");
                            char nGender = scan.next(".").charAt(0);
                            
                                cust[i].setGender(nGender);
                                
                                System.out.println("Customer's gender successfully updated...\n");
                        }

                        else if(cNo == 3){
                            System.out.println("\nOld DOB > " + cust[i].getDOB());
                            System.out.print("Please enter the new DOB: ");
                            String nDOB = scan.nextLine();
                            
                                cust[i].setDOB(nDOB);
                                
                                System.out.println("Customer's gender successfully updated...\n");
                        }

                        else if(cNo == 4){
                            System.out.println("\nOld contact number > " + cust[i].getTel());
                            System.out.print("Please enter the contact number: ");
                            String nTel = scan.nextLine();
                            
                            
                                cust[i].setTel(nTel);
                                
                                System.out.println("Customer's contact number successfully updated...\n");
                        }

                        else if(cNo == 5){
                            System.out.println("\nOld email > " + cust[i].getEmail());
                            System.out.print("Please enter the new email: ");
                            String nEmail = scan.nextLine();
                            
                                cust[i].setEmail(nEmail);
                                
                                System.out.println("Customer's email successfully updated...\n");
                        }

                        else if(cNo == 6){
                            System.out.println("\nOld password > " + cust[i].getPassword());
                            System.out.print("Please enter the new password: ");
                            String nPassword = scan.nextLine();
                            
                                cust[i].setPassword(nPassword);
                                
                                System.out.println("Customer's password successfully updated...\n");
                        }

                        else{
                            System.out.println();
                        }
                    }
                    else{
                        System.out.println("There is no customer in the database.");
                    }
                }
                
            }
0 Answers
Related