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.

Page 2 of 2 FirstFirst 12
Results 26 to 36 of 36

Thread: Help with null pointer exception

  1. #26
    Junior Member
    Join Date
    Nov 2011
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with null pointer exception

    Add a method to the Student class to add Module objects to the array and call that method 4 times from main.
    Sorry but I dont understand this.

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with null pointer exception

    Which part don't you understand?
    1)add a method? see the display method. its a method that you pass a Module object to
    2)add objects to the array. See the Student class's constructor. It adds Module elements to the array

    The only trick is the index to the array. The method that adds the elements to the array will be responsible for incrementing the index. The index must be a class variable so it keeps its value over calls to the add method.

  3. #28
    Junior Member
    Join Date
    Nov 2011
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with null pointer exception

    Am I heading in the right direction with this?

    package Assignment6;
     
    public class Student {
     
    	protected String name;
    	int id;
    	String programme;
    	public  Module[] modules;
     
     
     
    public   Student(String name,int id,String programme, int arraysize)
    		{
    			this.name= name;
    			this.id= id;
    			this.programme = programme;
    			modules = new Module[arraysize];
     
    		}
     
    	public void  objectadd(String code, String title ,float result)
    	{
     
    		for(int i=0;i<4;i++)
     
    		{
    			modules[i] = new Module(code,title,result);
    		}	
     
    	}
     
    	public void display()
    		{		
    			System.out.println("Name: " + name);
    			System.out.println("ID No.: " + id);
    			System.out.println("Programme: " + programme);
     
    			for(int i=0;i<4;i++)
     
    			{
    				Module.display(modules[i]);
    			}
     
    		}			
     
     
    	public static void main(String[] args) {
     
    		Student a = new Student("John Johnson",123456,"Electronic Engineering", 4);
     
    		a.objectadd("EE219","OOP",57.2f);
    		a.objectadd("EE203","Maths", 90.00f);
    		a.objectadd("EE223","Digital Electronics",75.8f);
    		a.objectadd("EE215","Solid State Electronics",45.7f);
     
    		a.display();
     
    }
     
     
     
    }

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with null pointer exception

    What happens when you execute it?

    Look at the second part of post #25

  5. #30
    Junior Member
    Join Date
    Nov 2011
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with null pointer exception

    Here is my code as it stands now;

    package Assignment6;
     
    public class Module {
     
    	  protected
    	   String code;
    	   String title;
    	   float result;
     
     
    	public  Module(String acode,String atitle, float aresult)
     
    		{
    			code = acode;
    			title = atitle;
    			result = aresult;
    		}
     
    	public  void display()
    	{
    		System.out.println("Module Code:" + code);
    		System.out.println("Moule Title:" + title);
    		System.out.println("Result:" + result + "%");
    	}
     
    	public static void main(String[] args) {
     
     
    	}
     
    }

    package Assignment6;
     
    public class Student {
     
    	protected String name;
    	int id;
    	String programme;
    	public  Module[] modules;
     
     
     
    public   Student(String name,int id,String programme, int arraysize)
    		{
    			this.name= name;
    			this.id= id;
    			this.programme = programme;
    			modules = new Module[arraysize];
     
    		}
     
    	public void  objectadd(String code, String title ,float result)
    	{
     
    		for(int i=0;i<4;i++)
     
    		{
    			modules[i] = new Module(code,title,result);
    		}	
     
    	}
     
    	public void display()
    		{		
    			System.out.println("Name: " + name);
    			System.out.println("ID No.: " + id);
    			System.out.println("Programme: " + programme);
     
    			for(int i=0;i<4;i++)
     
    			{
    				modules[i].display();
    			}
     
    		}			
     
     
    	public static void main(String[] args) {
     
    		Student a = new Student("John Johnson",123456,"Electronic Engineering", 4);
     
    		a.objectadd("EE219","OOP",57.2f);
    		a.objectadd("EE203","Maths", 90.00f);
    		a.objectadd("EE223","Digital Electronics",75.8f);
    		a.objectadd("EE215","Solid State Electronics",45.7f);
     
    		a.display();
     
    }
     
    }


    My output is now the same as before (output the same module 4 times)!
    I cant thank you enough for all your help!

  6. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with null pointer exception

    Why the loop in objectadd()? It should add one object. The caller of object will call it for as many times as it wants to add a new Module to the student.
    In display you should use the length of the array to control the loop and NOT have a hardcoded value of 4. In the next test there could be 6 modules added to the student.

  7. #32
    Junior Member
    Join Date
    Nov 2011
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with null pointer exception

    Why the loop in objectadd()? It should add one object. The caller of object will call it for as many times as it wants to add a new Module to the student.
    Im sorry but I dont understand this.How will it move to the next slot in the array to add the next module?

  8. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with null pointer exception

    How will it move to the next slot in the array to add the next module?
    If at any time you call the objectadd method to add an element to the array, where will it put that one element? How will it keep track of where the next empty element is located?

  9. #34
    Junior Member
    Join Date
    Nov 2011
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with null pointer exception

    I'm thinking I have to make a counter and increment it when a module is assigned but I dont know how or where to put it?

  10. #35
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with null pointer exception

    Make it a class variable in the class where the array is. Its initial value of 0 will be where the first element is to go in the array then increment for the next element. Its value at any time will be the number of elements in the array.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Dr.HughMan (November 30th, 2011)

  12. #36
    Junior Member
    Join Date
    Nov 2011
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with null pointer exception

    Finally got it to work! Thanks a million!

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Null Pointer Exception?
    By SeanEE89 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 16th, 2011, 06:21 PM
  2. Null Pointer exception
    By Demetrius82 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 2nd, 2011, 07:32 PM
  3. Null Pointer Exception Help !!
    By AlterEgo1234 in forum Member Introductions
    Replies: 1
    Last Post: March 27th, 2011, 10:07 AM
  4. Null Pointer Exception Help!!
    By puzzledstudent in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 11th, 2010, 06:46 PM
  5. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM