2014-09-26 74 views
3

我有以下代码在Netbeans中面临NullPointerException错误。代码实际上是用于使用Java框架的GUI。但我编辑只能从cmd使用它。我已经通过代码,但无法找出为什么错误弹出。任何人都可以强调这里的问题是什么?该错误消息指示错误的行是在threeDPixMod的分配和oneDPixNetbeans中的NullPointerException

package image_processor; 


import java.awt.*; 
import java.awt.image.*; 
import java.io.*; 
import javax.imageio.ImageIO; 

class ImgMod02a 
{ 
    BufferedImage rawImg; 
    BufferedImage buffImage; 
    int imgCols;//Number of horizontal pixels 
    int imgRows;//Number of rows of pixels 

    static String theProcessingClass = "C:/Users/Faiz/Documents/NetBeansProjects/image_processor/src/image_processor/ImgMod35a.java"; 

    static String theImgFile = "C:/Users/Faiz/Desktop/DCT/ibrahim2.jpg"; 

    int[][][] threeDPix; 
    int[][][] threeDPixMod; 
    int[] oneDPix; 

    //Reference to the image processing object. 
    ImgIntfc02 imageProcessingObject; 
    //-------------------------------------------// 

    public static void main(String[] args) throws IOException 
    { 
    //Display name of processing program and 
    // image file. 
    System.out.println("Processing program: " + theProcessingClass); 
    System.out.println("Image file: " + theImgFile); 


    //Instantiate an object of this class 
    ImgMod02a obj = new ImgMod02a(); 
    }//end main 
    //-------------------------------------------// 

    public ImgMod02a() throws IOException 
    { 

    rawImg = ImageIO.read(new File(theImgFile)); 
    imgCols = rawImg.getWidth(); 
    imgRows = rawImg.getHeight(); 

    threeDPixMod = imageProcessingObject.processImg(threeDPix,imgRows,imgCols); 

    oneDPix = convertToOneDim(threeDPixMod,imgCols,imgRows); 

    oneDPix = new int[imgCols * imgRows]; 

    //Create an empty BufferedImage object 
    buffImage = new BufferedImage(imgCols,imgRows,BufferedImage.TYPE_INT_ARGB); 

    // Draw Image into BufferedImage 
    Graphics g = buffImage.getGraphics(); 
    g.drawImage(rawImg, 0, 0, null); 

    //Convert the BufferedImage to numeric pixel 
    // representation. 
    DataBufferInt dataBufferInt = (DataBufferInt)buffImage.getRaster().getDataBuffer(); 
    oneDPix = dataBufferInt.getData(); 


    threeDPix = convertToThreeDim(oneDPix,imgCols,imgRows); 


    try 
    { 
     imageProcessingObject = (ImgIntfc02)Class.forName("image_processor.ImgMod35a").newInstance(); 

    }catch(Exception e) 
    { 
     System.out.println(e); 
    }//end catch 
    }//end constructor 
    //===========================================// 


    int[][][] convertToThreeDim(int[] oneDPix,int imgCols,int imgRows) 
    { 
    //Create the new 3D array to be populated 
    // with color data. 
    int[][][] data = new int[imgRows][imgCols][4]; 

    for(int row = 0;row < imgRows;row++){ 
     //Extract a row of pixel data into a 
     // temporary array of ints 
     int[] aRow = new int[imgCols]; 
     for(int col = 0; col < imgCols;col++) 
     { 
     int element = row * imgCols + col; 
     aRow[col] = oneDPix[element]; 
     }//end for loop on col 

     for(int col = 0;col < imgCols;col++) 
     { 
     //Alpha data 
     data[row][col][0] = (aRow[col] >> 24) & 0xFF; 
     //Red data 
     data[row][col][1] = (aRow[col] >> 16) & 0xFF; 
     //Green data 
     data[row][col][2] = (aRow[col] >> 8) & 0xFF; 
     //Blue data 
     data[row][col][3] = (aRow[col]) & 0xFF; 
     }//end for loop on col 
    }//end for loop on row 
    return data; 
    }//end convertToThreeDim 
    //-------------------------------------------// 

    final int[] convertToOneDim(int[][][] data,int imgCols,int imgRows) 
    { 
    int[] oneDPix = new int[imgCols * imgRows * 4]; 

    for(int row = 0,cnt = 0;row < imgRows;row++) 
    { 
     for(int col = 0;col < imgCols;col++){ 
     oneDPix[cnt] = ((data[row][col][0] << 24)& 0xFF000000)| ((data[row][col][1] << 16) & 0x00FF0000)| ((data[row][col][2] << 8) & 0x0000FF00)| ((data[row][col][3]) & 0x000000FF); 
     cnt++; 
     }//end for loop on col 
    }//end for loop on row 

    return oneDPix; 
    }//end convertToOneDim 
}//end ImgMod02a.java class 

的processImg是从一个接口的方法

interface ImgIntfc02 
    { 
     int[][][] processImg(int[][][] threeDPix, 
          int imgRows, 
          int imgCols); 
    } 

这是导致错误

线
threeDPixMod = imageProcessingObject.processImg(threeDPix,imgRows,imgCols); 

但是,当我尝试注释掉该行时,其他行似乎也有NullPointerException错误。

错误消息:在螺纹

异常 “主” 显示java.lang.NullPointerException
在 image_processor.ImgMod02a(ImgMod02a.java:48)
在 image_processor.ImgMod02a.main( ImgMod02a.java:37)

+2

48行是什么行?错误消息告诉你问题发生在哪里。 – unholysampler 2014-09-26 18:05:45

+0

我编辑过,突出显示导致错误的行。我尝试评论这条线,但其他线路仍在发生错误。这些声明有什么问题吗? – mfmz 2014-09-26 18:09:32

回答

2
public ImgMod02a() throws IOException 
    { 

    rawImg = ImageIO.read(new File(theImgFile)); 
    imgCols = rawImg.getWidth(); 
    imgRows = rawImg.getHeight(); 

    threeDPixMod = imageProcessingObject.processImg(threeDPix,imgRows,imgCols); 

    // ... Other Stuff ... 

    try 
    { 
     imageProcessingObject = (ImgIntfc02)Class.forName("image_processor.ImgMod35a").newInstance(); 

    }catch(Exception e) 
    { 
     System.out.println(e); 
    }//end catch 
    }//end constructor 

您正在尝试使用imageProcessingObject几行进入构造函数。但是,初始化imageProcessingObject是您在构造函数中做的最后一件事。在调用对象的方法之前,您总是需要初始化一个变量。

2

你还没有初始化threeDPix里面的构造函数。

而写作:

ImgMod02a obj = new ImgMod02a(); 

构造函数被调用:

public ImgMod02a() throws IOException 
    { 
    //your code 
} 

里面,你已经写了:

threeDPixMod = imageProcessingObject.processImg(threeDPix,imgRows,imgCols); 

你需要使用它之前初始化变量!