Results 1 to 6 of 6
  1. #1
    exose is offline Member
    Join Date
    Sep 2010
    Posts
    19
    Rep Power
    0

    Default displaying an array

    I am having an issue with displaying student details:


    Java Code:
    	public void displayStudentDetails()
    	{
    	    System.out.println(studentNo);
    	 
    	    for (int i=0; i<=studentNo; i++)
    	    {
    	        if (students[i].getFirstName()!=null)
    	            {
    	                System.out.print(i+1+")"+ " ");
    	                students[i].Show();
    	                System.out.print("\n");
    	            }
    	 
    	    }
    	 
    	}

    When I am displaying student details I am getting those errors:

    1) Student Name:aaa aaa Enrollment on:aa
    Module 1 is unassigned.
    2) Student Name:bbb bbb Enrollment on:bbb
    Module 1 is unassigned.
    Exception in thread "main" java.lang.NullPointerException
    at StudentDatabase.displayStudentDetails(StudentDatab ase.java:206)
    at StudentDatabase.main(StudentDatabase.java:64)

    Process completed.

    I guess it is because I have nulls in the array, how I can display it without nulls?

  2. #2
    Yids is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    System.out.println(studentNo);

    for (int i=0; i<=studentNo; i++)
    {
    if (students[i].getFirstName()!=null)
    {
    System.out.print(i+1+")"+ " ");
    students[i].Show();
    System.out.print("\n");
    }

    }

    }



    Try this with the for loop for (int i=0; i<=studentNo.length; i++)

    remove the first system.out.print

  3. #3
    Yids is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    also try

    System.out.println(students[3]);
    rather than
    students[i].Show()

  4. #4
    Yids is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    sorry not students[3] but students[i]

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Which line is line 206? (the line that throws the exception)

    I'm a bit tired here, so bear with me here. The best way to solve these kind of problems yourself(which is good to get comfortable doing) is to follow the stack trace. It tells you each line that it populated from. Trace it to the line the exception was thrown and try to figure out what caused that error. The exceptions have good names to help you understand what happened.

    I also don't know what your show() method does, maybe that's causing the problem?

    Java Code:
    public class NullText{
    	public static void main(String[] args){
    		Integer[] myInts = new Integer[4];
    		myInts[0] = new Integer(3);
    		for(int i = 0; i < myInts.length; i++){
    			System.out.println(myInts[i]);
    		}
    	}
    }
    produces

    Java Code:
    3
    null
    null
    null
    for me.
    Last edited by sunde887; 03-10-2011 at 01:46 PM.

  6. #6
    exose is offline Member
    Join Date
    Sep 2010
    Posts
    19
    Rep Power
    0

    Default

    i have got this to work, thanks.

    I have another problem...

    I am trying to assign module to the student.

    my Student class looks like that:

    Java Code:
    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 ??
      public Module module1;
      public Module module2;
      public Module module3;
    
    
      // default constructor - initializes default attr values
    	public Student()
       	{
       		module1 = null;
       		module2 = null;
       		module3 = null;
       	}
    
        public Student(String newFirstName, String newLastName, String newEnrollmentDate)
        {
          	this.firstName = newFirstName;
          	this.lastName = newLastName;
          	this.enrollmentDate = newEnrollmentDate;
       		module1 = null;
       		module2 = null;
       		module3 = null;
        }
    
    //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;
        }
    
        public void AssignStudentToModule(Module ref module1,Module ref module2,Module ref module3)
        {
    			this.module1=module1;
    			this.module2=module2;
    			this.module3=module3;
    
        }
        public void Show()
        {
        	System.out.println("Student Name:" + getFirstName() +"\t"+ getLastName() +"\t" +" Enrollment on:" + getEnrollmentDate());
        	if((module1!=null)&&(module2!=null)&&(module3!=null))
        	{
        		module1.Show();
        		module2.Show();
        		module3.Show();
        	}
        	else if ((module1!=null)&&(module2==null)&&(module3==null))
        	{
        		module1.Show();
        		System.out.print("Modules 2 and 3 are unassigned.");
        	}
        	else if ((module1!=null)&&(module2!=null)&&(module3==null))
        	{
        		module1.Show();
        		module2.Show();
        		System.out.print("Module 3 is unassigned.");
        	}
        	else if ((module1==null)&&(module2!=null)&&(module3!=null))
        	{
    
        		module2.Show();
        		module3.Show();
        		System.out.print("Module 1 is unassigned.");
        	}
        	else if ((module1==null)&&(module2==null)&&(module3!=null))
        	{
        		module3.Show();
        		System.out.print("Modules 1 and 2 are unassigned.");
        	}
        	else if ((module1!=null)&&(module2==null)&&(module3!=null))
        	{
        		module2.Show();
        		System.out.print("Module 2 is unassigned.");
        	}
        	else
        		System.out.print("Modules 1, 2 and 3 are unassigned.");
    
        }
    
    }
    and I have a assignModule method:

    Java Code:
     public void assignModule()
        {
        	int studentNo;
        	int moduleNo1;
        	int moduleNo2;
        	int moduleNo3;
    
        	displayStudentDetails();
    
        	System.out.print("Please select student from the list above: ");
           	studentNo = scan.nextInt();
           	System.out.println("");
    
    
           	displayModuleDetails();
           	System.out.print("Please select 1st module from the list above: ");
           	scan = new Scanner(System.in);
           	moduleNo1 = scan.nextInt();
           	System.out.println("");
           	System.out.print("Please select 2st module from the list above: ");
           	scan = new Scanner(System.in);
           	moduleNo2 = scan.nextInt();
           	System.out.println("");
           	System.out.print("Please select 3st module from the list above: ");
           	scan = new Scanner(System.in);
           	moduleNo3 = scan.nextInt();
           	System.out.println("");
    
           	students[studentNo-1].AssignStudentToModule(modules[moduleNo1-1],modules[moduleNo2-1],modules[moduleNo3-1]);
    assigning is working and I am really happy with it, I have a problem with deleting the existing module.

    1. module1 is assigned to the student (the reference)
    2. I am deleting module1 from modules array
    3. module1 is not appearing in modules array
    4. reference to the module1 is still appearing when I am displaying student details (this is the bad part)

    Do you know how to delete reference when deleting module from the array? I thought it will be done automatically, but it is not :(

Similar Threads

  1. Help displaying an array from a seperate class
    By aeon1613 in forum New To Java
    Replies: 2
    Last Post: 01-16-2011, 09:15 AM
  2. Displaying an image through an array
    By BariMutation in forum New To Java
    Replies: 0
    Last Post: 12-04-2010, 05:20 AM
  3. Displaying array elements as null except the last index
    By vasavi.singh in forum Advanced Java
    Replies: 2
    Last Post: 04-06-2009, 12:42 PM
  4. Problems displaying and array of JPanels
    By dousedfirepants in forum New To Java
    Replies: 3
    Last Post: 11-13-2008, 06:47 AM
  5. Problem with displaying search results from an array
    By BHCluster in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 04:34 AM

Posting Permissions

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