有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

下面的代码抛出一个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:尝试调用接口方法的分类器。结果分类器。分类器。在空对象引用上识别图像(安卓.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) 个答案

  1. # 1 楼答案

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

  2. # 2 楼答案

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

    @Edit: 在看到为分类器截取的代码后,您需要执行以下操作:

    private Classifier classifier = new ImageClassifier();
    

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