Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Java Menu


  • Please log in to reply

#16
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
Well, without seeing your code, I'm going to have to bed that you have a instance variable student in the beginning of your class. Then, you are trying to access it within a static function.

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.
  • 0

Advertisements


#17
zell_ffhut

zell_ffhut

    Member

  • Topic Starter
  • Member
  • PipPip
  • 34 posts
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"
  • 0

#18
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
You have the following:
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.
  • 0

#19
zell_ffhut

zell_ffhut

    Member

  • Topic Starter
  • Member
  • PipPip
  • 34 posts
I've changed the line to "student = new ArrayList();"

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.
  • 0

#20
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts

The program compiles correctly now, but when I print the List, it returns blank.

Well, where are you adding to the list?
  • 0

#21
zell_ffhut

zell_ffhut

    Member

  • Topic Starter
  • Member
  • PipPip
  • 34 posts
Well i add values to the variables studName and sNum, then in the Class i assign these to the class attributes

		//Sets student Name
		public void SetName (String studName)
		{
			name = studName;
		}
		//Sets Student Number
		public void SetStudNum (int sNum)
		{
			studNum = sNum;
		}

Is this incorrect?
  • 0

#22
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
But that doesn't add to the list. Your addStudent(Student) will add to the list. After receiving the info from the user, then add a new Student to the ArrayList.

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

  • 0

#23
zell_ffhut

zell_ffhut

    Member

  • Topic Starter
  • Member
  • PipPip
  • 34 posts
Thank you. I've got it adding records now, and displaying the last one i entered.

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 :tazz:
  • 0

#24
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts

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 :tazz:

There's nothing wrong with that. You could also use the Iterator class:
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));
}

  • 0

#25
zell_ffhut

zell_ffhut

    Member

  • Topic Starter
  • Member
  • PipPip
  • 34 posts
Yeah, the for loop seemed the easier option, but thanks for the input :tazz:

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.
  • 0

Advertisements


#26
deadmanwalking

deadmanwalking

    New Member

  • Member
  • Pip
  • 1 posts
you're either also from Brookes or another uni has set the exact same assignment1 :tazz:

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 ...
  • 0

#27
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts

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 ...

Please start your own thread.
  • 0

#28
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts

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.

Can you post your code?
  • 0

#29
zell_ffhut

zell_ffhut

    Member

  • Topic Starter
  • Member
  • PipPip
  • 34 posts
Here are the bits to do with adding records.

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


  • 0

#30
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
Well, yeah. You're only adding one student. Do you mean you want it to save the students added everytime you run the program?
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP