Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Why does my code not work?

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    22
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Why does my code not work?

    Why does my code not work?

    package demo;
     
    import java.util.List;
     
    public class Student {
        private String naam;
        private int num;
        private List<Student> students;
     
        public Student() {
        }
     
        public Student(String naam, int num) {
            this.naam = naam;
            this.num = num;
        }
     
        public void setStudents(Student student){
            students.add((Student) students);
        }
     
        public List<Student> getStudents() {
            return students;
        }
     
        public static void main(String[] args) {
            Student student = new Student();
            student.setStudents(new Student("Jen", 27));
            student.setStudents(new Student("Ruurs", 28));
     
            List<Student> stud = student.getStudents();
     
            for (Student students: stud){
                System.out.println("Name "+ students.naam + " number "+ students.num);
            }
     
        }
    }

    I don't get a clear error message

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why does my code not work?

    Please copy the full text of the error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2019
    Posts
    22
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Why does my code not work?

    Task :Student.main() FAILED
    Exception in thread "main" java.lang.NullPointerException
    at demo.Student.setStudents(Student.java:19)
    at demo.Student.main(Student.java:28)

    FAILURE: Build failed with an exception.

    * What went wrong:
    Execution failed for task ':Student.main()'.
    > Process 'command 'C:/Program Files/Java/jdk-11.0.2/bin/java.exe'' finished with non-zero exit value 1

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why does my code not work?

    Exception in thread "main" java.lang.NullPointerException
    at demo.Student.setStudents(Student.java:19)
    There is a variable with a null value on line 19 when it is executed.
    Look at line 19 and find the variable with the null value,
    then backtrack in the code to find out why that variable does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why does my code not work?

    Did you mean student in the add method?

    [code]
    public void setStudents(Student student){
    students.add((Student) student);
    }
    [code]

Similar Threads

  1. Replies: 6
    Last Post: August 5th, 2014, 11:19 AM
  2. why my code does not work ?? ://
    By without name in forum What's Wrong With My Code?
    Replies: 13
    Last Post: May 6th, 2014, 02:35 PM
  3. Why does this code now work?
    By Tuidog in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 6th, 2014, 08:06 AM
  4. Help me I am trying to get this code to work.
    By Leonardo1143 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 26th, 2013, 07:41 PM
  5. please tell me why this code does not work
    By amr in forum Java Theory & Questions
    Replies: 9
    Last Post: December 6th, 2010, 06:46 PM