You seem to be a student, and you seem to have some misconceptions about how arrays and so forth. As such, I'll try to explain as best I can.
First off, in your
main
method you create an array of
BtechStudent
objects. When you create a array, you specify the size (in this case, you used
3
). This creates "placeholders" for 3
BtechStudent
objects. It does not create the objects. in your loop, when you do
b[i].registration();
, there is no object on which to invoke the
registration
method. To fix this problem, you need to create an object and place it in the array before you try to call the
registration
method. So, that changes your loop to:
for(int i=0;i<3;i++)
{
b[i] = new BtechStudent();
b[i].registration();
}
Secondly, when you create your
registration
method, you specify that it will return a
BtechStudent
object, but it never returns anything. At the end of the method, you need to have a
return
statement. Since you are returning a
BtechStudent
object from the
BtechStudent
class, you will probably want to use
return this;
as your
return
statement.
this
is a special keyword that refers to the object on which you are currently executing a method.
That said, you should probably not return a
BtechStudent
object in that method anyway, since it would be redundant. Unless you have some reason for wanting to return a
BtechStudent
object, I suggest you change the method declaration to
void registration() throws IOException
void
means that the method does not return anything, so you can just call it to do some work or change the object in some way.
Next up is global variables. In your
registration
method, you capture some details and store it in local variables. This means that, as soon as the method ends, the variables are no longer accessible. Essentially, it means you asked the user to capture data and then you threw that data away. Instead, create some global variables in the
BtechStudent
class and store the captured data in them. This ensures that as long as you have the object, you have the data that the user captured. By now, your
BtechStudent
class looks like this:
class BtechStudent
{
public String name;
public String branch;
public String idNumber;
void registration() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter name");
name=br.readLine();
System.out.println("branch");
branch=br.readLine();
System.out.println("idNumber");
idNumber=br.readLine();
}
}
Lastly, in your
main
method, you try to print the
BtechStudent
object. The problem is, it's a class you created, so Java has no idea how to print it. That will result in Java printing some useless crap instead of the name, branch and ID number you expect to see. In order to tell Java what to print when you try to print the object, you need to create a
toString()
method in your
BtechStudent
class. It should look something like this:
public string toString()
{
return "Name='" + name + "', Branch='" + branch + "', ID Number='" + idNumber + "'.";
}