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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.