Getting Null Pointer Exception in my addMarking method.
I'm new to buffered images so I'm not completely sure what the problem is. I have a seperate class that breaks an image up into 3x3 arrays and calls findEnds() with it. When findEnds find the correct condition in the 3x3 array it calls addMarking with the coordinates to make a red dot on the picture. Note: Previously I tried using a 2dGraphic to write the picture on the bufferedImage but took it out in an attempt to fix this problem and copy the pixels manually with setRGB (haven't written this method yet). Note: The reason I can't just make the buffered image with the original file is that the file has a black and white color type not TYPE_INT_RGB.
Error:
Exception in thread "main" java.lang.NullPointerException
at findFeatures.addMarking(findFeatures.java:43)
at getImage.getColor(getImage.java:44)
at fingerprintDriver.main(fingerprintDriver.java:15)
The getImage is a class that breaks the photo into 3x3 arrays, it also returns the coordinates of requested arrays to the addMarking class.
fingerprintDriver:
/**
* Driver for the ElevationScanner Class
*
* @author benjamincole
* @version 4/23/18
*/
public class fingerprintDriver {
public static void main(String[] args) {
String fileName = JOptionPane.showInputDialog("Please enter a name for the file");
getImage test = new getImage(fileName);
findFeatures find = new findFeatures();
find.imageConstructor();
test.getColor();
}
}
findFeatures class:
/**
* This class tests a 3x3 array. The method findEnds tests this array for
* two black pixels. If this condition is found it is an end and the x
* and y-coords are marked.
*
* @author benjamincole
*
*/
public class findFeatures {
BufferedImage markedImage;
int counter;
public void imageConstructor() { // creates image to add markings to
markedImage = new BufferedImage(getImage.width, getImage.height, BufferedImage.TYPE_INT_RGB);
}
public void findEnds(boolean[][] colorArray) { // finds the ends of arches
counter = 0;
System.out.println();
for (int x = 0; x < 3; x++) {
System.out.println();
for (int y = 0; y < 3; y++) {
System.out.print(" " + colorArray[x][y]);
if (colorArray[x][y])
counter++;
}
}
if (counter == 2 || counter == 1) { // if only 2 or less pixels are
// black in the 3x3
System.out.println(getImage.xCord + " " + getImage.yCord);
System.out.println(" end found"); // end has been found
addMarking(getImage.getXCord(), getImage.getYCord(), Color.red.getRGB());
}
}
public void addMarking(int x, int y, int rgb) { // adds red `marking to image`
markedImage.setRGB(x, y, rgb); //ERROR HERE! (Null Pointer)
markedImage.setRGB(x + 1, y + 1, rgb);
markedImage.setRGB(x + 2, y + 2, rgb);
markedImage.setRGB(x, y + 2, rgb);
markedImage.setRGB(x + 2, y, rgb);
}