1

This code stopped working and i don't understand why. it returns this and crashes when it tries to perform StartActivity(Intent) [marked in code, inside OnClick]:

"java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)"

Here's the activity:

public class MainActivity extends ListActivity implements View.OnClickListener {

TextView responseField;

private ScoresDataSource datasource;

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

    responseField = (TextView)findViewById(R.id.textView);
    findViewById(R.id.buttonScore).setOnClickListener(this);
    findViewById(R.id.buttonAdd).setOnClickListener(this);

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


    List<Score> scores = datasource.getAllScores();

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

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void onClick(View view)
{
    ArrayAdapter<Score> adapter =
            (ArrayAdapter)getListAdapter();
    Score c = null;

    switch (view.getId()){
        case R.id.buttonScore:

            Intent scorePage = new Intent(this, ScoreboardActivity.class);
            startActivity(scorePage);
       //[CRASHES HERE]     <---------------


            break;
        case R.id.buttonAdd:

            EditText nameField = (EditText)findViewById(R.id.name);
            EditText scoreField = (EditText)findViewById(R.id.score);
            String name = nameField.getText().toString();
            String score = scoreField.getText().toString();

            try {
                int scoreInt = Integer.parseInt(score);
                responseField.setText("SCORE RECEIVED: " + System.getProperty("line.separator") + name + "  " + score);
                c = datasource.createScore(score, name);
                adapter.add(c);
            }
            catch(Exception e){
                responseField.setText("Not Valid Score");
            }

            adapter.notifyDataSetChanged();
 break; }

}
}  

This is xml sheet for the activity:

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">



    <Button
        android:layout_width="271dp"
        android:layout_height="wrap_content"
        android:text="AddScore"
        android:id="@+id/buttonAdd" />


<EditText
    android:layout_width="187dp"
    android:text="Player"
    android:layout_height="wrap_content"
    android:id="@+id/name" />

<EditText
    android:layout_width="184dp"
    android:text="Score"
    android:layout_height="wrap_content"
    android:id="@+id/score" />

<TextView
    android:layout_width="226dp"
    android:layout_height="70dp"
    android:text=""
    android:id="@+id/textView" />

<Button
    android:layout_width="222dp"
    android:layout_height="73dp"
    android:text="ScoreBoard"
    android:id="@+id/buttonScore" />

<ListView
    android:layout_width="39dp"
    android:layout_height="24dp"
    android:id="@android:id/list"
    android:layout_gravity="center_horizontal"
    android:visibility="invisible" />

Android Manifest:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".ScoreboardActivity"
        android:label="Scoreboard" >
    </activity>
</application>

Target Activity:

public class ScoreboardActivity extends ListActivity implements View.OnClickListener {



private ScoresDataSource datasource;

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

    findViewById(R.id.buttonAdd).setOnClickListener(this);
    findViewById(R.id.buttonDelete).setOnClickListener(this);

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


    List<Score> scores = datasource.getAllScores();

    ArrayAdapter<Score>
            adapter = new ArrayAdapter<Score>(
            this,
            android.R.layout.simple_list_item_1,
            scores);

//Sorting
    adapter.sort(new Comparator<Score>() {
        @Override
        public int compare(Score sc1, Score sc2) {
            return Integer.toString(sc1.getScore()).compareTo(Integer.toString(sc2.getScore()));         
        }
    });

    setListAdapter(adapter);
}

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void onClick(View view)
{
    ArrayAdapter<Score> adapter =
            (ArrayAdapter)getListAdapter();
    Score c = null;
    Random r = new Random();
    switch (view.getId()){
        case R.id.buttonBack:
        Intent main = new Intent(this, MainActivity.class);
        startActivity(main);
            break;

        case R.id.buttonDelete:
            if(adapter.getCount()>0){
                c = adapter.getItem(0);
                datasource.deleteScore(c);
                adapter.remove(c);
            }
            break;
    }
    adapter.notifyDataSetChanged();
}

}

santi-elus
  • 310
  • 2
  • 13

1 Answers1

1

Your error is telling you that you're trying to set a click listener on a null object.

If you say the error fires when you start the next activity then on your ScoreboardActivity there is an error on trying to set the click listener for one of this views:

findViewById(R.id.buttonAdd).setOnClickListener(this);
findViewById(R.id.buttonDelete).setOnClickListener(this);

Check your activity_scores.xml to see if you really have those ids.

Pedro Oliveira
  • 20,442
  • 8
  • 55
  • 82