the following code throws a NullPointerException and I'm not sure why. If someone could point me to the error it would be much apprecciated.
The code from the MainActivity, the error is in line 4:
private void init(){
this.createClassifier();
this.takePhoto();
}
private void createClassifier(){
try {
classifier = ImageClassifierFactory.create(
getAssets(),
Constants.GRAPH_FILE_PATH,
Constants.LABELS_FILE_PATH,
Constants.IMAGE_SIZE,
Constants.GRAPH_INPUT_NAME,
Constants.GRAPH_OUTPUT_NAME);
}catch (IOException e) {
Log.e("MainActivity", e.getMessage());
}
}
private Classifier classifier;
...
private final void classifyAndShowResult(final Bitmap croppedBitmap){
runInBackground((new Runnable(){
public final void run(){
Result result = classifier.recognizeImage(croppedBitmap);
showResult(result);
}
}));
}
There are different methods calling init(), so I can't figure out why classifier doesn't get initialised.
The result class:
public class Result {
private String result;
private float confidence;
public Result(String result, float confidence) {
this.result = result;
this.confidence = confidence;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public float getConfidence() {
return confidence;
}
public void setConfidence(float confidence) {
this.confidence = confidence;
}
}
the classifier Interface:
public interface Classifier {
Result recognizeImage(Bitmap bitmap);
and the initiated recognizeImage:
public Result recognizeImage(Bitmap bitmap){
preprocessImageToNormalizedFloats(bitmap);
classifyImageToOutputs();
PriorityQueue outputQueue = new PriorityQueue(getResults());
Object queue = outputQueue.poll();
return (Result)queue;
}
The error code was: java.lang.NullPointerException: Attempt to invoke interface method 'classifier.Result classifier.Classifier.recognizeImage(android.graphics.Bitmap)' on a null object reference
ImageClassifier Constructor:
public ImageClassifier(String inputName, String outputName, long imageSize, List labels, int[] imageBitmapPixels, float[] imageNormalizedPixels, float[] results, TensorFlowInferenceInterface tensorFlowInference) {
this.inputName = inputName;
this.outputName = outputName;
this.imageSize = imageSize;
this.labels = labels;
this.imageBitmapPixels = imageBitmapPixels;
this.imageNormalizedPixels = imageNormalizedPixels;
this.results = results;
this.tensorFlowInference = tensorFlowInference;
}
ImageClassifierFactory Class:
public class ImageClassifierFactory {
public final static Classifier create(AssetManager assetManager, String graphFilePath, String labelsFilePath, int imageSize, String inputName, String outputName) throws IOException {
List labels = getLabels(assetManager, labelsFilePath);
ImageClassifier im = new ImageClassifier(inputName, outputName, (long)imageSize, labels, new int[imageSize * imageSize], new float[imageSize * imageSize * COLOR_CHANNELS], new float[labels.size()], new TensorFlowInferenceInterface(assetManager, graphFilePath));
return im;
}
}