0

I am trying to open a New Activity within my application using an intent once a game on the previous activity is finished, like shown below. I am getting a NullPointerException, What am I doing wrong?

Code to open new activity:

//when no cards are left if(totalcards==0){ //get the avg med and att values getAverageMeditationValue(); getAverageAttentionValue(); if(getAverageMeditationValue() && getAverageAttentionValue()){ //add session detail to the SQLite DB writeToDatabase(); //intent to open activty showing results of session Intent t = new Intent(MemoryGame.this, com.example.brianapp.MemoryGameResults.class); t.putExtra("singleScore", single); // Start intent startActivity(t); //stop recording EEG data device.close(); } } 

New activity:

 public class MemoryGameResults extends Activity { //declare vars TextView tv1; TextView tv2; TextView tv3; TextView tv4; TextView tv5; TextView tv6; Session singleScore; LoginDataBaseAdapter loginDataBaseAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.memorygameresults); initialiseVars(); setUpAllTextViews(); } public void initialiseVars() { // the single score from the the meditation class singleScore = (Session) getIntent().getSerializableExtra( "singleScore"); // Declaring textViews tv1 = (TextView) findViewById(R.id.medresults1); tv2 = (TextView) findViewById(R.id.medresults2); tv3 = (TextView) findViewById(R.id.medresults3); tv4 = (TextView) findViewById(R.id.medresults4); tv5 = (TextView) findViewById(R.id.medresults5); tv6 = (TextView) findViewById(R.id.medresults6); } /** * Sets text for all textviews * in activity */ public void setUpAllTextViews(){ // setting text for each text view tv1.setText("Results of session:"); tv2.setText("Average Meditation Value: " + singleScore.getMeditation()); tv3.setText("Maximum Meditation Value: " + singleScore.getMax()); tv4.setText("Average Attention Value: " + singleScore.getAvgAttention()); tv5.setText("Maximum Attention Value: " + singleScore.getMaxAttention()); tv6.setText("Correct Answers: " + singleScore.getScore()); } } /** * Method that ensures user is returned to main menu when they press back * button */ @Override public void onBackPressed() { Intent t = new Intent(MemoryGameResults.this, com.example.brianapp.MainMenu.class); startActivity(t); } 

}

When I try to do so I am getting the following error:

08-24 10:56:44.326: E/AndroidRuntime(17074): FATAL EXCEPTION: main 08-24 10:56:44.326: E/AndroidRuntime(17074): Process: com.example.brianapp, PID: 17074 08-24 10:56:44.326: E/AndroidRuntime(17074): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.brianapp/com.example.brianapp.MemoryGameResults}: java.lang.NullPointerException 08-24 10:56:44.326: E/AndroidRuntime(17074): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305) 08-24 10:56:44.326: E/AndroidRuntime(17074): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363) 08-24 10:56:44.326: E/AndroidRuntime(17074): at android.app.ActivityThread.access$900(ActivityThread.java:161) 08-24 10:56:44.326: E/AndroidRuntime(17074): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265) 08-24 10:56:44.326: E/AndroidRuntime(17074): at android.os.Handler.dispatchMessage(Handler.java:102) 08-24 10:56:44.326: E/AndroidRuntime(17074): at android.os.Looper.loop(Looper.java:157) 08-24 10:56:44.326: E/AndroidRuntime(17074): at android.app.ActivityThread.main(ActivityThread.java:5356) 08-24 10:56:44.326: E/AndroidRuntime(17074): at java.lang.reflect.Method.invokeNative(Native Method) 08-24 10:56:44.326: E/AndroidRuntime(17074): at java.lang.reflect.Method.invoke(Method.java:515) 08-24 10:56:44.326: E/AndroidRuntime(17074): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) 08-24 10:56:44.326: E/AndroidRuntime(17074): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 08-24 10:56:44.326: E/AndroidRuntime(17074): at dalvik.system.NativeStart.main(Native Method) 08-24 10:56:44.326: E/AndroidRuntime(17074): Caused by: java.lang.NullPointerException 08-24 10:56:44.326: E/AndroidRuntime(17074): at com.example.brianapp.MemoryGameResults.setUpAllTextViews(MemoryGameResults.java:323) 08-24 10:56:44.326: E/AndroidRuntime(17074): at com.example.brianapp.MemoryGameResults.onCreate(MemoryGameResults.java:51) 08-24 10:56:44.326: E/AndroidRuntime(17074): at android.app.Activity.performCreate(Activity.java:5426) 08-24 10:56:44.326: E/AndroidRuntime(17074): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 08-24 10:56:44.326: E/AndroidRuntime(17074): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269) 08-24 10:56:44.326: E/AndroidRuntime(17074): ... 11 more 

Manifest sections relating to the two activities:

 <activity android:name="com.example.brianapp.MemoryGame" android:label="Memory Game" > <intent-filter> <action android:name="com.example.brianapp.MemoryGame" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name="com.example.brianapp.MemoryGameResults" android:label="Memory Game Results" > <intent-filter> <action android:name="com.example.brianapp.MemoryGameResults" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> 

mathsgameresults.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/medresults1" android:layout_width="290dp" android:layout_height="50dp" android:layout_gravity="center" android:textSize="20sp" > </TextView> <TextView android:id="@+id/medresults2" android:layout_width="290dp" android:layout_height="50dp" android:layout_gravity="center" android:background="@drawable/buttonshape" android:layout_marginTop="10dp" android:shadowColor="#A8A8A8" android:shadowDx="0" android:shadowDy="0" android:shadowRadius="5" android:textColor="#FFFFFF" android:textSize="20sp" > </TextView> <TextView android:id="@+id/medresults3" android:layout_width="290dp" android:layout_height="50dp" android:layout_gravity="center" android:layout_marginTop="10dp" android:background="@drawable/buttonshape3" android:shadowColor="#A8A8A8" android:shadowDx="0" android:shadowDy="0" android:shadowRadius="5" android:text=" " android:textColor="#FFFFFF" android:textSize="20sp" > </TextView> <TextView android:id="@+id/medresults4" android:layout_width="290dp" android:layout_height="50dp" android:layout_gravity="center" android:layout_marginTop="10dp" android:background="@drawable/buttonshape2" android:shadowColor="#A8A8A8" android:shadowDx="0" android:shadowDy="0" android:shadowRadius="5" android:text=" " android:textColor="#FFFFFF" android:textSize="20sp" > </TextView> <TextView android:id="@+id/medresults5" android:layout_width="290dp" android:layout_height="50dp" android:layout_gravity="center" android:background="@drawable/buttonshape" android:layout_marginTop="10dp" android:shadowColor="#A8A8A8" android:shadowDx="0" android:shadowDy="0" android:shadowRadius="5" android:textColor="#FFFFFF" android:textSize="20sp"> </TextView> <TextView android:id="@+id/medresults6" android:layout_width="290dp" android:layout_height="50dp" android:layout_gravity="center" android:layout_marginTop="10dp" android:background="@drawable/buttonshape3" android:shadowColor="#A8A8A8" android:shadowDx="0" android:shadowDy="0" android:shadowRadius="5" android:text=" " android:textColor="#FFFFFF" android:textSize="20sp" > </TextView> <Button android:id="@+id/btnMathsResultsViewgraph" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="12dp" android:onClick="Graph8Handler" android:text="View Graph " android:textSize="20sp" /> </LinearLayout> 
11
  • post mathsgameresults.xml Commented Aug 24, 2014 at 10:06
  • Just edited the post showing this, thanks Commented Aug 24, 2014 at 10:10
  • is this line 323 singleScore = (Session) getIntent().getSerializableExtra("singleScore");?? Commented Aug 24, 2014 at 10:11
  • No it is: tv1.setText("Results of session:"); Commented Aug 24, 2014 at 10:16
  • 1
    The problem is in MemoryGameResults, but you've posted MathsGameResults. Also, the line you just listed in your comment does not appear anywhere in the posted code. Commented Aug 24, 2014 at 10:17

1 Answer 1

2

The problem is in these lines :

 tv1 = (TextView) findViewById(R.id.medresults1); tv2 = (TextView) findViewById(R.id.medresults2); ... 

The textviews are not being initialized. Check your XML file(memorygameresults.xml) whether textviews with these IDs exist.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, the issue was as you described. R.id.medresults1 should have been R.id.memresults1. I.e. the "d" and "m" were mixed up resulting in the error.
Glad it solved your problem. :) Would be happy if you accept this as an answer :)
Accepted, Can I just ask a quick question. In this logcat code: 08-24 10:56:44.326: E/AndroidRuntime(17074): at com.example.brianapp.MemoryGameResults.setUpAllTextViews(MemoryGameResults.java:323) does "323" relate to the line number of the which there is the error?
Yes it indicates the line number at which the Exception occured
Thanks for your help. I always have problems with exceptions

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.