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 1 of 2 12 LastLast
Results 1 to 25 of 36

Thread: Help with null pointer exception

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

    Default Help with null pointer exception

    Hi there,
    I am trying to do this question as part of an assignment but I just can't get it to work properly.


    "Write a class called Module that has a module code, title and a result (e.g. MM105 Maths 57.2%). Write a Student class that has a name, id number, programme and an array of Modules. Add any methods that are necessary. Test that your class is fully working by creating a suitable display() mechanism, which displays a student’s full record for the current year. Note: for simplicity assume that there is only one year in a programme."

    I keep getting a null pointer exception when I try to compile and can't firgure out where i'm going wrong.

    "Exception in thread "main" java.lang.NullPointerException
    at Assignment6.Student.<init>(Student.java:21)
    at Assignment6.Student.main(Student.java:50)
    "
    I would greatly appreciate if anyone could shed some light on where i'm going wrong.
    Thanks in advance!


    package Assignment6;
     
    public class Module {
     
    	protected static 
    	 String code;
    	 static String title;
    	 static float result;
     
     
    	public  Module(String acode,String atitle, float aresult)
     
    		{
    			code = acode;
    			title = atitle;
    			result = aresult;
    		}
     
    	public static  void display(Module a)
    	{
    		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, Module[] modules)
    		{
    			this.name= name;
    			this.id= id;
    			this.programme = programme;
     
    			for(int i=0;i<modules.length;i++)
     
    			{
    				this.modules[i] = modules[i];
    			}
     
    		}	
     
    	public void display()
    		{		
    			System.out.println("Name: " + name);
    			System.out.println("ID No.: " + id);
    			System.out.println("Programme: " + programme);
     
    			for(int i=0;i<modules.length;i++)
     
    			{
    				Module.display(modules[i]);
    			}
     
     
    		}
     
    	public static void main(String[] args) {
     
    	Module [] modules = new Module [4];	
     
    	modules[0] = new Module("test","test",57.2f);
    	modules[1] = new Module("test1","test1", 90.00f);
    	modules[2] = new Module("test2","test2",75.8f);
    	modules[3] = new Module("test3","test3",45.7f);
     
    	Student a = new Student("John Johnson",123456,"Java Programming", modules);
    	a.display();
     
    }
     
     
     
    }
    Last edited by Dr.HughMan; November 30th, 2011 at 12:17 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with null pointer exception

    "Exception in thread "main" java.lang.NullPointerException
    at Assignment6.Student.<init>(Student.java:21)
    look at line 21 in the Student class. There is a variable there with a null value. Backtrack in your code to find out why and fix the code so that the variable has valid value.
    If you can not see the variable, add a println statement just before line 21 to print out the values of all the variables on line 21.

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

    Default Re: Help with null pointer exception

    Quote Originally Posted by Norm View Post
    look at line 21 in the Student class. There is a variable there with a null value. Backtrack in your code to find out why and fix the code so that the variable has valid value.
    If you can not see the variable, add a println statement just before line 21 to print out the values of all the variables on line 21.
    Thanks for the suggestion!

    But I still cant see how to fix it. When I print out the modules on line 21 it gives me the addresses of each module in the array. Which only confuses me more

    Any other suggestions?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with null pointer exception

    What variable is null?
    Post the contents of line 21 if you need help finding the variables that are used on that line.

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

    Default Re: Help with null pointer exception

    Quote Originally Posted by Norm View Post
    What variable is null?
    Post the contents of line 21 if you need help finding the variables that are used on that line.
    When I modify the code to print out contents of line 21


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

    I get the following output:

    "Assignment6.Module@1be2d65
    Assignment6.Module@9664a1
    Assignment6.Module@1a8c4e7
    Assignment6.Module@1172e08
    Name: John Johnson
    ID No.: 123456
    Programme: Electronic Engineering
    Exception in thread "main" java.lang.NullPointerException
    at Assignment6.Student.display(Student.java:33)
    at Assignment6.Student.main(Student.java:52)"

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with null pointer exception

    Exception in thread "main" java.lang.NullPointerException
    at Assignment6.Student.display(Student.java:33)
    The error has moved to line 33. What variable on line 33 is null?

    You never posted line 21 so I can't help you determine which variable on that line is null.

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

    Default Re: Help with null pointer exception

    Sorry this is line 21

    this.modules[i] = modules[i];

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

    Default Re: Help with null pointer exception

    and this is line 33

    Module.display(modules[i]);

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with null pointer exception

    On line 21 You need to print out the value of this.modules and modules.
    On line 33 you need to print out the value of Module and modules.

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

    Default Re: Help with null pointer exception

    When I print out this.modules and modules on line 21 I get
    "[LAssignment6.Module;@13e8d89
    null"

    And when I try to print out Module and modules on line 33 I get nothing and it wont let me print out Module as it is not a variable.

    Thank you for you patience!

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with null pointer exception

    Which variable is null on line 21? Why is it null? Did you assign any value to that variable?

    I get nothing
    Please explain. Do you mean it prints spaces or blanks?
    Change your println statement to have a String with the variable:
    println("var=" + var + "<");

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

    Default Re: Help with null pointer exception

    In my main of the student Class I create an array of modules with four modules, then the Student constructer is supposed to assign this array(modules) to the array Module [] modules that I declare at the top using

    "this.modules[i] = modules[i];"

    Heres the code I am trying to run at the moment to print out the Variables on line 33

    package Assignment6;
     
    public class Student {
     
    	protected String name;
    	int id;
    	String programme;
     
    	public Module[] modules;
     
     
    public   Student(String name,int id,String programme, Module[] modules)
    		{
    			this.name= name;
    			this.id= id;
    			this.programme = programme;
     
    			for(int i=0;i<modules.length;i++)
     
    			{
    				System.out.println(modules);
    				System.out.println(this.modules);
    				this.modules[i] = modules[i];
    			}
     
    		}	
     
    	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++)
     
    			{
    				System.out.println("var=" + modules  + "<");
    				System.out.println("var=" + Module + "<");
    			Module.display(modules[i]);
    			}
     
     
    		}
     
    	public static void main(String[] args) {
     
    	Module [] modules = new Module [4];	
     
    	modules[0] = new Module("EE219","OOP",57.2f);
    	modules[1] = new Module("EE203","Maths", 90.00f);
    	modules[2] = new Module("EE223","Digital Electronics",75.8f);
    	modules[3] = new Module("EE215","Solid State Electronics",45.7f);
     
    	Student a = new Student("John Johnson",123456,"Electronic Engineering", modules);
    	a.display();
     
    }

    The result I get is

    "
    Exception in thread "main" java.lang.NullPointerException
    at Assignment6.Student.<init>(Student.java:21)
    at Assignment6.Student.main(Student.java:54)
    [LAssignment6.Module;@13e8d89
    null
    "

    the code on line 54 is
    "
    Student a = new Student("John Johnson",123456,"Electronic Engineering", modules);

    "

    }

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with null pointer exception

    In your printout, What variable is null? Your print out does not have an id String with the null line.

    Exception in thread "main" java.lang.NullPointerException
    at Assignment6.Student.<init>(Student.java:21)
    at Assignment6.Student.main(Student.java:54)
    You read the stack trace from the bottom up.
    At line 54 in main() the code calls the Constructor<init> for the Student class.
    At line 21 in the constructor there is a null variable (that we are still looking for. What variable is it?

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

    Default Re: Help with null pointer exception

    I believe the null variable is "this.modules"

    I think it is null as the constructor does not seem to assign it the array that I pass to it.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with null pointer exception

    Where do you assign a value to the "this.modules" variable? You define it but never give it a value.

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

    Default Re: Help with null pointer exception

    "this.modules[i] = modules[i];" was supposed to assign it?

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with null pointer exception

    Yes that assigns a value to one of the slots in an existing array, but you need to define it first.
    What if modules[] had 2 slots, would you be able to assign values to the 10th slot?
    Before you use an array you must define it with a specific size.

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

    Default Re: Help with null pointer exception

    Does this not happen when in my main at

    "Module [] modules = new Module [4];" ?

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with null pointer exception

    That would define a variable: modules known only in the main method. The class variable with the same name would still not have a value.
    If you want to assign a value to the modules variable leave off the definition part of that statement:
    modules = new Module [4];

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

    Default Re: Help with null pointer exception

    Thank you that has sorted my Null Pointer Exception error.

    The programm now compiles and runs ok but is not outputing the correct information.
    package Assignment6;
     
    public class Module {
     
    	protected static 
    	 String code;
    	 static String title;
    	 static float result;
     
     
    	public  Module(String acode,String atitle, float aresult)
     
    		{
    			code = acode;
    			title = atitle;
    			result = aresult;
    		}
     
    	public static  void display(Module a)
    	{
    		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 static Module[] modules;
     
     
     
    public   Student(String name,int id,String programme, Module[] modules)
    		{
    			this.name= name;
    			this.id= id;
    			this.programme = programme;
     
     
    			for(int i=0;i<modules.length;i++)
     
    			{
    				this.modules[i] = modules[i];
    			}	
    		}
     
    	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) {
    	modules = new Module [4];
     
    	modules[0] = new Module("EE219","OOP",57.2f);
    	modules[1] = new Module("EE203","Maths", 90.00f);
    	modules[2] = new Module("EE223","Digital Electronics",75.8f);
    	modules[3] = new Module("EE215","Solid State Electronics",45.7f);
     
    	Student a = new Student("John Johnson",123456,"Electronic Engineering", modules);
    	a.display();
     
    }
     
     
     
    }

    Output
    "
    Name: John Johnson
    ID No.: 123456
    Programme: Electronic Engineering
    Module Code:EE215
    Moule Title:Solid State Electronics
    Result:45.7%
    Module Code:EE215
    Moule Title:Solid State Electronics
    Result:45.7%
    Module Code:EE215
    Moule Title:Solid State Electronics
    Result:45.7%
    Module Code:EE215
    Moule Title:Solid State Electronics
    Result:45.7%
    "

    Any ideas why this is happening, I just cant get my head around this

    Thanks again for all your help

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with null pointer exception

    Please explain what the problem is and show what you want the output to look like.

    The output reminds me of problems using static variables.
    Only the value of the last data assigned is present in all the instances because they are sharing one static variable instead of having their own variables.
    Last edited by Norm; November 30th, 2011 at 04:10 PM.

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

    Default Re: Help with null pointer exception

    The output im looking for is

    Name: John Johnson
    ID No.: 123456
    Programme: Electronic Engineering
    Module Code:EE219
    Moule Title:OOP
    Result:57.2%
    Module Code:EE203
    Moule Title:Maths
    Result:90.00%
    Module Code:EE223
    Moule Title: Digital Electronics
    Result:75.8f%
    Module Code:EE215
    Moule Title:Solid State Electronics
    Result:45.7%
    "
    The problem I am having is that the outputed Modules are all the same
    "
    Module Code:EE215
    Moule Title:Solid State Electronics
    Result:45.7%"
    "

    I had thought it might have been a problem with my "public static Module[] modules; " but when I remove the static from that I get an error(Cannot make a static reference to the non-static field modules) in my main where I am assigning the modules in the array.

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with null pointer exception

    You should get rid of all static variables.
    the outputed Modules are all the same
    Only the value of the last data assigned is present in all the instances because they are sharing one static variable instead of having their own variables.

    Change the code in the main() method so you do not need any static variables. Put the code in main in a constructor for the Student class. Have main pass the size of the array to the constructor.
    Add a method to the Student class to add Module objects to the array and call that method from main.

    The modules variable is not your problem. Continue looking and removing static

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

    Default Re: Help with null pointer exception

    Thank you for all your suggestions.

    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(Module a)
    	{
    		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 i)
    		{
    			this.name= name;
    			this.id= id;
    			this.programme = programme;
     
    			modules = new Module [i];
    			modules[0] = new Module("EE219","OOP",57.2f);
    			modules[1] = new Module("EE203","Maths", 90.00f);
    			modules[2] = new Module("EE223","Digital Electronics",75.8f);
    			modules[3] = new Module("EE215","Solid State Electronics",45.7f);
    		}
     
    	public void array()
    	{
    		for(int i=0;i<modules.length;i++)
     
    		{
     
    			this.modules[i] = modules[i];
    		}	
     
    	}
    	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.array();
    	a.display();
     
    }
     
     
     
    }

    My problem now is an error with "Module.display(modules[i]);" in the student class.
    (Cannot make a static reference to the non-static method display(Module) from the type Module)

    Then if I make the display method in the Module class static, all the variables have to be static and I'm back to the same problem of it outputting the same last module 4 times.

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with null pointer exception

    You haven't followed my suggestions (which I see were not clear). You should NOT put your testing code into the Student class's constructor.
    Have main pass the size of the array to the Student class's constructor.
    Add a method to the Student class to add Module objects to the array and call that method 4 times from main.

    Module.display(modules[i]);
    The syntax of this statement is backwards.
    display( ) is a method of the Module class. It has access to all the data in its instance of the class.
    modules[i] is an instance of the Module class. Use that instance to call the display method.
    Look at the code for the display method, what does it do with the parameter a?

Page 1 of 2 12 LastLast

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