1

I am trying to make a simple game. When I am trying to set the text of 3 TextViews I get a NullPointerException.

public class MainActivity extends Activity implements View.OnClickListener {

private void initRound() {
    countdown = 10;

    ViewGroup container = (ViewGroup) findViewById(R.id.container);
    container.removeAllViews();
    WimmelView wv = new WimmelView(this);
    container.addView(wv, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    wv.setImageCount(8*(10 + round));

    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);  //HERE DO I GET THE EXCEPTION
}

}

Here is the error from logcat:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.developer.nico.kissthefrognow, PID: 1940
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.developer.nico.kissthefrognow.MainActivity.fillTextView(MainActivity.java:80)
        at com.developer.nico.kissthefrognow.MainActivity.initRound(MainActivity.java:34)
        at com.developer.nico.kissthefrognow.MainActivity.newGame(MainActivity.java:21)
        at com.developer.nico.kissthefrognow.MainActivity.startGame(MainActivity.java:67)
        at com.developer.nico.kissthefrognow.MainActivity.onClick(MainActivity.java:58)
        at android.view.View.performClick(View.java:4756)
        at android.view.View$PerformClick.run(View.java:19749)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

And my xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/rl">

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/container">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="000000"
        android:id="@+id/points"
        android:layout_gravity="left|top"
        android:textColor="@color/Red" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:id="@+id/round"
        android:layout_gravity="right|top"
        android:textColor="@color/Red" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="00000"
        android:id="@+id/countdown"
        android:layout_gravity="center_horizontal|bottom"
        android:textColor="@color/Red" />
</FrameLayout>

Any ideas how to solve it? Thanks!

nic2307
  • 71
  • 2
  • 11
  • 2
    post your logcat please – Milad Faridnia Mar 07 '15 at 11:11
  • chech your points and round variables also check those ids maybe you've passed the wrong id – Milad Faridnia Mar 07 '15 at 11:13
  • 1
    Probably no `TextView` is found with the id. To avoid the program to break add a null check before `tv.setText(text)` – Razib Mar 07 '15 at 11:16
  • 1
    also please send the xml layout – Milad Faridnia Mar 07 '15 at 11:17
  • add fillTextView(R.id.points, Integer.toString(points)+" "); fillTextView(R.id.round, Integer.toString(round)+" "); fillTextView(R.id.countdown, Integer.toString(countdown*1000)+" "); most likely method is not getting any values passed, this will at least try to pass a blank, and remove text tag from all TextView – Pankaj Nimgade Mar 07 '15 at 11:19
  • hm thanks Pankaj but it doesn't work. I also don't see any problems with the id's. – nic2307 Mar 07 '15 at 11:28
  • @nic2307, you are right, certainly there isn't anything wrong with id's if you try to print them you will get the values, i am afraid that variable point and round is not passing properly, try to print them to see what are you passing – Pankaj Nimgade Mar 07 '15 at 11:30
  • 1
    You are removing the views in container with container.removeAllViews(); – Rick Sanchez Mar 07 '15 at 11:33
  • i get the exact same error when i delete container.removeAllViews(); – nic2307 Mar 07 '15 at 11:36
  • and when i print out point and round i get the value it should be- 0 and 1 – nic2307 Mar 07 '15 at 11:37
  • I think tv is null hence you are getting the exception. Give a Textview for that in xml and then initialise using R.id.textViewid and then try once. – keshav kowshik Mar 07 '15 at 11:42
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Simon Mar 07 '15 at 11:50

2 Answers2

1

How about this. In onCreate get references for your TextViews:

public class MainActivity extends Activity implements View.OnClickListener {

    TextView tvPoints;
    TextView tvRound;
    TextView tvCountdown;

    protected void onCreate(Bundle savedInstanceState) {
        ....
        ....
        tvPoints = (TextView) findViewById(R.id.points);
        tvRound = (TextView) findViewById(R.id.round);
        tvCountdown = (TextView) findViewById(R.id.countdown);
        ....
        ....
    }

then in your initRound() do this:

fillTextView(tvPoints, Integer.toString(points));
fillTextView(tvRound, Integer.toString(round));
fillTextView(tvCountdown, Integer.toString(countdown*1000));

and in your fillTextView:

private void fillTextView(TextView tv, String text)  {
    tv.setText(text);
}

If this doesn't work, then the error is in some of these lines of code (try removing them and see if the code works):

    container.removeAllViews();
    WimmelView wv = new WimmelView(this);
    container.addView(wv, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    wv.setImageCount(8*(10 + round));

EDIT: As for "TextViews are empty" problem, you need to test stuff, for example:

Instead of tv.setText(text); try using something like tv.setText("10"); (just some random text). If setting the number "10" is working, then the issue is with the Integer.toString(...) part (for some unknown reason). If setting the number "10" is not working, then you should try setting the text in the initRound() method without using the fillTextView method (just to test if setting the text works at all), something like this:

private void initRound() {
    countdown = 10;

    tvPoints.setText("10");
    tvRound.setText("7");
    tvCountdown.setText("19");    

    ViewGroup container = (ViewGroup) findViewById(R.id.container);
    container.removeAllViews();
    WimmelView wv = new WimmelView(this);
    container.addView(wv, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    wv.setImageCount(8*(10 + round));
}

If this works, then there is some issue with the WimmelView part of the code. And if setting the text still doesn't work, then the issue is probably where you call the initRound method, some code there is doing something that is affecting the TextViews...

miselking
  • 3,043
  • 4
  • 28
  • 39
  • okay the positive thing is that i don't get an exception any more. thanks for this. but now my textviews are empty ^^ – nic2307 Mar 07 '15 at 14:38
  • I am glad the `NullException` issue is resolved, now I edited the answer for the second problem, try this and hopefully the issue will be completely resolved. – miselking Mar 07 '15 at 15:45
  • Thanks i will try it tomorrow! Sounds logically. – nic2307 Mar 07 '15 at 18:42
  • i don't see any text wherever i set the text. i think the man above in the comments is right. i am removing the textviews with removeAllViews(); so i can't see them right? so how can i prevent this? xD – nic2307 Mar 08 '15 at 08:48
  • okay okay it works to simply add the textviews to the container. but one last question.. in my wimmelview i add a lot of images to the screen and the player has to find the correct image. the textviews are now partially covered by the images. how to add the textviews to the foreground? when i add the textviews after i add the wimmelview i dont see the wimmelview. could you please help me one last time? :D – nic2307 Mar 08 '15 at 08:56
  • Hmm, FrameLayout works in a way that when you add items to it, it adds items on top of each other in the top left corner.Now, I do not know the exact look of your app, but the idea is to properly position elements in the FrameLayout so all of them will be visible (using `layout_gravity`). Also, there is possibility that your TextView's background color is not transparent so it covers the `WimmelView`. Check out [this](http://stackoverflow.com/questions/7706913/overlay-text-over-imageview-in-framelayout-progrmatically-android), it might help you in some way. Sorry I couldn't be of more help. :( – miselking Mar 08 '15 at 12:50
  • Thanks anyways. You were a big help! – nic2307 Mar 08 '15 at 14:30
0

Initialise your textviews in onCreate() first

public class MainActivity extends Activity implements View.OnClickListener {

private TextView tv1, tv2, tv3;

private void initRound() { countdown = 10;

ViewGroup container = (ViewGroup) findViewById(R.id.container);
container.removeAllViews();
WimmelView wv = new WimmelView(this);
container.addView(wv, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
wv.setImageCount(8*(10 + round));

this is required - this way Android finds view before using them

tv1 = (TextView) findViewById(R.id.points);
tv2 = (TextView) findViewById(R.id.round);
tv3 = (TextView) findViewById(R.id.countdown);


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);  //HERE DO I GET THE EXCEPTION
}

}

this will solve your problem

Kushal
  • 8,100
  • 9
  • 63
  • 82