【发布时间】:2018-10-06 22:26:03
【问题描述】:
首先,为了快速了解一下,这是我昨天的帖子:
How to work around a NullPointerException in Java?
所以我得到了这个 NullPointerException,我现在相信它发生在我尝试在字符串数组中找到第一个副本的索引之前。在搜索第一个副本的索引之前,我使用此方法将字符串数组的大小加倍:
static String[] upSizeArr( String[] fullArr )
{
int size = fullArr.length;
String[] newSizeArr = new String[(2 * size)];
for (int a = 0; a < size; a++) {
newSizeArr[a] = fullArr[a];
}
return newSizeArr;
}
然后我在这个 while 循环的上下文中使用该方法:
static final int CAPACITY = 10;
int wordCount = 0;
BufferedReader wordFile = new BufferedReader( new FileReader(args[1]) );
String[] wordList = new String[CAPACITY];
while ( wordFile.ready() )
{ if ( wordCount == wordList.length )
wordList = upSizeArr( wordList );
wordList[wordCount++] = wordFile.readLine();
}
wordFile.close();
是否可以使用 upSizeArr 方法解决此问题?我希望解决方案是基本的,并且只使用没有其他数据结构的数组。我是编程新手,我真的很想掌握基础知识……大约一周左右一直在寻找解决这个 NullPointException 的方法。
这是完整的代码:
import java.io.*;
import java.util.*;
public class Practice
{
static final int CAPACITY = 10;
static final int NOT_FOUND = -1;
public static void main (String[] args) throws Exception
{
if (args.length < 1 )
{
System.out.println("\nusage: C:\\> java Practice <words filename>\n\n"); // i.e. C:\> java Lab2 10Kints.txt 172822words.txt
System.exit(0);
}
String[] wordList = new String[CAPACITY];
int wordCount = 0;
BufferedReader wordFile = new BufferedReader( new FileReader(args[0]) );
while ( wordFile.ready() ) // i.e. while there is another line (word) in the file
{ if ( wordCount == wordList.length )
wordList = upSizeArr( wordList );
wordList[wordCount++] = wordFile.readLine();
} //END WHILE wordFile
wordFile.close();
System.out.format( "%s loaded into word array. size=%d, count=%d\n",args[0],wordList.length,wordCount );
int dupeIndex = indexOfFirstDupe( wordList, wordCount );
if ( dupeIndex == NOT_FOUND )
System.out.format("No duplicate values found in wordList\n");
else
System.out.format("First duplicate value in wordList found at index %d\n",dupeIndex);
} // END OF MAIN
// TWO METHODS
static String[] upSizeArr( String[] fullArr )
{
int size = fullArr.length; //find the length of the arrays
String[] newSizeArr = new String[(2 * size)]; // creates new array, doubled in size
for (int a = 0; a < size; a++) {
newSizeArr[a] = fullArr[a];
}
return newSizeArr;
}
static int indexOfFirstDupe( String[] arr, int count )
{
Arrays.sort(arr);
int size = arr.length;
int index = NOT_FOUND;
for (int x = 0; x < size; x++) {
for (int y = x + 1; y < size; y++) {
if (arr[x].equals(arr[y])) {
index = x;
break;
}
}
}
return index;
}
} // END OF PROGRAM
另外,用作参数的文件是一个 txt 字符串文件。
【问题讨论】:
-
我没有看到您提供的任何会取消引用 null 的代码。你能提供更多的上下文/代码吗?异常到底是在哪里抛出的?在您链接到的原始帖子中,异常是由于数组的一半(调整大小后的“新”一半)包含
nulls,Arrays.sort将抛出一个尝试比较nulls 的异常。这可以通过创建一个处理null的比较器来解决,如下所示:stackoverflow.com/questions/14514467/… -
我推荐
ArrayList或System.arraycopy -
wordFile.ready()没有做你认为的事情 -
哪一行实际抛出异常?
-
所以我对您的代码进行了非常快速的破解,没有尝试读取文件,并且它不会生成 NPE。我怀疑
wordFile.ready()和wordList[wordCount++] = wordFile.readLine()的组合会将null值放入数组中(wordFile.readLine()将在到达文件末尾时返回null)。
标签: java arrays nullpointerexception