Jump to content


Photo

java exception issue

java

  • Please log in to reply
1 reply to this topic

#1 hamza

hamza

    Advanced Member

  • Members
  • PipPipPip
  • 320 posts

Posted 03 August 2013 - 08:31 AM

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

	
}


#2 requinix

requinix

    Transforming Moderator

  • Moderators
  • 6,128 posts
  • LocationWA

Posted 03 August 2013 - 09:16 PM

So how about mentioning where that exception is being thrown from?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com