0

im learning Java and i was trying to create two arrays one for Employees and another for Employees positions and it ask the user to enter his name then compare it to the Employees name array . but i keep getting this error and i know its coming from the if statement inside( for ) but i cant figure out what cues it please help me to understand it . and thank you <3 here is the error im getting :

Enter Your Name to know Your Position : jjj No! Exception in thread "main" java.lang.NullPointerException at constructors.Aconstructors.main(Aconstructors.java:19)

package constructors;

import java.util.Scanner ;

public class Aconstructors {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in) ;
        String user ; 
        String [] empName , empPos ; 
        empName = new String [10] ;
        empName [0] = "Employ one" ;
        empPos = new String [10] ;
        empPos [0] = " Manager" ;

        System.out.println("Enter Your Name to know Your Position : ");
        user = scan.nextLine()  ;
        for (int i = 0 ; i <empName.length;i++ ) {

            if(empName[i].equals(user)) {
                System.out.println("yes !!");
            } else {
                System.out.println("No!");
            }


                }

}

        }
setget
  • 23
  • 5

1 Answers1

0

You are only adding one string to you arrays

consider putting this at the top of your loop

if (empName[i] == null) {
    System.out.println("This array element has no value");
    continue;
}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64