变量g可能尚未初始化
我对这个项目有很多疑问,我正在处理中。这是一个电影虚拟数据库。我有一个小的MovieEntry类(用于处理单个条目)和一个跟踪所有10k +条目的大型MovieDatabase类。在我的第二个searchYear方法以及随后的方法中,我得到错误“变量g(或d或其他)可能没有被初始化。” 我也有一个弹出式错误,说Warnings from last compilation: unreachable catch clause. thrown type java.io.FileNotFoundException has already been caught.
我对这两个都很肯定。下面的代码:变量g可能尚未初始化
public class MovieDatabase
{
private ArrayList<MovieEntry> Database = new ArrayList<MovieEntry>();
public MovieDatabase(){
ArrayList<MovieDatabase> Database = new ArrayList<MovieDatabase>(0);
}
public int countTitles() throws IOException{
Scanner fileScan;
fileScan = new Scanner (new File("movies.txt"));
int count = 0;
String movieCount;
while(fileScan.hasNext()){
movieCount = fileScan.nextLine();
count++;
}
return count;
}
public void addMovie(MovieEntry m){
Database.add(m);
}
public ArrayList<MovieEntry> searchTitle(String substring){
for (MovieEntry title : Database)
System.out.println(title);
return null;
}
public ArrayList<MovieEntry> searchGenre(String substring){
for (MovieEntry genre : Database)
System.out.println(genre);
return null;
}
public ArrayList<MovieEntry> searchDirector (String str){
for (MovieEntry director : Database)
System.out.println(director);
return null;
}
public ArrayList<String> searchYear (int yr){
ArrayList <String> yearMatches = new ArrayList<String>();
for (MovieEntry m : Database)
m.getYear(yr);
if(yearMatches.contains(yr) == false){
String sYr = Integer.toString(yr);
yearMatches.add(sYr);
}
return yearMatches;
}
public ArrayList<MovieEntry> searchYear(int from, int to){
ArrayList <String> Matches = new ArrayList<String>();
for(MovieEntry m : Database);
m.getYear();
Matches.add();
return Matches;
}
public void readMovieData(String movies){
String info;
try{
Scanner fileReader = new Scanner(new File("movies"));
Scanner lineReader;
while(fileReader.hasNext()){
info = fileReader.nextLine();
lineReader = new Scanner(info);
lineReader.useDelimiter(":");
String title = lineReader.next();
String director = lineReader.next();
String genre = lineReader.next();
int year = lineReader.nextInt();
}
}catch(FileNotFoundException error){
System.out.println("File not found.");
}catch(IOException error){
System.out.println("Oops! Something went wrong.");
}
}
public int countGenres(){
ArrayList <String> gList = new ArrayList<String>();
for(MovieEntry m : Database){
String g = m.getGenre(g);
if(gList.contains(g) == false){
gList.add(g);
}
return gList.size();
}
}
public int countDirectors(){
ArrayList <String> dList = new ArrayList<String>();
for(MovieEntry m : Database){
String d = m.getDirector(d);
if(dList.contains(d) == false){
dList.add(d);
}
return dList.size();
}
}
public String listGenres(){
ArrayList <String> genreList = new ArrayList<String>();
}
}
catch(IOException error){
System.out.println("Oops! Something went wrong.");
}
它告诉你的是,FileNotFoundException
会处理一下IOException就会被抓,所以IOException异常变得不可作为它永远赶不上一个IO exceltion,为什么只是没有赶上一个Exception而不是
至于初始化
public int countDirectors(){
ArrayList <String> dList = new ArrayList<String>();
for(MovieEntry m : Database){
String d = m.getDirector(d); //THIS LINE
if(dList.contains(d) == false){
dList.add(d);
}
return dList.size();
}
线String d = m.getDirector(d);
可能是问题,d
不会被初始化,除非有东西在MovieEntry并尽可能我可以看到,因为你是它初始化为空数组列表绝不会有任何东西
ArrayList<MovieDatabase> Database = new ArrayList<MovieDatabase>(0);
也许你应该传递的数组电影的构造函数,然后将这些电影添加到Database
变量?
似乎这个代码有很多问题。
MovieEntry.getGenre()期望什么参数?在这种情况下,您可能不会使用g,因为它尚未定义。
您提到的异常问题意味着异常已被捕获,或者可能永远不会抛出。我相信在这种情况下,IOException永远不会从try块中的代码中抛出。
有一些的,都应该返回一个值的方法,但不这样做,例如:
public String listGenres(){
ArrayList <String> genreList = new ArrayList<String>();
}
此外,它是一个Java命名约定使用值较低的情况下,第一个字符(骆驼) :
private ArrayList<MovieEntry> database = new ArrayList<MovieEntry>();
哦,你需要在构造函数?:
public MovieDatabase(){
ArrayList<MovieDatabase> Database = new ArrayList<MovieDatabase>(0);
}
重新初始化数据库变量10
希望这有帮助。
可以请您告诉我们,如果它的'g'或'd',所以我们知道代码的哪一部分要看? – 2012-03-29 19:27:31
你将不得不比'变量g(或d或其他)'更具体一些。线条有帮助。具体的变量名称帮助更多。 – 2012-03-29 19:30:08