Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: how to implement method to add new Student and display it

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default how to implement method to add new Student and display it

    hi,

    I am at the beginning of my assignment and I have some problems, here is my code:

    import java.io.*;
    import java.util.*;
     
    public class StudentDatabase
    {
    	static int STUDENT_SIZE = 20;
        Student[] students = new Student[STUDENT_SIZE];
        int length = 0; //the current size of the array
    	Scanner scan = new Scanner(System.in);
     
    	public static void main(String[] args)
    	{
     
    		Menu menu = new Menu();
    		StudentDatabase aStudentDatabase=null;
     
     
    		do{
    			menu.showMenu();
    			int aChoice = 0;
    			aChoice=menu.choice();
    			while(aChoice==0)
    			{
    				System.err.println ( "Unrecognized option, please select again" );
    				menu.showMenu();
    				aChoice=menu.choice();
    			}
     
    			if(aStudentDatabase==null)
    			{
    				aStudentDatabase = new StudentDatabase();
    			}
    			switch(aChoice)
    			{
     
    				case 1:
    				aStudentDatabase.addStudent();
    				break;
     
    				case 2:
    				break;
     
    				case 3:
    				break;
     
    				case 4:
    				break;
     
    				case 5:
    				break;
     
    				case 6:
    					aStudentDatabase.displayStudentDetails();
    				break;
     
    				case 7:
    				break;
     
    				case 8:
    				System.exit(0);
    				break;
    			}
    		} while(true);
        }
     
     
        public boolean Start()
        {
    		return true;
        }
     
    	public void addStudent()
    	{
    	   	students[length++] = new Student();
    	}
     
        public boolean deleteStudent()
        {
    		return true;
        }
     
        public boolean addModule()
        {
    		return true;
        }
     
        public boolean deleteModule()
        {
    		return true;
        }
     
        public boolean assignModule()
        {
    		return true;
        }
     
        public void displayStudentDetails()
        {
    		System.out.println("Student Name:" + student.getFirstName() +" "+ student.getLastName() + " Enrollment on:" + student.getEnrollmentDate());
        }
     
        public boolean displayModuleDetails()
        {
    		return true;
        }
     
     
    }

    import java.io.*;
    import java.util.*;
     
    public class Menu
    {
    	// Scanner for user input
       public Scanner scan;
       public int choice;
     
       	public void showMenu()
    	{
    		System.out.println("*****************************");
    		System.out.println("Welcome to the Student Database!");
    	   	System.out.println ( "***********************************" );
    	   	System.out.println ( "*************MAIN MENU*************" );
    	   	System.out.println ( "***********************************" );
           	System.out.println ( "Select (1) to Add Student\nSelect (2) to Delete Student\nSelect (3) to Add Module\nSelect (4) to Delete Module\nSelect (5) to Assign Student to Module\nSelect (6) to Display Student Details\nSelect (7) to Display Module Details\nSelect (8) to Exit Program\n");
    	}
     
       	public int choice()
       	{
     
       		scan = new Scanner(System.in);
     
       		System.out.print("Please select an option: ");
     
       		//Read the input from the user
       		choice = scan.nextInt();
     
       		//Return the user's choice
       		return choice;
       	}
    }

    import java.io.*;
    import java.util.*;
     
    public class Student
    {
      private String firstName;
      private String lastName;
      private String enrollmentDate; // better would be to use util.Date not String ??
      private Scanner scan;
     
      // default constructor - initializes default attr values
    	public Student()
       	{
    		scan = new Scanner(System.in);
    		System.out.print("Student Name:");
     
    		firstName = scan.next();
     
    		System.out.print("Student Surname:");
     
    		lastName = scan.next();
     
    		System.out.print("Enrollment Date:");
     
    		enrollmentDate = scan.next();
       	}
     
        public Student(String newFirstName, String newLastName, String newEnrollmentDate)
        {
          this.firstName = newFirstName;
          this.lastName = newLastName;
          this.enrollmentDate = newEnrollmentDate;
        }
     
    //setters
    	public void setFirstName (String newFirstName)
    	{
    		firstName = newFirstName;
    	}
     
    	public void setLastName (String newLastName)
    	{
    		lastName = newLastName;
    	}
     
    	public void setEnrollmentDate (String newEnrollmentDate)
    	{
    		enrollmentDate = newEnrollmentDate;
    	}
     
    //getters
        public String getFirstName()
        {
          return firstName;
        }
     
        public String getLastName()
        {
          return lastName;
        }
     
        public String getEnrollmentDate()
        {
          return enrollmentDate;
        }
    }

    1. How I can implement addStudent method in the StudentDatabase class?
    2. How to implement the constructor in the Studnet class?
    3. How I can implement displayStudentDetails method?

    Can you drop me few tips here please?

    thanks
    E


  2. #2
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: how to implement method to add new Student and display it

    What is the problem you're having with the code, the methods and constructor you mention are in there. Are they not working as expected?

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to implement method to add new Student and display it

    well... I am trying to figure out how to increase number of students and add another student to the array, for example:

    When I will choose "Add Student" from Menu, I should be able to add a student to Array.

    Then I should be able to display it somehow, program is compiling but when I add a student and then I want to display it, it shows:

    Please select an option: 6
    Exception in thread "main" java.lang.NullPointerException
    at StudentDatabase.displayStudentDetails(StudentDatab ase.java:100)
    at StudentDatabase.main(StudentDatabase.java:54)

    Process completed.

  4. #4
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: how to implement method to add new Student and display it

    With this line:
     System.out.println("Student Name:" + student.getFirstName() +" "+ student.getLastName() + " Enrollment on:" + student.getEnrollmentDate());
    Your calling the Student methods on variable student but you haven't initialised it anywhere so you're calling methods on an object that doesn't exist which is why you're getting NullPointerException

    You're adding the students to the array so use the Student objects in the array to call methods on

     students[length].getFirstName()

    and so on

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to implement method to add new Student and display it

    ok thanks,

    I have changed the addStudent method to:

    	public void addStudent()
    	{
    	   	students[length].getFirstName();
    	   	students[length].getLastName();
    	   	students[length].getEnrollmentDate();
    	}

    and also I have added:

    Student aStudent=null;

    then

        public void displayStudentDetails()
        {
    		System.out.println("Student Name:" + aStudent.getFirstName() +" "+ aStudent.getLastName() + " Enrollment on:" + aStudent.getEnrollmentDate());
        }

    now I am getting an error when:

    1. Select Add student

    Please select an option: 1
    Exception in thread "main" java.lang.NullPointerException
    at StudentDatabase.addStudent(StudentDatabase.java:75 )
    at StudentDatabase.main(StudentDatabase.java:38)

    2. display Student details:

    Please select an option: 6
    Exception in thread "main" java.lang.NullPointerException
    at StudentDatabase.displayStudentDetails(StudentDatab ase.java:102)
    at StudentDatabase.main(StudentDatabase.java:54)

    Those errors are thrown cos object does not exist again...?

  6. #6
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: how to implement method to add new Student and display it

    The add method was ok as it was except maybe you'd want to put it like this
    public void addStudent()
    {
    	   	students[length] = new Student();
                    length++;
    }

    It was your display student method originally that was causing the problem. You don't really need the aStudent variable, you can call the methods from the objects in the array

    public void displayStudentDetails()
    {
    		System.out.println("Student Name:" + students[length-1].getFirstName() +" "+ students[length-1].getLastName() + " Enrollment on:" + students[length-1].getEnrollmentDate());
    }

    It needs to be length-1 because when you first create a Student and add it to the array, it adds it at position length which is 0 but you then add 1 to length. So students[length] would be students[1] and there's nothing in that part of the array so you get your null pointer exception.

  7. The Following User Says Thank You to samfin For This Useful Post:

    exose (February 10th, 2011)

  8. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to implement method to add new Student and display it



    It is working! thanks so much! I have even changed some stuff for example:

        public void displayStudentDetails()
        {
    		for (int i=0; i<length; i++)
    			{
    				System.out.println("Student Name:" + students[i].getFirstName() +" "+ students[i].getLastName() + " Enrollment on:" + students[i].getEnrollmentDate());
    			}
     
        }

    so if I add more students all students from database are getting displayed.

    I have now created class called module, and I did the same with it, so I am able to add new module and display it.

    The main question now is how I can assign a student to the module, where every student can be only max assigned to 3 modules (assuming I can have only 6 max modules) :O

    I was thinking about making two dimension array [20] (this is a max Students) [6] (max Modules) and just say true/false if student is assigned to it?

    or maybe I should think about something different?

Similar Threads

  1. Student TreeMap
    By raphytaffy in forum Algorithms & Recursion
    Replies: 6
    Last Post: March 2nd, 2010, 12:11 AM
  2. how to execute a simple display without a main method
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: January 13th, 2010, 07:28 AM
  3. Implement dragging a circle
    By jolly in forum AWT / Java Swing
    Replies: 0
    Last Post: August 19th, 2009, 02:48 PM
  4. [SOLVED] Help me with different activities in Java program
    By xyldon27 in forum Java Theory & Questions
    Replies: 10
    Last Post: June 9th, 2009, 09:42 AM
  5. How to display the contents of my queue?
    By rocafella5007 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 30th, 2009, 11:46 AM