java - NullPointerException 复制不断增长的内部数组和外部数组
问题描述
我已经对有关 2D 数组和 (NPE) 的类似问题进行了尽可能多的研究NullPointerException
,但没有找到与我的情况相似的答案。
我的程序应该是非常基本的:获取一个整数值的输入“图像”文件,并通过取每个值的平均值来“软化”这些值。
我在将文件复制到带有 while 循环的二维数组的初始过程中遇到了问题,尽管循环似乎不是问题,因为我已经尝试过do
-while
循环。
我最初尝试使用Arrays.copyOf
来复制数组,但这最初给了我一个 NPE,所以我尝试编写自己的静态方法来完成这项工作,因为我在某个地方读到了Arrays.copyOf
只适用于一维数组的地方。
public class ex7_imageSmoother {
public static void main ( String[] args ) throws IOException {
// build utility object(s)
Scanner ScUser = new Scanner( System.in );
// ph for ascii art
System.out.println( "\n\nAre your ready to Soften some \"hard\" files?" );
////..repeat program by prompt
String stRepeat1;
do {
// get hard file name to be softened
System.out.print( "\n\nWhat file would you like to soften? " );
String StHardName = ScUser.nextLine().trim();
File FiHardIn = new File ( StHardName );
Scanner ScHardIn = new Scanner( FiHardIn );
//-- build 2d "Hard" array
// array will be of at least one cell
int[][] AyHardImg = { { 0 } } ;
int iRowCount = 0;
//// for every line in the file; i.e. check that there is a next line
while (ScHardIn.hasNextLine() ) {
// create a string that can be read through by scanner for every line of the file
String StInLine = ScHardIn.nextLine();
// build scanner to read through each row
Scanner ScInLine = new Scanner( StInLine );
// use static method to copy array; make larger on further iterations
AyHardImg = smCopyArrayOuter( AyHardImg, iRowCount );
int iColCount = 0;
//// for every integer in the row
while ( ScInLine.hasNextInt() ) {
// create temporary array in an attempt to circumvent NPE
int[] temp = new int[ AyHardImg[ iRowCount ].length ]; // ...--... this line creates the NPE!!
// copy into temp array all values from inner array of 2d array
for (int i = 0; i < AyHardImg[ iRowCount ].length; i++) {
temp[ i ] = AyHardImg[ iRowCount ][ i ];
}
// copy array and make larger on further iteration
temp = smCopyArrayInner( temp, iColCount );
// use temp array to copy into 2d inner array; included is commented out previous attempt without temp array
AyHardImg[ iRowCount ] = temp; //= smCopyArray( AyHardImg[ iRowCount ], iColCount );
AyHardImg[ iRowCount ][ iColCount ] = ScInLine.nextInt();
iColCount++;
}
iRowCount++;
ScInLine.close();
}
// test algorithm works as intended by printing hard array to screen
for ( int i = 0; i < AyHardImg.length; i++ ) {
for ( int j = 0; j < AyHardImg[i].length; j++ ) {
System.out.print ( AyHardImg[ i ][ j ] + " " );
}
System.out.println();
}
ScHardIn.close();
// get user response to repeat program
System.out.print( "Would you like to soften another file (y/n)? " );
stRepeat1 = ScUser.nextLine().trim().toLowerCase();
} while ( stRepeat1.equals( "y" ) );
}
/*-----
* copies inner array, hopefully to solve null
* pointer exception
*------------------*/
public static int[] smCopyArrayInner( int[] AyX, int growth ) {
int[] AyY = new int[ AyX.length +growth ];
for ( int i = 0; i < AyX.length; i++ ) {
AyY[ i ] = AyX[ i ];
}
return AyY;
}
/*-----
* copies outer array, hopefully to solve null
* pointer exception
*------------------*/
public static int[][] smCopyArrayOuter( int[][] AyX, int growth ) {
int[][] AyY = new int[ AyX.length +growth ][];
for ( int i = 0; i < AyX.length; i++ ) {
AyY[ i ] = AyX[ i ];
}
return AyY;
}
}
NPE如下
Exception in thread "main" java.lang.NullPointerException
at ex7_imageSmoother.main(ex7_imageSmoother.java:101)
解决方案
感谢任何阅读此问题以提供帮助的人,但我通过手动“调试”逻辑来解决这个问题。
本质上,我编写了一个较小的测试程序,它只处理单行输入到一维数组中,并注意到我的smCopyArrayOuter
和smCopyArrayInner
都变得比它们需要的大。
本质上是一条线
int[][] AyY = new int[ AyX.length +growth ][];
变成了
int[][] AyY = new int[ 1 +growth ][];
这解决了这个问题。
我还能够取消temp
数组并AyHardImg
直接处理,如果您查看 gitHub 上的 repo github.com/q1pt2rx/ex7_imageSmoother 就可以看到。
在发布此答案时,该程序不完整,但此 NPE 问题已解决。