Jump to content

java exception issue


hamza

Recommended Posts

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!");
	}

	
}
Link to comment
https://forums.phpfreaks.com/topic/280799-java-exception-issue/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.