I';我是一名新程序员,每次运行这段代码时,Java中都会出现空指针异常
这是对main方法中的类的调用 String word=theScramble.getRealWord();I';我是一名新程序员,每次运行这段代码时,Java中都会出现空指针异常,java,nullpointerexception,Java,Nullpointerexception,这是对main方法中的类的调用 String word=theScramble.getRealWord(); 我应该更改哪些部分以避免空指针异常您正在本地范围中声明变量。在您的置乱构造函数中: //This Is the Class private String words; private File textFile; private Scanner inputFile; StringBuilder stringBuilder = new StringBuilder(); public Scr
我应该更改哪些部分以避免空指针异常您正在本地范围中声明变量。在您的
置乱
构造函数中:
//This Is the Class
private String words;
private File textFile;
private Scanner inputFile;
StringBuilder stringBuilder = new StringBuilder();
public Scramble(String j) throws FileNotFoundException
{
File textFile = new File(j);
Scanner inputFile = new Scanner(textFile);
}
public String getRealWord() throws IOException
{
//Make the loop while !null
//if null close the document
while (inputFile.hasNext())
{
stringBuilder.append(inputFile);
}
inputFile.close();
return stringBuilder.toString();
}
…正在声明一个名为textFile
的新变量,该变量隐藏了名为textFile
的实例成员
您将要更改:
File textFile = new File(j);
致:
这样,您引用的是实例变量,而不是局部变量。您是在局部范围内声明变量。在您的
置乱
构造函数中:
//This Is the Class
private String words;
private File textFile;
private Scanner inputFile;
StringBuilder stringBuilder = new StringBuilder();
public Scramble(String j) throws FileNotFoundException
{
File textFile = new File(j);
Scanner inputFile = new Scanner(textFile);
}
public String getRealWord() throws IOException
{
//Make the loop while !null
//if null close the document
while (inputFile.hasNext())
{
stringBuilder.append(inputFile);
}
inputFile.close();
return stringBuilder.toString();
}
…正在声明一个名为textFile
的新变量,该变量隐藏了名为textFile
的实例成员
您将要更改:
File textFile = new File(j);
致:
这样,您引用的是实例变量,而不是局部变量。您是在局部范围内声明变量。在您的
置乱
构造函数中:
//This Is the Class
private String words;
private File textFile;
private Scanner inputFile;
StringBuilder stringBuilder = new StringBuilder();
public Scramble(String j) throws FileNotFoundException
{
File textFile = new File(j);
Scanner inputFile = new Scanner(textFile);
}
public String getRealWord() throws IOException
{
//Make the loop while !null
//if null close the document
while (inputFile.hasNext())
{
stringBuilder.append(inputFile);
}
inputFile.close();
return stringBuilder.toString();
}
…正在声明一个名为textFile
的新变量,该变量隐藏了名为textFile
的实例成员
您将要更改:
File textFile = new File(j);
致:
这样,您引用的是实例变量,而不是局部变量。您是在局部范围内声明变量。在您的
置乱
构造函数中:
//This Is the Class
private String words;
private File textFile;
private Scanner inputFile;
StringBuilder stringBuilder = new StringBuilder();
public Scramble(String j) throws FileNotFoundException
{
File textFile = new File(j);
Scanner inputFile = new Scanner(textFile);
}
public String getRealWord() throws IOException
{
//Make the loop while !null
//if null close the document
while (inputFile.hasNext())
{
stringBuilder.append(inputFile);
}
inputFile.close();
return stringBuilder.toString();
}
…正在声明一个名为textFile
的新变量,该变量隐藏了名为textFile
的实例成员
您将要更改:
File textFile = new File(j);
致:
这样,您指的是实例变量,而不是局部变量