-4

私はグレースケール画像圧縮プログラムに取り組んでおり、これまでのところ(ほぼ)画像を2D配列に変換することができました。以下のコードは私が今持っているものです。私には問題ないように見えますが、実行すると、コードの15行目でnullポインター例外エラーが発生します。これがメインであり、エラーは以下に記述されています。

どんな助けでも本当にありがたいです!! =)

コードとメインは次のとおりです。

import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.File;
import java.io.*;
import javax.imageio.ImageIO;

public class gsTo2d {

private static String dir="C:/Documents and Settings/Administrator/workspace/GrayScaleBitmapCompressor/inputImage"; // add here the directory to the folder contains the image

public int [][] compress() throws IOException
{
    File file = new File(dir , "2.TIF");// file object to get the file, the second argument is the name of the image file
    BufferedImage image = ImageIO.read(file);
    Raster image_raster = image.getData();

    int[][] original; // where we'll put the image

    //get pixel by pixel
    int[] pixel = new int[1];
    int[] buffer = new int[1];

    // declaring the size of arrays
    original = new int[image_raster.getWidth()][image_raster.getHeight()];


    //get the image in the array
    for(int i = 0 ; i < image_raster.getWidth() ; i++)
        for(int j = 0 ; j < image_raster.getHeight() ; j++)
        {
            pixel = image_raster.getPixel(i, j, buffer);
            original[i][j] = pixel[0];
        }
    return original;                   

}
}

public static void main(String[] args) throws IOException{

    gsTo2d obj = new gsTo2d();
    int[][] imageArray = obj.compress();
    System.out.println(imageArray);     

}

エラーは次のとおりです。

Exception in thread "main" java.lang.NullPointerException
at gsTo2d.compress(gsTo2d.java:15)a
at convTest.main(convTest.java:9)
4

1 に答える 1

0

登録された ImageReader が次の行の画像を読み取ることができない場合、画像は null になります。

BufferedImage image = ImageIO.read(file);

これにより、次の行で NullPointerException がトリガーされます。

多分あなたのイメージは何らかの形で無効ですか?行を改行して、画像が null かどうかを確認してください。

于 2013-01-21T20:37:01.813 に答える