我已经对有关2D数组和NullPointerException
(NPE)的类似问题进行了尽可能多的研究,但没有找到与我的情况相似的答案。
我的程序应该是非常基本的:获取一个输入值“ image”的整数值文件,并通过取每个值的平均值来“软化”这些值。
我在使用while循环将文件复制到二维数组的初始过程中遇到了麻烦,尽管循环似乎并不是问题,因为我尝试了do
-{{1 }}已经循环。
我最初尝试使用while
复制数组,但是最初给了我NPE,所以我尝试编写自己的静态方法来完成这项工作,因为我在某处读到Arrays.copyOf
仅适用一维数组。
Arrays.copyOf
NPE如下
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;
}
}
答案 0 :(得分:0)
感谢阅读此问题的任何人试图提供帮助,但我通过手动“调试”逻辑来弄清楚了。
本质上,我编写了一个较小的测试程序,该程序只处理一维数组中的单行输入,并注意到我的smCopyArrayOuter
和smCopyArrayInner
都变得比需要的大。< / p>
本质上就是行
int[][] AyY = new int[ AyX.length +growth ][];
成为
int[][] AyY = new int[ 1 +growth ][];
解决了这个问题。
我还可以取消temp
数组并直接处理AyHardImg
,您可以查看是否检查了gitHub的回购,它是github.com/q1pt2rx/ex7_imageSmoother。
在发布此答案时,程序尚未完成,但此NPE问题已解决。