31

This is what I'm trying to do for several hours: I've got a MainActivity.java file (listing below) and a fragment_start.xml file with a start button. Tapping the start-button should display the activity_main.xml file with points-/round- and countdown-Textviews. It doesn't work and this is what is happening:

The logcat tells me: PID: 1240 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

The emulator displays: Unfortunately, GAME has stopped.

Necessary to mention that I'm rather new in programming?

Thanks for any advice!

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class MainActivity extends Activity implements View.OnClickListener {

private int points;
private int round;
private int countdown;

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

private void newGame () {
    points=0;
    round=1;
    initRound();
}

private void initRound() {
    countdown = 10;
    update();
}

private void update () {
    fillTextView(R.id.points, Integer.toString(points));
    fillTextView(R.id.round, Integer.toString(round));
    fillTextView(R.id.countdown, Integer.toString(countdown * 1000));
}

private void fillTextView (int id, String text) {
    TextView tv = (TextView) findViewById(id);
    tv.setText(text);
}

private void showStartFragment() {
    ViewGroup container = (ViewGroup) findViewById(R.id.container);
    container.removeAllViews();
    container.addView(
            getLayoutInflater().inflate(R.layout.fragment_start, null) );
    container.findViewById(R.id.start).setOnClickListener(this);
}

@Override
public void onClick(View view) {
    if(view.getId() == R.id.start) {
        startGame();
    }
}

public void startGame() {
    newGame();
}
}
George
  • 367
  • 1
  • 3
  • 4
  • 5
    Posting whole exception message/logcat would help. – arleitiss Jan 12 '15 at 21:31
  • Please post your xml files. – Rohit5k2 Jan 12 '15 at 21:33
  • 1
    Look at my answer. The explanation cant be clearer, but you have to track down yourself why you give a wrong id as a paremeter (because if a view with this ID existed, the method would not return null) – ProgrammingIsAwsome Jan 12 '15 at 21:45
  • 1
    When you look for your TextView `TextView tv = (TextView) findViewById(id);` using findViewById you have to be sure about the layout you're looking into. If you are looking inside an inflated layout like this `View theView = inflater.inflate(R.layout.inflatedLayout, null);` you have to specify the view you're looking into like so `TextView tv = (TextView) theView.findViewById(id);` – fcicer Jul 19 '16 at 23:07
  • Refer the following link. https://vshivam.wordpress.com/2015/01/14/inflating-different-xml-layouts-in-an-android-listview-for-different-objects/ – Abdul Gaffar Jan 10 '17 at 11:01
  • If anyone is working with multiple packages, in version control, better sync the project with gradle files and run the app again. – Reejesh May 02 '23 at 07:37

3 Answers3

31

The problem is the tv.setText(text). The variable tv is probably null and you call the setText method on that null, which you can't. My guess that the problem is on the findViewById method, but it's not here, so I can't tell more, without the code.

Ru Chern Chong
  • 3,692
  • 13
  • 33
  • 43
JustOneJavaDev
  • 359
  • 3
  • 5
21

Here lies your problem:

private void fillTextView (int id, String text) {
    TextView tv = (TextView) findViewById(id);
    tv.setText(text); // tv is null
}

--> (TextView) findViewById(id); // returns null But from your code, I can't find why this method returns null. Try to track down, what id you give as a parameter and if this view with the specified id exists.

The error message is very clear and even tells you at what method. From the documentation:

public final View findViewById (int id)
    Look for a child view with the given id. If this view has the given id, return this view.
    Parameters
        id  The id to search for.
    Returns
        The view that has the given id in the hierarchy or null

http://developer.android.com/reference/android/view/View.html#findViewById%28int%29

In other words: You have no view with the id you give as a parameter.

ProgrammingIsAwsome
  • 1,109
  • 7
  • 15
4
private void fillTextView (int id, String text) {
    TextView tv = (TextView) findViewById(id);
    tv.setText(text);
}

If this is where you're getting the null pointer exception, there was no view found for the id that you passed into findViewById(), and the actual exception is thrown when you try to call a function setText() on null. You should post your XML for R.layout.activity_main, as it's hard to tell where things went wrong just by looking at your code.

More reading on null pointers: What is a NullPointerException, and how do I fix it?

Community
  • 1
  • 1
mgomov
  • 66
  • 2