1

new to java, and i'm making a student record system. I want to use the specified year stored in the arraylist in this code (i have used an array list as i plan on eventually making this loop to allow multiple inputs)

    import java.util.ArrayList;
import java.util.Scanner;

public class StudentYear 
{
    public ArrayList<Integer> studentYear;
    public void StudentYear()
    {
        studentYear = new ArrayList<Integer>();
        Scanner sc = new Scanner(System.in); 
        System.out.println("Please enter the year the student has most recently completed: ");
        studentYear.add(sc.nextInt());
        System.out.println(studentYear);
        }
    public ArrayList<Integer> getStudentYear()
    {
        return studentYear;
    }
}

This is the code for the class that i want the stored year value in the arraylist studentYear, from the above code to go into

   import java.util.ArrayList;
import java.util.Scanner;

public class Results 
{
    public static void Results(StudentYear studentYear)
    {

        int year = studentYear.getStudentYear().get(0); 
        ArrayList<Integer> results = new ArrayList<Integer>();
        Scanner sc = new Scanner(System.in); 
        System.out.println("Please enter the results for "+year+":");
        results.add(sc.nextInt());
        System.out.println(results);

        }
    }

this is my main code

StudentYear yearInputobject = new StudentYear();
yearInputobject.StudentYear();  


Results resultsInputobject = new Results();
resultsInputobject.Results();  //error here < The method Results(StudentYear) in the type Results is not applicable for the arguments ()

Any help would be greatly appreciated, ive been stuck on this for hours.

Edit: error now

exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method Results(StudentYear) in the type Results is not applicable for the arguments ()

    at Main.main(Main.java:21)

results code once again

import java.util.ArrayList;
import java.util.Scanner;

public class Results 
{
    public static void Results(StudentYear studentYear)
    {

        int year = studentYear.getStudentYear().get(0); 
        ArrayList<Integer> results = new ArrayList<Integer>();
        Scanner sc = new Scanner(System.in); 
        System.out.println("Please enter the results for "+year+":");
        results.add(sc.nextInt());
        System.out.println(results);

        }
    }
2
  • A NullPointerException is not a compile-time error. And, I thought you said you had a compile-time error when using get. Did you update your question with a new question? Commented Nov 30, 2013 at 20:28
  • that error has gone now :) Commented Nov 30, 2013 at 20:38

1 Answer 1

1

You're calling get on a StudentYear object, not the ArrayList.

You already have a method to fetch the ArrayList, so you simply need to use it, and then fetch the element from the ArrayList it returns:

int year = studentYear.getStudentYear().get(0);

As for your second problem:

(First off, the only methods that start with a capital letter is by convention constructors).

You should rework the Results method to a constructor. And, you will then need to move some stuff to a new method.

Here's how I'd construct a Results object:

Change

public static void Results(StudentYear studentYear)...

to

public Results(int year) {
    this.year = year;
}

Note that the rest of the method was removed, and should be in a separate method (to learn why simply look up constructors and their purpose). And the this.year = year row simply means that you have a field called year, just like your studentYear array. It is assigned the value you use when constructing the object. Your other method will then be able to use this value.

When you construct this object you'd do something like:

StudentYear yearInputobject = new StudentYear();
int year = yearInputobject.getStudentYear();
Results resultsInputobject = new Results(year);
resultsInputobject.printResults();

Note that I reworked StudentYear as well, and that I have a method called getStudentYear() which returns an int directly instead.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that got rid of the error there. Now when it comes to compiling i get the error in the main code ( i have updated OP with this). Im confused as to why.
@user3050340 You can't do this: Results resultsInputobject = new Results(); since Results takes a parameter StudentYear. And, only constructors start with a capital letter, and they don't use the keywords static or void. You should read a tutorial.
Just read a chapter on on objects and classes, still pretty confused! When you say rework methods to become constructors, would you be able elaborate a bit? I'v been working on this project all day and have literally got no where simply because im having trouble linking all these classes back to the main class to use their methods. And I will be sure to accept the answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.