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(); } }