After I enter the credit hours and grade for each course then press the calculate the GPA button, my app just crashes.
I have tried debugging and found all the logic is working good up until the set text for my textview. Then it crashes and does not display the GPA below the button.
package com.example.gpa_tejadaa4_calculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText ch1,ch2,ch3,ch4,ch5;
private EditText g1,g2,g3,g4,g5;
private Button gpa_calc;
private TextView result;
//private int credit_sum = 0;
//private int grade_sum = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Credit Hours
ch1 = (EditText)findViewById(R.id.C_H1);
ch2 = (EditText)findViewById(R.id.C_H2);
ch3 = (EditText)findViewById(R.id.C_H3);
ch4 = (EditText)findViewById(R.id.C_H4);
ch5 = (EditText)findViewById(R.id.C_H5);
//Grades
g1= (EditText)findViewById(R.id.L_G1);
g2= (EditText)findViewById(R.id.L_G2);
g3= (EditText)findViewById(R.id.L_G3);
g4= (EditText)findViewById(R.id.L_G4);
g5= (EditText)findViewById(R.id.L_G5);
//Button
gpa_calc = (Button) findViewById(R.id.button);
//Result
result = (TextView)findViewById(R.id.result);
gpa_calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculateGPA();
}
});
}
public void calculateGPA() {
double quality_points = 0;
double credit_sum = 0;
double gpa=0;
double chour1 = Double.valueOf(ch1.getText().toString());
double chour2 = Double.valueOf(ch2.getText().toString());
double chour3 = Double.valueOf(ch3.getText().toString());
double chour4 = Double.valueOf(ch4.getText().toString());
double chour5 = Double.valueOf(ch5.getText().toString());
String l1 = String.valueOf(g1.getText());
String l2 = String.valueOf(g2.getText());
String l3 = String.valueOf(g3.getText());
String l4 = String.valueOf(g4.getText());
String l5 = String.valueOf(g5.getText());
double[] creditArray = new double[]{chour1, chour2, chour3, chour4, chour5};
String[] gradeArray = new String[]{l1,l2,l3,l4,l5};
credit_sum = chour1+chour2+chour3+chour4+chour5;
for(int i=0; i<gradeArray.length; i++){
if (gradeArray[i].equals("A")){
quality_points += (creditArray[i] * 4);
}
else if (gradeArray[i].equals("B")){
quality_points += (creditArray[i] * 3);
}
else if (gradeArray[i].equals("C")){
quality_points += (creditArray[i] * 2);
}
else if (gradeArray[i].equals("D")){
quality_points += (creditArray[i] * 1);
}
else if (gradeArray[i].equals("F")){
quality_points += (creditArray[i] * 0);
}
}
gpa = (quality_points/credit_sum);
String str = Double.toString(gpa);
result.setText(str);
}}
2019-02-15 22:14:57.347 14693-14693/? I/daa4_calculato: Not late-enabling -Xcheck:jni (already on)
2019-02-15 22:14:57.527 14693-14693/? W/daa4_calculato: Unexpected CPU variant for X86 using defaults: x86
2019-02-15 22:14:58.474 14693-14693/com.example.gpa_tejadaa4_calculator W/daa4_calculato: JIT profile information will not be recorded: profile file does not exits.
2019-02-15 22:14:58.483 14693-14693/com.example.gpa_tejadaa4_calculator I/chatty: uid=10086(com.example.gpa_tejadaa4_calculator) identical 10 lines
2019-02-15 22:14:58.483 14693-14693/com.example.gpa_tejadaa4_calculator W/daa4_calculato: JIT profile information will not be recorded: profile file does not exits.
2019-02-15 22:14:58.575 14693-14693/com.example.gpa_tejadaa4_calculator I/InstantRun: starting instant run server: is main process
2019-02-15 22:14:59.642 14693-14706/com.example.gpa_tejadaa4_calculator I/daa4_calculato: Background concurrent copying GC freed 6297(2MB) AllocSpace objects, 0(0B) LOS objects, 54% free, 1297KB/2MB, paused 14.095ms total 327.337ms
2019-02-15 22:14:59.794 14693-14693/com.example.gpa_tejadaa4_calculator W/daa4_calculato: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
2019-02-15 22:14:59.813 14693-14693/com.example.gpa_tejadaa4_calculator W/daa4_calculato: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
2019-02-15 22:15:00.449 14693-14693/com.example.gpa_tejadaa4_calculator W/daa4_calculato: JNI critical lock held for 17.117ms on Thread[1,tid=14693,Runnable,Thread*=0xe82dc000,peer=0x74473f50,"main"]
2019-02-15 22:15:00.807 14693-14693/com.example.gpa_tejadaa4_calculator D/OpenGLRenderer: Skia GL Pipeline
2019-02-15 22:15:01.401 14693-14718/com.example.gpa_tejadaa4_calculator I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2019-02-15 22:15:01.402 14693-14718/com.example.gpa_tejadaa4_calculator I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
2019-02-15 22:15:01.402 14693-14718/com.example.gpa_tejadaa4_calculator I/OpenGLRenderer: Initialized EGL, version 1.4
2019-02-15 22:15:01.402 14693-14718/com.example.gpa_tejadaa4_calculator D/OpenGLRenderer: Swap behavior 1
2019-02-15 22:15:01.439 14693-14718/com.example.gpa_tejadaa4_calculator D/EGL_emulation: eglCreateContext: 0xe23582a0: maj 3 min 0 rcv 3
2019-02-15 22:15:01.469 14693-14718/com.example.gpa_tejadaa4_calculator D/EGL_emulation: eglMakeCurrent: 0xe23582a0: ver 3 0 (tinfo 0xe2303730)
2019-02-16 00:26:44.489 14693-14693/com.example.gpa_tejadaa4_calculator I/Choreographer: Skipped 54 frames! The application may be doing too much work on its main thread.
2019-02-16 00:26:44.579 14693-14718/com.example.gpa_tejadaa4_calculator D/EGL_emulation: eglMakeCurrent: 0xe23582a0: ver 3 0 (tinfo 0xe2303730)
2019-02-16 00:26:45.337 14693-14718/com.example.gpa_tejadaa4_calculator I/OpenGLRenderer: Davey! duration=1747ms; Flags=0, IntendedVsync=45140893368546, Vsync=45141793368510, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=45141795801750, AnimationStart=45141796075440, PerformTraversalsStart=45141800459550, DrawStart=45141816295150, SyncQueued=45141885063390, SyncStart=45141887514590, IssueDrawCommandsStart=45141887629200, SwapBuffers=45142416409300, FrameCompleted=45142643449160, DequeueBufferDuration=739000, QueueBufferDuration=2893000,
2019-02-16 00:26:45.376 14693-14693/com.example.gpa_tejadaa4_calculator I/Choreographer: Skipped 50 frames! The application may be doing too much work on its main thread.
2019-02-16 00:26:45.506 14693-14718/com.example.gpa_tejadaa4_calculator I/OpenGLRenderer: Davey! duration=970ms; Flags=0, IntendedVsync=45141841971519, Vsync=45142675304819, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=45142683525380, AnimationStart=45142683649680, PerformTraversalsStart=45142684303680, DrawStart=45142686442990, SyncQueued=45142697271280, SyncStart=45142697377900, IssueDrawCommandsStart=45142697527290, SwapBuffers=45142698807910, FrameCompleted=45142812805650, DequeueBufferDuration=88000, QueueBufferDuration=1463000,
2019-02-16 00:27:17.077 14693-14693/com.example.gpa_tejadaa4_calculator W/daa4_calculato: JNI critical lock held for 22.109ms on Thread[1,tid=14693,Runnable,Thread*=0xe82dc000,peer=0x74473f50,"main"]
2019-02-16 00:27:17.116 14693-14693/com.example.gpa_tejadaa4_calculator W/daa4_calculato: JNI critical lock held for 24.765ms on Thread[1,tid=14693,Runnable,Thread*=0xe82dc000,peer=0x74473f50,"main"]
2019-02-16 00:27:36.598 14693-14693/com.example.gpa_tejadaa4_calculator D/AndroidRuntime: Shutting down VM
2019-02-16 00:27:36.601 14693-14693/com.example.gpa_tejadaa4_calculator E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.gpa_tejadaa4_calculator, PID: 14693
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.gpa_tejadaa4_calculator.MainActivity.calculateGPA(MainActivity.java:101)
at com.example.gpa_tejadaa4_calculator.MainActivity$1.onClick(MainActivity.java:47)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-02-16 00:27:36.639 14693-14693/com.example.gpa_tejadaa4_calculator I/Process: Sending signal. PID: 14693 SIG: 9
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/L_G5"
android:layout_width="113dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:ems="10"
android:hint="@string/Grade"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.87"
app:layout_constraintStart_toEndOf="@+id/C_H5"
app:layout_constraintTop_toBottomOf="@+id/L_G4"
android:importantForAutofill="no" tools:targetApi="o" />
<EditText
android:id="@+id/L_G4"
android:layout_width="113dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:ems="10"
android:hint="@string/Grade"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.94"
app:layout_constraintStart_toEndOf="@+id/C_H4"
app:layout_constraintTop_toBottomOf="@+id/L_G3"
android:importantForAutofill="no" tools:targetApi="o" />
<EditText
android:id="@+id/C_H5"
android:layout_width="114dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="@string/credit_hours"
android:inputType="textPersonName"
app:layout_constraintStart_toEndOf="@+id/c5"
app:layout_constraintTop_toBottomOf="@+id/C_H4"
android:importantForAutofill="no" tools:targetApi="o" />
<TextView
android:id="@+id/c5"
android:layout_width="67dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="50dp"
android:text="@string/course_5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/c4" />
<TextView
android:id="@+id/c3"
android:layout_width="67dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="50dp"
android:text="@string/course_3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/c2" />
<EditText
android:id="@+id/C_H4"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="@string/credit_hours"
android:inputType="textPersonName"
app:layout_constraintStart_toEndOf="@+id/c4"
app:layout_constraintTop_toBottomOf="@+id/C_H3"
android:importantForAutofill="no" tools:targetApi="o" />
<TextView
android:id="@+id/c4"
android:layout_width="67dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="50dp"
android:text="@string/course_4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/c3" />
<EditText
android:id="@+id/L_G3"
android:layout_width="113dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:ems="10"
android:hint="@string/Grade"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/C_H3"
app:layout_constraintTop_toBottomOf="@+id/L_G2"
android:importantForAutofill="no" tools:targetApi="o" />
<EditText
android:id="@+id/C_H3"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="@string/credit_hours"
android:inputType="textPersonName"
app:layout_constraintStart_toEndOf="@+id/c3"
app:layout_constraintTop_toBottomOf="@+id/C_H2"
android:importantForAutofill="no" tools:targetApi="o" />
<EditText
android:id="@+id/L_G2"
android:layout_width="112dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:ems="10"
android:hint="@string/Grade"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/C_H2"
app:layout_constraintTop_toBottomOf="@+id/L_G1"
android:importantForAutofill="no" tools:targetApi="o" />
<EditText
android:id="@+id/C_H2"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="@string/credit_hours"
android:inputType="textPersonName"
app:layout_constraintStart_toEndOf="@+id/c2"
app:layout_constraintTop_toBottomOf="@+id/C_H1"
android:importantForAutofill="no" tools:targetApi="o" />
<TextView
android:id="@+id/c2"
android:layout_width="wrap_content"
android:layout_height="21dp"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="50dp"
android:text="@string/course_2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/c1" />
<TextView
android:id="@+id/c1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="112dp"
android:text="@string/course_1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/C_H1"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:hint="@string/credit_hours"
android:inputType="numberDecimal"
app:layout_constraintEnd_toStartOf="@+id/L_G1"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/c1"
app:layout_constraintTop_toTopOf="parent"
android:importantForAutofill="no" tools:targetApi="o" />
<EditText
android:id="@+id/L_G1"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:ems="10"
android:hint="@string/Grade"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/C_H1"
app:layout_constraintTop_toTopOf="parent"
android:importantForAutofill="no" tools:targetApi="o" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="78dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:onClick="calculateGPA"
android:text="@string/compute_gpa"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/C_H5" />
<TextView
android:id="@+id/result"
android:layout_width="66dp"
android:layout_height="wrap_content"
android:layout_marginStart="170dp"
android:layout_marginLeft="170dp"
android:layout_marginTop="46dp"
android:layout_marginEnd="175dp"
android:layout_marginRight="175dp"
android:layout_marginBottom="119dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</android.support.constraint.ConstraintLayout>
expected output: calculated GPA Actual Results : App crashes