Heres my MainActivity java code:
package peter_seeproductions.deapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import java.util.ArrayList;
import java.lang.Math;
public class MainActivity extends AppCompatActivity {
class Body{
String eng;
String ger;
public Body(String a, String b){
eng = a;
ger = b;
}
public Body(){
}
}
class buttonVal{
Body word = new Body();
public buttonVal(Body x){
word = x;
}
public buttonVal() {
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
buttonVal btn1 = new buttonVal();
buttonVal btn2 = new buttonVal();
buttonVal btn3 = new buttonVal();
buttonVal btn4 = new buttonVal();
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
Button button3 = (Button)findViewById(R.id.button3);
Button button4 = (Button)findViewById(R.id.button4);
public void List(int btn){
ArrayList<Body> list = new ArrayList<Body>();
list.add(new Body("der Finger","Finger"));
list.add(new Body("der Oberarm","Upper arm"));
list.add(new Body("der Unterarm","Lower arm"));
list.add(new Body("der Ellbogen","Elbow"));
list.add(new Body("die Hüfte","Hip"));
list.add(new Body("der Oberschenkel","Upper leg"));
list.add(new Body("der Unterschenkel","Lower leg"));
list.add(new Body("das Knie","Knee"));
list.add(new Body("die Zehe","Toe"));
list.add(new Body("der Fuss","Foot"));
list.add(new Body("das Bein","Leg"));
list.add(new Body("die Hand","Hand"));
list.add(new Body("der Bauch","Abdomen"));
list.add(new Body("der Nabel","Bellybutton"));
list.add(new Body("der Arm","Arm"));
list.add(new Body("die Brust","Breast"));
list.add(new Body("die Schulter","Shoulder"));
list.add(new Body("der Hals","Throat"));
list.add(new Body("der Mund","Mouth"));
list.add(new Body("die Nase","Nose"));
list.add(new Body("das Auge","Eye"));
list.add(new Body("das Haar","Hair"));
list.add(new Body("der Kopf","Head"));
int q1,q2,q3,q4;
if (btn == 0){
q1 = (int)(Math.random()* (list.size() - 1));
do {
q2 = (int)(Math.random()*(list.size() - 1));
}while(q2 == q1);
do {
q3 = (int)(Math.random()*(list.size() - 1));
}while(q3 == q2 || q3 == q1);
do {
q4 = (int)(Math.random()*(list.size() - 1));
}while(q4 == q3||q4==q2||q4==q1);
btn1.word = list.get(q1);
btn2.word = list.get(q2);
btn3.word = list.get(q3);
btn4.word = list.get(q4);
button1.setText(btn1.word.ger);
button2.setText(btn2.word.ger);
button3.setText(btn3.word.ger);
button4.setText(btn4.word.ger);
}
}
public void Next(View view){
List(0);
}
}
It all seems to be fine, however when I run it I get a null pointer exception which crashes the app. I have not been doing java for too long, and I am not very good at finding these bugs. When I run I get this error code:
03-07 23:55:43.885 32387-32387/peter_seeproductions.deapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: peter_seeproductions.deapp, PID: 32387
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{peter_seeproductions.deapp/peter_seeproductions.deapp.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2124)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5097)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1892)
at peter_seeproductions.deapp.MainActivity.<init>(MainActivity.java:79)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1084)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2115)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5097)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Ive tried looking for the null pointer but I really have no clue