NullPointerException
Hi guys, got a problem with the following source that compiles but on run-time I get a nullpointerexception. Do some of you guys have any clue about what could be the cause? Thanks.
import java.io.*;
import java.awt.*;
import java.lang.*;
import java.util.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Student {
static public BufferedReader keyboard =
new BufferedReader(new InputStreamReader(System.in));
public String name;
public int stnumber; //KEY
public Module modulest[];
/** Creates a new instance of Student */
public Student() {
String name;
int stnumber;
System.out.println("Please input student name: ");
this.name = Text.ReadString(keyboard);
System.out.println("Please input student number: ");
this.stnumber = Text.ReadInt(keyboard);
//students[count]=;
//Module.students[count]=;
}
public String Modules_Taken() {
String result="0";//=" "; // = null;
for(int i=0;i<=1;i++) {
result = result + modulest.modtitle;
}
return result;
}
public String getName() {
return name;
}
public int getStnumber() {
return stnumber;
}
}
import java.io.*;
import java.awt.*;
import java.lang.*;
import java.util.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TheStuModMain {
static Vector v1,v2;
public static Vector Members = new Vector();
/** Creates a new instance of TheStuModMain */
public TheStuModMain() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
v1 = new Vector();
v2 = new Vector();
//Members = new Vector();
Undergraduate undstu = new Undergraduate();
v1.add(undstu);
System.out.println(v1.elementAt(0).toString());
//System.out.println(undstu.Modules_Taken());
//UndModule unmd = new UndModule(8001, "Intro to Comp 1");
//UndModule unmd1 = new UndModule(8003, "Intro to Comp 2"); // Initialisation of the compulsory modules for all undergrads
//Members.add(unmd);
//Members.add(unmd1);
//System.out.println(unmd.modnumber+" " + unmd.title);
//System.out.println(unmd.ToString());// to re-assure these modules have been added 8001 8003 next step to save it in a vector
//v1.addElement(unmd);
//System.out.println(Members.elementAt(0).toString());
//System.out.println(Members.elementAt(1).toString());
// TODO code application logic here
//v1.addElement(unmd);
//v1.elementAt(0);
//System.out.println(v1.get(0));
}
}
Feel Free To Request Any Other .java File
Thanks Again
Here is the ouput msg by netbeans
Exception in thread "main" java.lang.NullPointerException
at Undergraduate.toString(Undergraduate.java:31)
at TheStuModMain.main(TheStuModMain.java:25)
Java Result: 1
BUILD SUCCESSFUL (total time: 10 seconds)
and undegraduate extends student class is here:
import java.io.*;
import java.awt.*;
import java.lang.*;
import java.util.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Undergraduate extends Student {
//public Vector Members1;
/** Creates a new instance of Undergraduate */
protected int count =2; //2 compulsory modules to be added 8001 8003
protected int count2 = 6; //total number of modules
//protected Module modules[] = new Modules[count];
/*public void AddCompMods() {
for(int i=0;i < Members.size();i++) {
}
}*/
public Undergraduate() {
super();
Module modulest[] = new Module[6];
modulest[0]= new UndModule(8001,"IntroComp");
modulest[1]= new UndModule(8003,"AdvComp");
}
public String toString() {
return ("name: " + getName() + "student number: " +getStnumber() + modulest[0].modtitle ); // Modules_Taken() ); //+ Modules_Taken());
}
//Module modulest[] = new Module[count2];
//for(int i=0;i<count;i++) {
//}
}>

