I got these two classes. The one below should get an ArrayList with movies...

import java.util.ArrayList ;
import android.app.Activity ;
import android.os.Bundle ;

public class UIDActivity extends Activity {

    private static ArrayList<Movie> movieList;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }

    public static void main(String String, int 
    int args[]


        ) {     

movieList = new ArrayList<Movie>();
        movie1 = new Movie("Fight Club", "David Fincher", "1999", "3");

        movieList.add(movie1);

    }

    public String getTitle() {
        return title;

    }

    public String getDirector() {
        return director;

    }

    public int getYear() {
        return year;
    }

    public int getRating() {
        return rating;
    }
}

this is the Movie class, in which I tell what the attributes are of a Movie

import java.io.Serializable ;

@SuppressWarnings("serial")
public class Movie implements Serializable {

    public String title;
    public String director;
    public int year;
    public int rating;

    public Movie(String deTitle, String deDirector, int hetYear, int deRating) {
        title = deTitle;
        director = deDirector;
        year = hetYear;
        rating = deRating;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setDirector(String director) {
        this.director = director;

    }

    public void setYear(int year) {
        this.year = year;

    }

    public void setRating(int rating) {
        this.rating = rating;

    }
}

For some reason I can't figure out what simple thing I keep forgetting?!?! Eclipse wants to change my public Movie to four times String... No errors there, but rest won't work neither.

share|improve this question
2  
While reformatting your code I notice that your main declaration is public static void main(String String, int int args[]), is that a typo (the 2 int + the weird signature with additional int args[] parameter)? And String String? That code would not compile. Can you post the actual code. – assylias Jul 5 at 17:54
please post the exact error message – matt b Jul 5 at 17:59
Guess a typo, but tried all possible combinations there lol. Got my Movie class sorted out as it should be.. at least I hope at least that is flawless.. But Eclipse keeps giving all kinds of errors on all I want to do with the ArrayList. And when I got rid of the errors over there, then all the sudden the .add doesn't work no more :( – Rick Jul 5 at 18:02
JUG has answered your question. – avgvstvs Jul 5 at 18:07
@mattb the 2nd int gives Syntax error on token "int", invalid VariableDeclaratorId, and my getTitle gives Cannot override the final method from Activity while my other getters are fine accept the return value: cannot be resolved to a variable. I know I'm overlooking something, but made the same thing few days ago and worked perfectly.. and be honest.. this should not be hard. – Rick Jul 5 at 18:09
show 1 more comment
feedback

4 Answers

As far as I am aware, if you create a static variable, you have to initialize it at that point.

Try this:

private static ArrayList<Movie> movieList = new ArrayList<Movie>();

As an aside, shouldn't the getters for UIDActivity be either moved to the Movie class or include an index for which movie you want?

share|improve this answer
Eh? You can use static initializer blocks instead of initializing them in their declaration. – Louis Wasserman Jul 5 at 18:03
Hmm...wasn't aware of that. Good to know. – Wolfman2000 Jul 6 at 17:53
feedback

The problem is that you're trying to use a constructor that takes two Strings and two ints, but passing the constructor 4 strings instead.

The method signature for the constructor in the movie class is

public Movie(String deTitle, String deDirector, int hetYear, int deRating)

The way that you are trying to call the constructor is incorrect.

This:

movie1 = new Movie("Fight Club", "David Fincher", "1999", "3");

Should be:

movie1 = new Movie("Fight Club", "David Fincher", 1999, 3);
share|improve this answer
feedback

You need to have setters/getters only in your Movie class, and additionally, make certain that the class variables are declared private and only accessible through the appropriate getter.

public class Movie implements Serializable {

private String title;
private String director;
private int year;
private int rating;

public Movie(String deTitle, String deDirector, int hetYear, int deRating) {
    title = deTitle;
    director = deDirector;
    year = hetYear;
    rating = deRating;
}

public void setDirector(String director) {
    this.director = director;
}

public String getDirector() {
    return director;
}

...

}

Also, I would define your list using the List interface, and the instantiation as ArrayList.

share|improve this answer
Oke, now I placed the getters back to the Movie class. Now I got problems with my private static ArrayList<Movie> movielist; the ; goes red all the sudden :s just as the ; behind (R.layout.main).. I now get why to put the getters back there.. but getting more confused about the changing errors in the UIDActivity – Rick Jul 5 at 18:27
feedback

Your Movie constructor is expecting two String objects and two int -

public Movie(String deTitle, String deDirector, int hetYear, int deRating) 

But you are passing four String objects -

 movie1 = new Movie("Fight Club", "David Fincher", "1999", "3");

Why are you attempting to pass four String values to Movie constructor instead of two String and two int as expected by the constructor? By the way of your constructor you should create movie object as following -

 movie1 = new Movie("Fight Club", "David Fincher", 1999, 3);
share|improve this answer
Thanks for that.. I can be such an Idiot every now and then.. but still I remain errors in my public static void main and my getters – Rick Jul 5 at 18:14
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.