0

I'm doing for my project voting management information system. There is a NullPointerException on line 146. Please help. int the case 3 in the line of if statement this if(!pangalan[i].equals(""))

  public static void main(String[] args) {
  
    Scanner inputUser=  new Scanner (System.in);// input from user
    
    Scanner choiceUser = new Scanner (System.in);// num
    
     boolean flag = true;
    int index =5;
    
   
    String pangalan[]= new String [8];
  
    String trabaho[]= new String [8];
    
    int votingCount []= new int [8];
    
   
    
    
    System.out.println("\t-----------------------------------------------"); 
    System.out.println("\t| Voting   2021 Management information system |");
    System.out.println("\t-----------------------------------------------"); 
    
    System.out.println("\t------|WELCOME TO 2021 ELECTION|--------\n");
    
    
  do{
   
   
       
           System.out.println("[1] Add");
           System.out.println ("[2] Edit");
           System.out.println("[3] Search/Read");
           System.out.println("[4] Delete");
           System.out.println("[5] Stop");
           System.out.print("\nEnter Your Choice :");
           int choice = choiceUser.nextInt();
   
   
   
           switch(choice){
           
               case 1: 
                   System.out.println("----Add Your Candidate-----");
           
                   System.out.print("Enter the Name of your Candidate:");
                    String name = inputUser.nextLine();
         
                    System.out.print("Enter The Position of Candidate:");
                    String position = inputUser.nextLine();
                    
                    System.out.print("Enter your Vote scale 1 to 10:");
                    int output= choiceUser.nextInt();
                  
                   
                    pangalan [index] = name;
                    trabaho [index] = position;
                     votingCount [index] =  output;
                   
                    index--;
                    System.out.println("Your Vote added succesful");
                    break;
                    
                    
                    
               case 2:
                   System.out.println("----Edit the Result---");
                  
                   System.out.println("Enter the Candidate Name:");
                   String find= inputUser.nextLine();
                 
                   for(int i= 0; i<index;i++) {
                       if(pangalan[i].equalsIgnoreCase(find)){
                   System.out.println("Candidate Found");
                   System.out.println("Candidate name:");
                   System.out.println ("Candidate Position:");
                   System.out.println ("Candidate Result:");
                   
                   
                                  
                   System.out.print("Enter new Candidate Name:");
                   String newuser= inputUser.nextLine();
                   
                   System.out.print("Enter new Candidate Position:");
                   String newjob=  inputUser.nextLine();
                   
                   System.out.print("Enter new Vote Scale");
                   int newscale= choiceUser.nextInt();
                   
                   System.out.print("Candidate added succesful!");
                   
                   
                   
                   pangalan[i]=  newuser;
                   trabaho[i] = newjob;
                   votingCount[i]= newscale;
                   
                   
                   
                   break;
                   }
                       if(i== index-1)
                           System.out.println("Candidate Not Found!");
           }
                    
               case 3:
                   System.out.println("----View the result-----");
                   
                   System.out.println("NAME\t\tPOSITION\t\tRESULT");
                   for (int i= 0; i<index; i++){
                      if(!pangalan[i].equals(""))
                       System.out.println(pangalan[i]+"\t\t"+trabaho[i]+"\t\t"+votingCount[i]);
                   }
                         
                    break;
                    
             case 4:
                   System.out.println("----Delete the Candidate---");
                  
                   System.out.println("Enter the Candidate Name:");
                   String delfind= inputUser.nextLine();
                 
                   for(int i= 0; i<index;i++) {
                       if(pangalan[i].equalsIgnoreCase(delfind)){
                 
                   pangalan[i]=  "";
                   trabaho[i] = "";
                   votingCount[i]= 0;
                   
                   System.out.print("Candidate added succesful!");
                   
                  
                   }
                       if(i== index-1)
                           System.out.println("Candidate Not Found!");
           }
        break;
              case 5:
                 flag=false;
                  
                  System.out.println("Pasensya kana, God Bless!.....");
                    break;
  


                
      
           }         
  
   }while(flag);
    
    
  }
  
  } 
    
  • I have edited out a bunch of stuff that is not relevant to your question and that is likely to provoke a negative response. I think ScaryWombat has identified the cause of your problem, and I have added a Q&A about how deal with NullPointerExceptions in general. The rest is up to you. – Stephen C Jan 12 '21 at 02:31
  • For future reference: 1. You should always include the complete stacktrace in a question asking for Java debug assistance. 2. Telling us that an error occurs at a particular line number is not very helpful. You need to >>identify<< the actual line in the source code. (In this case, we can't even find it by counting lines, 'cos you left out the start of the source file.) – Stephen C Jan 12 '21 at 02:34

0 Answers0