static functions cannot use methods or variables that are not static as well. Therefore, we have the same issue as before. Either make an instance of your class and do instanceOfClass.student.toString();, or make student static.
Java Menu
#16
Posted 02 March 2006 - 03:39 PM
static functions cannot use methods or variables that are not static as well. Therefore, we have the same issue as before. Either make an instance of your class and do instanceOfClass.student.toString();, or make student static.
#17
Posted 03 March 2006 - 04:10 AM
import java.io.*; import java.util.*; public class Main { static private BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); private String menu; static ArrayList student; int count = 0; public static void showMenu() { System.out.println("------------------------------------------------"); System.out.println("-- Welcome to the Student Registration System --"); System.out.println("-- Please make your choice --"); System.out.println("-- --"); System.out.println("-- 1. Add Student --"); System.out.println("-- 2. Submit Student Modules --"); System.out.println("-- 3. Update Module Results --"); System.out.println("-- 4. Display a Student Record --"); System.out.println("-- 5. Display all Student Records --"); System.out.println("-- 6. Edit Module --"); System.out.println("-- 7. Display Module Details --"); System.out.println("-- 8. Display all Post Grad Students --"); System.out.println("-- 9. Display all Under Grad Students --"); System.out.println("-- 0. Leave Program --"); System.out.println("------------------------------------------------"); } public static void main(String[] args) { showMenu(); int menuChoice = 10; String studName; int sNum; while (menuChoice != 0) { showMenu(); System.out.println("Please choose your desired task"); menuChoice = Text.ReadInt(in); if (menuChoice == 1) { ArrayList student = new ArrayList(); System.out.println("Please enter the Student Number - "); sNum = Text.ReadInt(in); System.out.println("Please enter the Students Name - "); studName = Text.ReadString(in); System.out.print("Thank you for registering with us " + studName); System.out.println(", your student number is " + sNum); } if (menuChoice == 5) { student.toString(); } if (menuChoice == 0) { System.out.println("Goodbye"); } } } public void print() { } public void addStudent(Student s) { student.add(s); } }
Thats the code. As you can see i have tried the option of making student static, but when i run the program i get the error -
java.lang.NullPointerException
at assignmentone.Main.main(Main.java:80)
Exception in thread "main"
#18
Posted 03 March 2006 - 06:24 AM
if (menuChoice == 1) { ArrayList student = new ArrayList(); // ... }This student variable is hiding your other one; the other one is never being instantiated, hence the NullPointerException. Either make that line student = new ArrayList();, or get rid of that line completely, and instantiate student in the beginning of the class.
#19
Posted 03 March 2006 - 01:17 PM
The program compiles correctly now, but when I print the List, it returns blank.
Code is much the same as before. I'm thinking it may be an error in my Student Class.
#20
Posted 03 March 2006 - 02:53 PM
Well, where are you adding to the list?The program compiles correctly now, but when I print the List, it returns blank.
#21
Posted 03 March 2006 - 04:49 PM
//Sets student Name public void SetName (String studName) { name = studName; } //Sets Student Number public void SetStudNum (int sNum) { studNum = sNum; }
Is this incorrect?
#22
Posted 03 March 2006 - 07:43 PM
In your Student class, you should have an overloaded constructor that has parameters for a name and number.
public Student(String name, int num) { setName(name); setNumber(num); }
Now, after retrieving the info from the user, you can do:
addStudent(new Student(studName, sNum));
#23
Posted 04 March 2006 - 03:53 AM
Now i need to have a think how to make it display all, im thinking i could probebly use the size() function and a for loop. Unless theres an easier way
#24
Posted 04 March 2006 - 06:49 AM
There's nothing wrong with that. You could also use the Iterator class:Now i need to have a think how to make it display all, im thinking i could probebly use the size() function and a for loop. Unless theres an easier way
Iterator it = list.iterator(); while (it.hasNext()) { System.out.println(it.next()); }But doing it the other way is perfectly fine:
for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); }
#25
Posted 05 March 2006 - 03:46 AM
Im not sure that my list is allowing anymore than one student to be added to it though, as printing the array list only returns the last record i entered.
#26
Posted 05 March 2006 - 06:29 AM
anyone know how to make a "Press any key to continue..." thing actually work in Java cause i can get it so you type something in and press enter and it carrys on but not any key, or even simply just enter ...
#27
Posted 05 March 2006 - 07:03 AM
Please start your own thread.anyone know how to make a "Press any key to continue..." thing actually work in Java cause i can get it so you type something in and press enter and it carrys on but not any key, or even simply just enter ...
#28
Posted 05 March 2006 - 07:04 AM
Can you post your code?Im not sure that my list is allowing anymore than one student to be added to it though, as printing the array list only returns the last record i entered.
#29
Posted 05 March 2006 - 07:12 AM
public static void addStudent(Student s) { student.add(s); }
if (menuChoice == 1) { student = new ArrayList(); System.out.println("Please enter the Student Number - "); sNum = Text.ReadInt(in); System.out.println("Please enter the Students Name - "); studName = Text.ReadString(in); System.out.print("Thank you for registering with us " + studName); System.out.println(", your student number is " + sNum); addStudent(new Student(studName, sNum)); }
if (menuChoice == 5) { for (int i = 0; i < student.size(); i++) { System.out.println(student.get(i)); } }
#30
Posted 05 March 2006 - 08:32 AM
Similar Topics
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users