stca11
stca11

Reputation: 1

Why does it say it is static when the method isn't static?

I am attempted to make an object oriented stock system for a theoretical video shop.

I am constantly getting the following error message:

non-static variables xyz cannot be accessed from a static context.

All the info I have found on static contexts has been for when one method is static and the other is not, however none of my methods are static.

In this piece of code I get that error message twice and I don't understand why.

if (enterOption == 1) {
    Movie movieNew = new Movie (titleInput, yearInput, directorInput, ratingInput, genreInput);
    VideoShop.movies.add(movieNew);  
} else {
    UI.runUI();
}

I get it from VideoShop.movies.add(movieNew); and the UI.runUI(); method call.

Full method:

public void createMovie ()
{
    Scanner sc = new Scanner (System.in);

    System.out.println ("Title: ");
    String titleInput = sc.next();

    System.out.println ("Year: ");
    int yearInput = sc.nextInt();

    System.out.println ("Director: ");
    String directorInput = sc.next();

    System.out.println ("Rating [G / PG / M / MA15 / R18]: ");
    String ratingInput = sc.next();

    System.out.println ("Genre [a - Action/ b - Drama/ c - Comedy/ d - Musical/ e - Family/ f - Documentary]: ");
    String genreInput = sc.next();

    System.out.println ("Format [VCD/DVD]: ");
    String formatInput = sc.next();

    System.out.println ("Cost: ");
    double costInput = sc.nextDouble(); 

    System.out.println ("Quantity: ");
    int quantityInput = sc.nextInt();


    System.out.println("Confirm?");
    System.out.println("1. Yes 2. No, return to main menu");

    System.out.println("Enter option: ");
    int enterOption = sc.nextInt();

    if (enterOption == 1) {
        Movie movieNew = new Movie (titleInput, yearInput, directorInput, ratingInput, genreInput);
        VideoShop.movies.add(movieNew);

    } else {
        UI.runUI();
    }
}

Upvotes: 0

Views: 128

Answers (1)

Tagir Valeev
Tagir Valeev

Reputation: 100239

It's likely that VideoShop.movies is non-static field. Instead of using VideoShop.movies you should create an object:

VideoShop shop = new VideoShop();
shop.movies.add(movieNew);

The same for UI:

UI ui = new UI();
ui.runUI();

Upvotes: 4

Related Questions