Results 1 to 3 of 3
Thread: setter and getter
- 08-17-2013, 06:49 PM #1
Member
- Join Date
- Jul 2013
- Location
- india
- Posts
- 15
- Rep Power
- 0
setter and getter
package com.necre.oops;
public class StudentInfo {
private int rollNo;
private String name;
private String course;
private String dateOfBirth;
private String mobileNo;
private String contactNo;
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getMobileNo() {
return mobileNo;
}
public void setMobileNo(String mobileNo) {
this.mobileNo = mobileNo;
}
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
}
package com.necre.oops;
import java.util.Scanner;
public class Student {
static StudentInfo[] infos=new StudentInfo[5];
static Scanner scanner=new Scanner(System.in);
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.print("enter rollno:");
int rollno=scanner.nextInt();
infos[i].setRollNo(rollno);
System.out.println("enter name");
String name=scanner.next();
infos[i].setName(name);
System.out.println("enter mobile no:");
String mobile=scanner.next();
infos[i].setMobileNo(mobile);
System.out.println("enter date of birth");
String dob=scanner.next();
infos[i].setDateOfBirth(dob);
System.out.println("enter course");
String course=scanner.next();
infos[i].setCourse(course);
System.out.println("enter contact no:");
String contact=scanner.next();
infos[i].setContactNo(contact);
}
for (int i = 0; i < 5; i++) {
System.out.println(infos[i].getRollNo());
System.out.println(infos[i].getName());
System.out.println(infos[i].getContactNo());
System.out.println(infos[i].getMobileNo());
System.out.println(infos[i].getCourse());
System.out.println(infos[i].getDateOfBirth());
}
}
}
while executing it showing error like this..............why
enter rollno:5
Exception in thread "main" java.lang.NullPointerException
at com.necre.oops.Student.main(Student.java:14)
- 08-17-2013, 08:29 PM #2
Senior Member
- Join Date
- Mar 2013
- Location
- Greece
- Posts
- 183
- Rep Power
- 9
Re: setter and getter
first please put your code inside the [CODE ] [ /CODE] it's help to read it..
Second you should understand what the error message means.. java.lang.NullPointerException means that you havent initialize something and that's null
now let's check the line 14 your program hit error when you do this : infos[i].setRollNo(rollno);
so what is that?!? hmmm infos[i] supposed to be an item of the StudentInfo [] (Array) so the array has Objects so when you do infos[i] means take the first object of the array info .. but you didn't have create this object so how you call it? you should create the object first then add it to the array and finally call the methods..
here is the correct code :
Java Code:import java.util.Scanner; class StudentInfo { private int rollNo; private String name; private String course; private String dateOfBirth; private String mobileNo; private String contactNo; public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCourse() { return course; } public void setCourse(String course) { this.course = course; } public String getDateOfBirth() { return dateOfBirth; } public void setDateOfBirth(String dateOfBirth) { this.dateOfBirth = dateOfBirth; } public String getMobileNo() { return mobileNo; } public void setMobileNo(String mobileNo) { this.mobileNo = mobileNo; } public String getContactNo() { return contactNo; } public void setContactNo(String contactNo) { this.contactNo = contactNo; } } class Student { static StudentInfo[] infos = new StudentInfo[5]; static Scanner scanner=new Scanner(System.in); public static void main(String[] args) { for (int i = 0; i < 5; i++) { StudentInfo studentObject = new StudentInfo(); // we create a new Object infos[i] = studentObject; // then we add the object inside the Array System.out.print("enter rollno:"); int rollno = scanner.nextInt(); infos[i].setRollNo(rollno); // so now we take the object of the array and call he's method System.out.println("enter name"); String name=scanner.next(); infos[i].setName(name); System.out.println("enter mobile no:"); String mobile=scanner.next(); infos[i].setMobileNo(mobile); System.out.println("enter date of birth"); String dob=scanner.next(); infos[i].setDateOfBirth(dob); System.out.println("enter course"); String course=scanner.next(); infos[i].setCourse(course); System.out.println("enter contact no:"); String contact=scanner.next(); infos[i].setContactNo(contact); } for (int i = 0; i < 5; i++) { System.out.println(infos[i].getRollNo()); System.out.println(infos[i].getName()); System.out.println(infos[i].getContactNo()); System.out.println(infos[i].getMobileNo()); System.out.println(infos[i].getCourse()); System.out.println(infos[i].getDateOfBirth()); } } }
Last edited by ShadowWalker; 08-17-2013 at 08:40 PM.
- 08-18-2013, 02:59 AM #3
Member
- Join Date
- Jul 2013
- Location
- india
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Call remote getter/setter
By rcbandit in forum Advanced JavaReplies: 1Last Post: 06-26-2013, 03:46 PM -
How to add annotations to getter and setter property?
By athar in forum New To JavaReplies: 1Last Post: 02-05-2013, 03:34 PM -
getter and setter method help please!
By merdzins in forum New To JavaReplies: 2Last Post: 12-06-2010, 05:06 AM -
use of private in getter and setter
By katturv in forum New To JavaReplies: 1Last Post: 12-03-2010, 07:17 PM -
Constructors, Setter & Getter << need some explaining
By A.M.S in forum New To JavaReplies: 4Last Post: 01-01-2009, 12:03 PM
Bookmarks