我试图为理论视频商店制作面向对象的库存系统。
我不断收到以下错误消息:
non-static variables xyz cannot be accessed from a static context.
我在静态上下文中找到的所有信息都是针对一个方法是静态而另一个方法不是静态的,但我的方法都不是静态的。
在这段代码中,我收到两次错误消息,我不明白为什么。
if (enterOption == 1) {
Movie movieNew = new Movie (titleInput, yearInput, directorInput, ratingInput, genreInput);
VideoShop.movies.add(movieNew);
} else {
UI.runUI();
}
我是从VideoShop.movies.add(movieNew);
和UI.runUI();
方法调用中获得的。
完整方法:
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();
}
}
答案 0 :(得分:4)
VideoShop.movies
可能是非静态字段。您应该创建一个对象,而不是使用VideoShop.movies
:
VideoShop shop = new VideoShop();
shop.movies.add(movieNew);
UI
:
UI ui = new UI();
ui.runUI();