1

我在我的 Android 代码中使用 IBM Watson 的 Tone Analyzer,但我不断收到java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference

以下是我的代码

public class MainActivity extends AppCompatActivity {

    final ToneAnalyzer toneAnalyzer =
            new ToneAnalyzer("2018-01-19");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        JSONObject credentials = null; // Convert the file into a JSON object
        try {
            credentials = new JSONObject(IOUtils.toString(
                    getResources().openRawResource(R.raw.credentials), "UTF-8"

            ));

            String username = credentials.getString("username");
            String password = credentials.getString("password");
            toneAnalyzer.setUsernameAndPassword(username, password);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Button analyzeButton = (Button)findViewById(R.id.analyze_button);

        analyzeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText userInput = (EditText)findViewById(R.id.user_input);

                final String textToAnalyze = userInput.getText().toString();
                ToneOptions options = new ToneOptions.Builder()
                        .addTone(Tone.EMOTION)
                        .html(false).build();

                toneAnalyzer.getTone(textToAnalyze, options).enqueue(
                        new ServiceCallback<ToneAnalysis>() {
                            @Override
                            public void onResponse(ToneAnalysis response) {
                                Log.i("Hii", "onResponse: "+response.getDocumentTone());


                                List<ToneScore> scores = response.getDocumentTone()
                                        .getTones()
                                        .get(0)
                                        .getTones();


                                String detectedTones = "";
                                for(ToneScore score:scores) {
                                    if(score.getScore() > 0.5f) {
                                        detectedTones += score.getName() + " ";
                                    }
                                }

                                final String toastMessage =
                                        "The following emotions were detected:\n\n"
                                                + detectedTones.toUpperCase();

                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(getBaseContext(),
                                                toastMessage, Toast.LENGTH_LONG).show();
                                    }
                                });
                            }

                            @Override
                            public void onFailure(Exception e) {
                                e.printStackTrace();
                            }
                        });




            }
        });


    }
}

有人可以指出我做错了什么。我将 credentials.json 文件保存在 raw 文件夹中。我尝试在我的 Android 应用程序中写下每一种情感,但我一直没有得到任何回应。任何帮助将不胜感激。

4

0 回答 0