10 Replies - 297 Views - Last Post: 03 August 2013 - 05:45 PM Rate Topic: -----

#1 dreamincodehamza  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: -12
  • View blog
  • Posts: 330
  • Joined: 12-September 08

nullpointer exception issue

Posted 03 August 2013 - 12:50 PM

Newbie to java.
Getting nullpointerexception issue not sure how to fix it.
Please help here is my code.

When we search for roll number after selecting option 2 from menu.

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

	
}



Is This A Good Question/Topic? 0
  • +

Replies To: nullpointer exception issue

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: nullpointer exception issue

Posted 03 August 2013 - 12:55 PM

It's helpful to post the error you're getting. Then we often don't have to actually run what you've posted.

In fact, I'm not sure I can duplicate your error, so please post your error.
Was This Post Helpful? 0
  • +
  • -

#3 dreamincodehamza  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: -12
  • View blog
  • Posts: 330
  • Joined: 12-September 08

Re: nullpointer exception issue

Posted 03 August 2013 - 12:58 PM

View PostGregBrannon, on 03 August 2013 - 12:55 PM, said:

It's helpful to post the error you're getting. Then we often don't have to actually run what you've posted.

In fact, I'm not sure I can duplicate your error, so please post your error.


I hope you red my message completely i mentioned above

"Getting nullpointerexception issue not sure how to fix it."
Was This Post Helpful? 0
  • +
  • -

#4 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: nullpointer exception issue

Posted 03 August 2013 - 01:01 PM

View Postdreamincodehamza, on 03 August 2013 - 07:58 PM, said:

I hope you red my message completely i mentioned above

"Getting nullpointerexception issue not sure how to fix it."


A location is helpful to know where you are getting your NullPointerException :)
Was This Post Helpful? 0
  • +
  • -

#5 dreamincodehamza  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: -12
  • View blog
  • Posts: 330
  • Joined: 12-September 08

Re: nullpointer exception issue

Posted 03 August 2013 - 01:05 PM

View Postschutzzz, on 03 August 2013 - 01:01 PM, said:

View Postdreamincodehamza, on 03 August 2013 - 07:58 PM, said:

I hope you red my message completely i mentioned above

"Getting nullpointerexception issue not sure how to fix it."


A location is helpful to know where you are getting your NullPointerException :)/>



Please see below code line number 116 and 57.
Here it is pointing for error.

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];

					if (stud.getRollNo.equals(no)) {
						stud.output();
						return;
					}				
				
			}
		}
		
		System.out.println("Student is not found!");
	}
	
}


Was This Post Helpful? 0
  • +
  • -

#6 dreamincodehamza  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: -12
  • View blog
  • Posts: 330
  • Joined: 12-September 08

Re: nullpointer exception issue

Posted 03 August 2013 - 02:40 PM

Please anyone help me with this issue.
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: nullpointer exception issue

Posted 03 August 2013 - 02:47 PM

Okay, you have to make it as easy for us as possible (or less hard) to help you. How about running the program on your computer and copying and pasting the entire results of the run from your terminal output, including all prompts, user inputs, and error messages, so that we can see what you're seeing and can duplicate the error.
Was This Post Helpful? 0
  • +
  • -

#8 dreamincodehamza  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: -12
  • View blog
  • Posts: 330
  • Joined: 12-September 08

Re: nullpointer exception issue

Posted 03 August 2013 - 03:00 PM

View PostGregBrannon, on 03 August 2013 - 02:47 PM, said:

Okay, you have to make it as easy for us as possible (or less hard) to help you. How about running the program on your computer and copying and pasting the entire results of the run from your terminal output, including all prompts, user inputs, and error messages, so that we can see what you're seeing and can duplicate the error.


here is an error image of console.please have a look at image attached this message.

Attached image(s)

  • Attached Image

This post has been edited by dreamincodehamza: 03 August 2013 - 03:02 PM

Was This Post Helpful? 0
  • +
  • -

#9 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: nullpointer exception issue

Posted 03 August 2013 - 03:42 PM

Excellent! A simple copy and paste from the terminal window you showed would have been preferred, but this will work.

This is what is happening:

From the error message, bottom up:

at AddressBook.main(AddressBook.java:57)

The user chose #2, so addressBook.Search() is called.
(Note: method names should begin with lower-case letters.)

Then:

at AddressBook.Search(AddressBook.java:116)

in the method Search(), line 116 encounters a null pointer. Why?

Well, line 116 is:

if (stud.getRollNo.equals(no)) {

Taking each item in that line, which could be null? Not 'if', that's a Java key word, but what about 'stud'? 'stud' was just created at line 109, so that shouldn't be null. So what about stud.getRollNo? That's a possibility.

Follow this:

stud = new Student() calls the the default Student() constructor, but there isn't one. However, Student extends Person, so the default, no-argument, Person() constructor is called to create the stud object. (This happens automatically, whether there is a default Student() constructor or not.) Wait a minute . . . there is no default Person() constructor either. So, what must all of those Person and Student instance variables be by default? NULL!

So, stud.getRollNo is null, hence the NullPointerException.

How to fix it? Well, the method Search() is supposed to find an existing Student object at the index provided by the user at line 107. If the Search() method is looking for an existing Student object, why is a new Student object, stud, created in the Search() method? Shouldn't the Search() method just return the Student object located at the requested index? Why is an iteration of 'record' required? The index is known. And if there isn't a Student object at the requested index, then the Search() method should return an appropriate indication that the requested Student() object doesn't exist.

Make sense? If not, ask more questions.

Good luck!

This post has been edited by GregBrannon: 03 August 2013 - 03:48 PM

Was This Post Helpful? 3
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,204
  • Joined: 20-September 08

Re: nullpointer exception issue

Posted 03 August 2013 - 03:50 PM

                    //if (stud.getRollNo.equals(no)) {
                    if (stud.getRollNo().equals(no)) {


Make the above correction and get rid of that redundant and highly misleading variable with the same name as the (correct) method call
Was This Post Helpful? 1
  • +
  • -

#11 dreamincodehamza  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: -12
  • View blog
  • Posts: 330
  • Joined: 12-September 08

Re: nullpointer exception issue

Posted 03 August 2013 - 05:45 PM

View Postg00se, on 03 August 2013 - 03:50 PM, said:

                    //if (stud.getRollNo.equals(no)) {
                    if (stud.getRollNo().equals(no)) {


Make the above correction and get rid of that redundant and highly misleading variable with the same name as the (correct) method call


@g00se - You are right missing () before function getRollNo(). thank you

View PostGregBrannon, on 03 August 2013 - 03:42 PM, said:

Excellent! A simple copy and paste from the terminal window you showed would have been preferred, but this will work.

This is what is happening:

From the error message, bottom up:

at AddressBook.main(AddressBook.java:57)

The user chose #2, so addressBook.Search() is called.
(Note: method names should begin with lower-case letters.)

Then:

at AddressBook.Search(AddressBook.java:116)

in the method Search(), line 116 encounters a null pointer. Why?

Well, line 116 is:

if (stud.getRollNo.equals(no)) {

Taking each item in that line, which could be null? Not 'if', that's a Java key word, but what about 'stud'? 'stud' was just created at line 109, so that shouldn't be null. So what about stud.getRollNo? That's a possibility.

Follow this:

stud = new Student() calls the the default Student() constructor, but there isn't one. However, Student extends Person, so the default, no-argument, Person() constructor is called to create the stud object. (This happens automatically, whether there is a default Student() constructor or not.) Wait a minute . . . there is no default Person() constructor either. So, what must all of those Person and Student instance variables be by default? NULL!

So, stud.getRollNo is null, hence the NullPointerException.

How to fix it? Well, the method Search() is supposed to find an existing Student object at the index provided by the user at line 107. If the Search() method is looking for an existing Student object, why is a new Student object, stud, created in the Search() method? Shouldn't the Search() method just return the Student object located at the requested index? Why is an iteration of 'record' required? The index is known. And if there isn't a Student object at the requested index, then the Search() method should return an appropriate indication that the requested Student() object doesn't exist.

Make sense? If not, ask more questions.

Good luck!



Thank you very well explained.Problem solved.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1