-2

Possible Duplicate:
LogCat entry meaning 2

LogCat say that the problem is with the Array Adapter that it has a NullPointerException. How can I solve this problem? The application crashes if I press the button add.

package com.example.to_doliste;

import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;

public class MainActivity extends ListActivity {
    private CommentsDataSource datasource;

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

        datasource = new CommentsDataSource(this);
        datasource.open();

        List<Comment> values = datasource.getAllComments();

Array Adapter get build

        ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
            android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);
      }

it is used here

      public void onClick(View view) {
        @SuppressWarnings("unchecked")
        ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();
        Comment comment = null;
        switch (view.getId()) {
        case R.id.add:

          String comments;
            EditText Feld1 = (EditText)findViewById(R.id.editText1);

            if (Feld1.getText().toString().length() == 0)
                    {
                return;
                    }

            comments = (Feld1.getText().toString());

            Feld1.setText(String.valueOf(comments));


          adapter.add(comment);
          break;
        case R.id.delete:
          if (getListAdapter().getCount() > 0) {
            comment = (Comment) getListAdapter().getItem(0);
            datasource.deleteComment(comment);
            adapter.remove(comment);
          }
          break;
        }
        adapter.notifyDataSetChanged();
      }

here the adapter stops

      @Override
      protected void onResume() {
        datasource.open();
        super.onResume();
      }

      @Override
      protected void onPause() {
        datasource.close();
        super.onPause();
      }

    } 
Community
  • 1
  • 1
android
  • 27
  • 5
  • 3
    Please add the LogCat (stack trace) output – Mattias Isegran Bergander Dec 26 '12 at 17:56
  • 12-26 18:33:09.651: E/AndroidRuntime(26038): FATAL EXCEPTION: main 12-26 18:33:09.651: E/AndroidRuntime(26038): java.lang.NullPointerException 12-26 18:33:09.651: E/AndroidRuntime(26038): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394) – android Dec 26 '12 at 17:58
  • 1
    You should slow down and try to solve these problems yourself before posting them. This is your third question in a half hour. Also you should accept the best answer to each previous question. – Sam Dec 26 '12 at 17:58
  • 1
    Wait this is the same question: [LogCat entry meaning 2](http://stackoverflow.com/q/14044001/1267661). Shall I post the same answer? – Sam Dec 26 '12 at 17:59
  • 1
    This information should be an edit to your older question, not a new question. – Raghav Sood Dec 26 '12 at 18:00
  • @Sam : yes i agree with you. difference is that user1929838 include code in this post – ρяσѕρєя K Dec 26 '12 at 18:05

1 Answers1

2

Here

Comment comment = null;

You are trying to add comment to adapter but comment is null. You are not updating any new value in comment after initializing it to null, so update it before adding or removing it from adapter.

Yauraw Gadav
  • 1,706
  • 1
  • 18
  • 39
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213