Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By ShadowWalker

Thread: setter and getter

  1. #1
    rajeev is offline Member
    Join Date
    Jul 2013
    Location
    india
    Posts
    15
    Rep Power
    0

    Default 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)

  2. #2
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    112
    Rep Power
    0

    Default 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.
    rajeev likes this.

  3. #3
    rajeev is offline Member
    Join Date
    Jul 2013
    Location
    india
    Posts
    15
    Rep Power
    0

    Default Re: setter and getter

    thanx ShadowWalker..................

Similar Threads

  1. Call remote getter/setter
    By rcbandit in forum Advanced Java
    Replies: 1
    Last Post: 06-26-2013, 03:46 PM
  2. Replies: 1
    Last Post: 02-05-2013, 03:34 PM
  3. getter and setter method help please!
    By merdzins in forum New To Java
    Replies: 2
    Last Post: 12-06-2010, 05:06 AM
  4. use of private in getter and setter
    By katturv in forum New To Java
    Replies: 1
    Last Post: 12-03-2010, 07:17 PM
  5. Replies: 4
    Last Post: 01-01-2009, 12:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •