import java.util. *;
import java.util. ArrayList;
public class Module
{
    private int moduleCode;
    private String classlist;

private ArrayList<String>studentsEnrol;
public Module(){
 studentsEnrol = new ArrayList<String>();
}

public void storestudent(String student){
studentsEnrol.add(student); 
} 
public String classlist(){
return classlist(); 
}

This is a code i wrote, the code meant to store student in a array list, e.g. when invoke void storestudent(String student) method it will ask me to add student name, then that student name should be store in array list so next time when i call String classlist() the student name that are store in ArrayList should get returned.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

ehm ... maybe should, but:

1. you never enter values in your classlist variable
2.

public String classlist(){
return classlist();
}

will indeed give you errors. remove the brackets, after all, you're not trying to return the method, but the values (you should have) stored in the classlist.
so, either you just return the arraylist, instead of classlist, or, you must fill the values in classlist first.

since I assume you'll want later on to add the possibility to remove names, you can either return the arraylist, or fill the names into the string variable when you try to return it.

Ok. First of all what is stored in classList String???

And also... when classList() method is called should it return the "ArrayList" or a "String array" of all the students in the ArrayList??

BTW, the reason for the syntax error is you have added a "()" after "return classList" statement. remove the brackets.

But even after that your code makes little sense... so please answer the questions Ive asked you so that we can try to help you out with what you are trying to do....

Regards.

when invoking classList String the window will pop up which will ask user to enter name of students...those names will get store in ArrayList. and when calling String classlist() method it should return all the student that was stored in Arraystring..

And yes i removed brackets "return classlist;" but still it shows error......when i invoke "void storestudent(String student)" method, the programme asks for a user to enter a string, i enter any word and when i click enter it prints this message "java.lang.NullPointerException
at Module.storestudent(Module.java:15)" with showing high light in this sentence "studentsEnrol.add(student); "

can you also show the code you're using to call this code?
I don't really get what you are trying to do (or think you are doing, for that much ... ) what do you mean by: when invoking classList .. you do realize your class is called Module, right?

okkkk i am trying to answer these three questions and i am stuck on question 4 and 5 here are the questions:
3. Write a constructor with one String formal parameter that initialises the
module code attribute to be equal to the value of the actual parameter it is
invoked with. Additionally, the constructor should initialise the classlist.
4. Write a method called ‘getCode()’ that will return the module code.
5. Write a method called ‘enrolExisting(…)’ with one Student parameter
that when invoked will add the student to the classlist.
6. Write a method called ‘enrolNew(…)’ with one String parameter that
when invoked will create a Student object with name as given by
enrolNew’s actual parameter, and adds this student to the classlist.

yes, but what is the code that you are using, and that is causing the problems?

added a few responses on your post:
3. your constructor does not take a parameter, so you'll have to look into that as well
4. getCode() is supposed to do just that: return the code (which is the variable you call moduleCode
for 5 and 6:
your arraylist should not contain String objects, but it should contain objects of the type 'Student' which you'll still have to make.

Edited 5 Years Ago by stultuske: added some answers to OP's questions

i am using java BlueJ as IED and the code i written is shown in the thread. when compiling it is fine but when i enter student name in" void storestudent(String student)" method and click ok it shows this error "java.lang.NullPointerException
at Module.storestudent(Module.java:15)"

in simple form i want to write a programme that asks users to enter student name and that name gets store in ArrayList and when calling "classlist method" (}
public String classlist(){
return classlist;
} ) it should return all the student that have been entered in by users

that is not your complete code.

at the very least, you are missing a bracket at the end, and you don't have a main method, without which you can not run the application.

so, can you paste in your next post the code as you have when you try to run it?

import java.util. *;
import java.util. ArrayList;
public class Module
{
private int moduleCode;
private String classlist;


private ArrayList<String>studentsEnrol;
public Module(){
studentsEnrol = new ArrayList<String>();
}

public void storestudent(String student){
studentsEnrol.add(student);
}
public String classlist(){
return classlist;
}


public Module(int newmoduleCode){
moduleCode = newmoduleCode;

}
public int getCode(){
return moduleCode;
}
}

This is the full code i try to run

well ... you can't "run" this code, since, as I pointed out, it has no main method.
look at it like buying a car without an engine or wheels. off course, you can sit in it, but it won't move.

You'll need an engine and wheels (or, in Java terms, a main method) to get it running.

also, as I already pointed out: you don't want

private ArrayList<String>studentsEnrol;

you need

private ArrayList<Student>studentsEnrol;

which also means you'll need to create a Student class.

okk soo ur saying i need to make another class which is called "Student" ---okkk i made the class called Student u recon i should input all my fields to student class ( private int moduleCode;
private String classlist;) to make connection between two classes...

no, Student should contain String name (as far as I can tell)
with a constructor, setter and getter for the name

one more question i defined all the student behaviours in Module class soo i was wondering what should i write in student class...Even the string was introduced in Module class sooo i just need help distributing fides

you need to create a class Student, with a variable name (as mentioned before, and in your assignment) and you need to create a new instance or use existing instances of this class as described in your assignment.

what 'behaviour' do you mean? Student doesn't have real behaviour, it has a name and means to get (getter) and set (setter and constructor) the value of that variable.

what do you mean by 'distributing fides?'

why are u returning classList???? IT does not have anything in it (it has null).

Dude... come on... open your eyes and see... if you are not gonna do anything with the student names other then print it, you can just concatenate all string names in a single string and return that string...

why are u returning classList???? IT does not have anything in it (it has null).

Dude... come on... open your eyes and see... if you are not gonna do anything with the student names other then print it, you can just concatenate all string names in a single string and return that string...

stevanity: to quote yourself:
Dude... come on... open your eyes and see...

I already told him those things, and helped him further.
don't add pointless comments, I've seen that you are way better than that. For this thread, give him the time to show for himself what he did, and when he does, point out the flaws in that. Don't go bashing about what has been corrected ten posts ago.

Im sorry Stulske... I didnt notice the second page.... Ill be more careful from now on. Thanks!

Hi stultuske and stevanity
I just picked up on this thread, and there's one post I wanted to clarify...

public String classlist(){
    return classlist();
  }

will indeed give you errors. remove the brackets, after all, you're not trying to return the method, but the values

the reason for the syntax error is you have added a "()" after "return classList" statement. remove the brackets.

This perfectly valid source code does not generate a syntax error, nor does it try to return a method. It's a simple recursive call that returns the value returned from calling classlist(). Mind you. although the syntax is valid, the runtime effect is simply an infinite recursive loop that will very quickly throw a stack overflow, so it's totally useless in practice.
Sorry if this sounds pedantic, but with most readers being newbies I think it's important to be precise about these things. I'll go away now and leave you all in peace...

Edited 5 Years Ago by JamesCherrill: n/a

This perfectly valid source code does not generate a syntax error, nor does it try to return a method. It's a simple recursive call that returns the value returned from calling classlist().

nyeah, I ment to say that he would be trying to return the 'value returned by that method', not the variable, I didn't really chose my words well.

and as for error, it still is a logical error :)

"a logical error" is a polite way of putting it. "Heap of steaming dingo's kidneys" is about right. But yes, I was being pedantic. ;-)

Hi stultuske and stevanity
I just picked up on this thread, and there's one post I wanted to clarify...

public String classlist(){
    return classlist();
  }

This perfectly valid source code does not generate a syntax error, nor does it try to return a method. It's a simple recursive call that returns the value returned from calling classlist(). Mind you. although the syntax is valid, the runtime effect is simply an infinite recursive loop that will very quickly throw a stack overflow, so it's totally useless in practice.
Sorry if this sounds pedantic, but with most readers being newbies I think it's important to be precise about these things. I'll go away now and leave you all in peace...

Well, we'll be more precise next time. Thanks James. Youve made a really valid point there.

This article has been dead for over six months. Start a new discussion instead.