2014-09-26 3 views
3

Netbeans에서 NullPointerException 오류가 발생하는 다음 코드가 있습니다. 이 코드는 실제로 자바 프레임을 사용하는 GUI에서 사용하도록 작성되었습니다. 그러나 나는 cmd로만 사용하도록 편집했습니다. 나는 코드를 조사했지만 오류가 왜 나타나는지 알 수 없었다. 누구든지 여기서 문제를 강조 할 수 있습니까? 에러 메시지는 오류의 라인 threeDPixMod 할당에있다 표시 및 processImg이 오류Netbeans의 NullPointerException

을 일으키는 라인

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

인터페이스에서 방법

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 

을 oneDPix

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

+0

오류를 일으킨 행을 강조 표시하여 편집했습니다. 줄을 주석 처리하려고 시도했지만 다른 줄에서는 계속 오류가 발생합니다. 선언에 문제가 있습니까? – mfmz

답변

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); 

당신은 그것을 사용하기 전에 변수를 초기화해야!