hamza Posted August 3, 2013 Share Posted August 3, 2013 Hi all , i am newbie in java. i am getting error not sure about this when you select option two after running this code. this error will occur please help Error:exception in thread “main” Java.lang.nullPointerException error? here is my code import java.util.*; class Person { String name; int phoneNumber; } class Student extends Person { String rollNo; double CGPA; public Object getRollNo; public void input() { System.out.print("Enter rollNo and CGPA: "); Scanner in = new Scanner(System.in); rollNo = in.next(); CGPA = in.nextFloat(); } public void output() { System.out.println(" Student Details " ); System.out.println("\n============" ); System.out.println("Roll No: " + rollNo + "\nCGPA: " + CGPA); } public String getRollNo() { return rollNo; } } public class AddressBook { public static void main(String args[]) { int choice = 3; AddressBook addressBook= new AddressBook(3); System.out.println("\n1.Press 1 to Add Contact."); System.out.println("\n2.Press 2 to Search."); System.out.println("\n3.Exit."); System.out.println("\n=========================================="); System.out.print("Enter your choice: "); Scanner in = new Scanner(System.in); int choice1 = in.nextInt(); while (choice1 != 3) { //try { if (choice1 == 1) { addressBook.AddContact(); } else if (choice1 == 2) { addressBook.Search(); } else if (choice1 == 3) { System.out.print("Exting..."); } else { System.out.print("Please enter a valid menu option."); } /*} catch (ArrayIndexOutOfBoundsException e) { System.out.print("Please enter a valid menu option."); } */ System.out.println("\n1.Press 1 to Add Contact."); System.out.println("\n2.Press 2 to Search."); System.out.println("\n3.Exit."); System.out.println("\n=========================================="); System.out.print("Enter your choice: "); choice1 = in.nextInt(); } } int sizeOfAddressBook; int currentRecord; //Person[] record; // <-- Defined a variable which will be an array of Person objects Person[] record = new Person[10]; AddressBook(int size) { sizeOfAddressBook = size; for (int iter = 0; iter < sizeOfAddressBook; iter++) { record[iter] = new Person(); // error } currentRecord = 0; } void AddContact() { try { Student student = new Student(); student.input(); record[currentRecord++] = student; } catch(ArrayIndexOutOfBoundsException e1) { System.out.println("No more free place exist in array record!"); } } void Search() { System.out.print("Enter rollNo: "); Scanner in = new Scanner(System.in); String no = in.next(); Student stud = new Student(); // me for (int iter = 0; iter < sizeOfAddressBook; iter++) { if (record[iter] instanceof Student) { stud = (Student) record[iter]; try{ if (stud.getRollNo.equals(no)) { stud.output(); return; } } catch(NullPointerException e) { e.printStackTrace(); System.out.println("exception details" + e); } } } System.out.println("Student is not found!"); } } Quote Link to comment https://forums.phpfreaks.com/topic/280799-java-exception-issue/ Share on other sites More sharing options...
requinix Posted August 4, 2013 Share Posted August 4, 2013 So how about mentioning where that exception is being thrown from? Quote Link to comment https://forums.phpfreaks.com/topic/280799-java-exception-issue/#findComment-1443391 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.