0

My code will not execute from line 29 onwaard. I am beginner in java and do not know about exception handling and can not handle the NULL pointer exception thrown. Is there a way other than exception handling or there is flaw in my code?

public static void run(int a){
        Scanner scanf = new Scanner(System.in);
    switch (a) {
            case 1:
                int i=0;
                int q=0;
                while(i!=5 || q!=-99999){
                System.out.println("Enter Student Name");
                String x = scanf.nextLine();
                student[i].setName(x);
                System.out.println("Enter Registration Number");
                String z = scanf.nextLine();
                student[i].set_RegistrationNo(z);
                System.out.println("Enter -99999 to exit");
                i++;
                 q =scanf.nextInt();
                }
                break;
            case 2:
                System.out.println("Which Record do you want to change?\n 1.Name 2.Registration Number");
                int y=scanf.nextInt();
                if(y==1){
                    System.out.println("Enter Student Name");
                    student[y].setName(null);
                    String x = scanf.nextLine();
                    student[y].setName(x);
                }
                else if(y==2)
                {
                    student[y].set_RegistrationNo(null);
                    System.out.println("Enter Student Registration Number");
                    String z=scanf.nextLine();
                    student[y].set_RegistrationNo(z);
                }
                question();
                break;
            case 3:
                System.out.println("Enter record you want to delete");
                int k=scanf.nextInt();
                student[k].setName(null);
                student[k].set_RegistrationNo(null);
                question();
                break;
            case 4:
                System.out.println("Enter record you want to view/search");
                int c=scanf.nextInt();
                System.out.println("Name is");
                student[c].getName();
                System.out.println("Registration number is");
                student[c].get_registrationNo();
                question();
                break;
            case 5:
                for(int r=0;r<5;r++){
                    System.out.printf("Name of Student %d\n",r);
                    student[r].getName();
                    System.out.printf("Registration Number of Student %d\n", r);
                    student[r].get_registrationNo();
                } 
                question();
                break;
            case 6:
                break;
        }
    }
}
Sajeel
  • 1
  • 1
  • 2
  • Quite simply, you're either trying to use an object that hasn't been initialized yet, or something else that is null and can't be to be used. Refer to the post you duplicated and you'll discover your error. – Drew Kennedy Mar 29 '16 at 19:21
  • Which one is line 29? I got a lonely "{" at that point. How do you initialize student array? – RubioRic Mar 29 '16 at 19:22
  • apologies i mean the input line in any of the cases – Sajeel Mar 31 '16 at 13:52

0 Answers0