尝试调用虚拟方法'java.lang.String java.util.HashSet.toString()。获取空指针异常

时间:2018-12-07 18:28:21

标签: java android android-studio

android studio在键入时没有显示任何错误,此错误仅在运行时出现。原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.String java.util.HashSet.toString()'

问题应该在这三行之一

HashSet set =(HashSet)sharedPreferences.getStringSet(“ notes”,null);

Log.i(“ test”,set.toString());

if(set == null){

public class MainActivity extends AppCompatActivity {

static ArrayList<String> notes = new ArrayList<>();
static ArrayAdapter arrayAdapter;

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.add_note_menu, menu);

    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);

    if (item.getItemId() == R.id.add_note) {

        Intent intent = new Intent(getApplicationContext(), NoteEditorActivity.class);

        startActivity(intent);

        return true;

    }

    return false;

}

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

    ListView listView = (ListView) findViewById(R.id.listView);

    SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.robpercival.notes", Context.MODE_PRIVATE);

    HashSet<String> set = (HashSet<String>) sharedPreferences.getStringSet("notes", null);

    Log.i("test", set.toString());

    if (set == null) {

        notes.add("Example note");

    } else {

        notes = new ArrayList(set);

    }

    arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, notes);

    listView.setAdapter(arrayAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            Intent intent = new Intent(getApplicationContext(), NoteEditorActivity.class);
            intent.putExtra("noteId", i);
            startActivity(intent);

        }

    });

    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {

            final int itemToDelete = i;

            new AlertDialog.Builder(MainActivity.this)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setTitle("Are you sure?")
                    .setMessage("Do you want to delete this note?")
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {

                                    notes.remove(itemToDelete);
                                    arrayAdapter.notifyDataSetChanged();

                                    SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.robpercival.notes", Context.MODE_PRIVATE);

                                    HashSet<String> set = new HashSet(MainActivity.notes);

                                    sharedPreferences.edit().putStringSet("notes", set).apply();

                                }
                            }
                    )
                    .setNegativeButton("No", null)
                    .show();

            return true;
        }

    });

}
}

// 2nd class

 public class NoteEditorActivity extends AppCompatActivity {

int noteId;

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

    EditText editText = (EditText) findViewById(R.id.editText);

    Intent intent = getIntent();
    noteId = intent.getIntExtra("noteId", -1);

    if (noteId != -1) {

        editText.setText(MainActivity.notes.get(noteId));

    } else {

        MainActivity.notes.add("");
        noteId = MainActivity.notes.size() - 1;
        MainActivity.arrayAdapter.notifyDataSetChanged();

    }

    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            MainActivity.notes.set(noteId, String.valueOf(charSequence));
            MainActivity.arrayAdapter.notifyDataSetChanged();

            SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.robpercival.notes", Context.MODE_PRIVATE);

            HashSet<String> set = new HashSet(MainActivity.notes);

            sharedPreferences.edit().putStringSet("notes", set).apply();



        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

}
 }

2 个答案:

答案 0 :(得分:0)

您必须测试是否使用它为空

SELECT
  PP.Month,
  PP.item,
  PP.store,
  PP.manager,
  PP.TY AS [TY-Price],
  PP.LY AS [LY-Price],
  PP.PY AS [PY-Price],
  PW.TY AS [TY-Weight],
  PW.LY AS [LY-Weight],
  PW.PY AS [PY-Weight]
FROM
  Pivot_Price AS PP INNER JOIN Pivot_Weight AS PW
ON
  PP.month = PW.month AND
  PP.item = PW.item AND
  PP.store = PW.store AND
  PP.manager = PW.manager

答案 1 :(得分:0)

// problem is here. "set" object is null.
 Log.i("test", set.toString());

if (set == null) {

    notes.add("Example note");

} else {

    notes = new ArrayList(set);

}

更改为

if (set == null) {

    notes.add("Example note");

} else {

    notes = new ArrayList(set);
    // suggestion use Log.d("test", set.toString()); since d -> debug, i-> 
   // information
   Log.i("test", set.toString());
}