java.lang.NullPointerException:尝试在空对象引用上调用接口方法

时间:2019-01-26 16:27:22

标签: java android interface

下面的代码抛出NullPointerException,我不确定为什么。如果有人可以指出我的错误,将不胜感激。

MainActivity中的代码,错误在第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);
        }
    }));

}

有不同的方法调用init(),所以我不知道为什么分类器没有被初始化。

结果类:

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

分类器界面:

public interface Classifier {
Result recognizeImage(Bitmap bitmap);

和启动的识别图像:

public Result recognizeImage(Bitmap bitmap){
    preprocessImageToNormalizedFloats(bitmap);
    classifyImageToOutputs();
    PriorityQueue outputQueue = new PriorityQueue(getResults());
    Object queue = outputQueue.poll();
    return (Result)queue;
}

错误代码为:java.lang.NullPointerException:尝试在空对象引用上调用接口方法'classifier.Result classifier.Classifier.recognizeImage(android.graphics.Bitmap)'

ImageClassifier构造函数:

 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类:

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;

}

}

2 个答案:

答案 0 :(得分:0)

您无法创建分类器接口Classifier的实例。 您需要将Classifier更改为A类,并创建该类的classifier实例,或者创建另一个实现Classifier接口的Another class的实例

答案 1 :(得分:0)

声明classifyAndShowResult-方法的类的属性中必须包含classifier。调用该方法时,该变量为null。要对此进行调试,您应该以“调试”模式启动应用程序(假设您以eclipse编程),然后读取NullPointerException的堆栈以找出未初始化classifier的原因。

@编辑: 看到您的代码被分类后,您需要执行以下操作:

private Classifier classifier = new ImageClassifier();

...假设classifier在初始化时不需要参数。