首页 > 解决方案 > 如何使用构造函数设置不在构造函数内的其他字段

问题描述

我正在猜测我从文本文件中获取电影列表的电影游戏。我有两个课程用于Game获取随机电影和Main游戏的其余部分。现在我想通过更改Game. 我分别将“h”或“b”作为输入。我调用Game带有参数的构造函数来相应地选择文件,但它不起作用,并且文件总是null显示NullPointerException

此图像显示了调试期间发生的情况。它跳过setMovieListand 构造函数并进入下一行

编辑:我是 OOP 的新手,所以请多多包涵。我刚刚在调试期间看到调试器首先进入类字段,然后进入构造函数,我实际上试图使用file(在构造函数内部)初始化其他字段,因为它的值是null并且正在显示NullPointerException

现在我的问题仍然是如何使用filenoOfMovies初始化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];


    }




标签: javaoopconstructor

解决方案


我不确定..也许你想得到这样的结果:

游戏类

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 字段,因为它会自动获取列表大小。

希望能帮助到你...


推荐阅读