-
arraylist
i am making three classes student school and teststudent, in student class student name,subject age is defined and in school class student objects are created using arraylist, but in my program i am unable to access the method of student class,whats wrong with my code
public class Student {
String name;
String subject;
int age;
Student(String name,String subject,int age){
this.name = name;
this.subject = subject;
this.age = age;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public String getSubject(){
return this.subject;
}
public int getAge(){
return this.age;
}
}
import java.util.*;
public class School {
public ArrayList <Student> students = new ArrayList <Student>();
public void addStudent(String name,String subject,int age){
Student newStudent = new Student(name,subject,age);
students.add(newStudent);
}
public void showSubject(String student){
newStudent.getSubject();
}
}
-
Re: arraylist
You have defined the newStudent inside the addStudent() and you are accessing the newStudent in showSubject(). This is the root of problem.
Either pass newStudent to showSubject() or declare newStudent at class level.
-
Re: arraylist
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
-
Re: arraylist
Hi ankit, Pls try this code to access the subject of perticular student.
..code removed
-
Re: arraylist
@basha2013, please read the forum rules and the following:
http://www.javaprogrammingforums.com...n-feeding.html