NullPointerException in Netbeans

I have the following code that is facing a NullPointerException error in Netbeans. The code is actually written for use with a GUI using java frames. But I edited to only use it from cmd. I ran through the codes but couldn't find out why the error appears. Can anyone point out what the problem is? The error message indicates that the error string is related to the assignment of threeDPixMod and oneDPix

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 is a method from the interface

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

      

This is the line causing the error

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

      

But when I try to comment out a line, other lines also have a NullPointerException error.

Error message:

Exception on thread "main" java.lang.NullPointerException
in image_processor.ImgMod02a. (ImgMod02a.java:48)
at image_processor.ImgMod02a.main (ImgMod02a.java:37)

+3


source to share


2 answers


  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

      



You are trying to use imageProcessingObject

multiple lines in the constructor. However, initialization imageProcessingObject

is the last thing you do in the constructor. You always need to initialize a variable before you can call a method on an object.

+2


source


You haven't initialized threeDPix

inside the contructor.

when writing:

   ImgMod02a obj = new ImgMod02a();

      

the constructor is called:



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

      

inside which you wrote:

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

      

You need to initialize the variable before using it!

+2


source







All Articles