0

When the clarifai API is called on an image it returns List of ClarifaiOutput Concepts. But on accessing the concepts only values can be accessed. Accessing name of the concepts results in NullPointerException.

concept.name() results in exception whereas concept.value() works fine

The Exception can be handled using a try catch block but I want to know the predicted concept names.

Please let me know what can be done to resolve this.

Below is the code for the same:

public void run() {
                List<String> ResultList = null;
                String ResultString = "";
                final List<ClarifaiOutput<Concept>> predictionResults = client.getDefaultModels().generalModel().predict().
                        withInputs(ClarifaiInput.forImage(new File(pictureFile.getAbsolutePath())))
                        .executeSync()
                        .get();
                if(predictionResults!=null && predictionResults.size()>0){
                    ListIterator<ClarifaiOutput<Concept>> itr= predictionResults.listIterator();
                    while(itr.hasNext()){
                        ClarifaiOutput<Concept> output = itr.next();
                        List<Concept> concepts = output.data();
                        if(concepts != null && concepts.size() > 0){
                            for (int j = 0; j < concepts.size(); j++) {
                                Concept concept = concepts.get(j);
                                float cval=0;
                                cval = concept.value();
                                if(cval > 0){
                                    ResultList.add(concept.name());
                                    ResultString += concept.name();
                                }
                            }
                        }
                    }
                }
            }

1 Answers1

0

The NullPointerException is not due to concept.name() it is rather due to ResultList.add() method.

The ResultList needs to be initialized to avoid the exception

List<String> ResultList = new ArrayList<String>();

This will solve the issue