java - 如何使用构造函数设置不在构造函数内的其他字段
问题描述
我正在猜测我从文本文件中获取电影列表的电影游戏。我有两个课程用于Game
获取随机电影和Main
游戏的其余部分。现在我想通过更改Game
. 我分别将“h”或“b”作为输入。我调用Game
带有参数的构造函数来相应地选择文件,但它不起作用,并且文件总是null
显示NullPointerException
。
此图像显示了调试期间发生的情况。它跳过setMovieList
and 构造函数并进入下一行
编辑:我是 OOP 的新手,所以请多多包涵。我刚刚在调试期间看到调试器首先进入类字段,然后进入构造函数,我实际上试图使用file
(在构造函数内部)初始化其他字段,因为它的值是null
并且正在显示NullPointerException
。
现在我的问题仍然是如何使用file
和noOfMovies
初始化Game
.
//showing the setter method that i tried
//Main class
/*only showing the part having Game class*/
//making an object of Game class to get a random movie from the file
System.out.println("Enter 'h' for hollywood and 'b' for bollywood ");
Scanner input = new Scanner(System.in);
char genre = input.next().charAt(0);
Game newGame = new Game(genre);
//Game class
public class Game
{
public Game(char genre)
{
setMovieList(genre);
}
File file;
int noOfMovies;
public void setMovieList(char genre)
{
if(genre == 'h')
{
this.file = new File("C:\\Users\\Rashim\\Desktop\\java\\GuessTheMovie\\src\\hollywoodMovies.txt");
this.noOfMovies = 30;
}
else if(genre == 'b')
{
this.file = new File("C:\\Users\\Rashim\\Desktop\\java\\GuessTheMovie\\src\\bollywoodMovies.txt");
this.noOfMovies = 20;
}
// EDIT ------> I want to initialize the below fields <-------
private Scanner scan = new Scanner(this.file);
private int lineCount = 0;
int random = (int)(Math.random()*noOfMovies)+1;
//array for storing the movie titles
private String[] movieArray = new String[noOfMovies];
}
解决方案
我不确定..也许你想得到这样的结果:
游戏类
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Game {
private File file = null;
//private int noOfMovies = 0;
private List<String> movies= null;
FileInputStream read = null;
public Game(char genre) {
movies = getMovieList();
System.out.println(movies);
}
public void setMovieList(char genre) {
if (genre == 'h') {
this.file = new File("C:\\Users\\Rashim\\Desktop\\java\\GuessTheMovie\\src\\hollywoodMovies.txt");
// this.noOfMovies = 30;
} else if (genre == 'b') {
this.file = new File("C:\\Users\\Rashim\\Desktop\\java\\GuessTheMovie\\src\\bollywoodMovies.txt");
// this.noOfMovies = 20;
}
}
public List<String> getList() {
List<String> movieList = new ArrayList<>();
String[] values = null;
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
values = line.split(";");
movieList.add(values[0]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return movieList;
}
public String getMovie(){
System.out.println(movies.size());
int min = 1;
int max = movies.size();
int random = min + (int) (Math.random() * (max - min));
System.out.println(random);
String title = movies.get(random);
return title;
}
}
主要课程
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Enter 'h' for hollywood and 'b' for bollywood ");
Scanner input = new Scanner(System.in);
char genre = input.next().charAt(0);
Game newGame = new Game(genre);
String randomMovie = newGame.getMovie();
System.out.println(randomMovie);
}
}
请注意,我已经使用 List 数据结构代替了数组,但这显然取决于您...如果这看起来像您正在尝试做的事情,请告诉我...当然可以进行一些其他改进,但应该可以。
它还假设您有一个 txt 文件,其中电影标题用分号分隔...否则您必须调整 getList one 中的 split 方法..
此外,您不再需要 noOfMovies 字段,因为它会自动获取列表大小。
希望能帮助到你...
推荐阅读
- javascript - 从多个用户列表中选择他/她后,如何导航到特定用户的个人资料页面?
- android - 启用后退按钮或 hamberger 按钮时,位于工具栏标题末尾的自定义工具栏侧 textView 被分离
- php - 为什么 PHP 5.5 在 Windows 10 命令提示符中不生成 ANSI 颜色?
- google-cloud-platform - 谷歌云端点 API 密钥未验证
- python - 错误:找不到 pg_config 可执行文件。即使我在环境变量上添加了 pg_config 的路径
- php - 为什么 null 语句在我的 sql 查询中不起作用?
- java - 没有密钥,缓存驱逐不起作用,方法没有参数?
- tensorflow - 如何在 Keras 模型定义中索引张量列表
- ios - ReactiveSwift 缓冲管道
- ssl - Google Cloud Platform 上的 Bitnami 启用 SSL 问题:请在启用 SSL 时修复 DNS 条目