THEROFLBOAT THEROFLBOAT - 4 months ago 36
Java Question

Cannot run app in Android Studio. NullPointerException

I'm trying to launch a simple app that when you click a button (StartButton), it will launch a Mix() method which will scramble a word chosen randomly from a String array (wordPick) and set the text of a textfield (ScambleF) to it. From there, another button (EnterButton) reads from an editible text field (EnterF) and checks to see if the user inputted word in the EnterF textfield matches the word unscrambled.

Unfortunately I keep getting the following error immediately on launching the app in my emulator. I've searched for an answer here but so far the only thing that has helped is not initializing my variables before onCreate().

EDIT: There is a chronometer added, but right now it has no functionality.



FATAL EXCEPTION: main
Process: com.example.roland.scramblegame, PID: 19156

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.roland.scramblegame/com.example.roland.scramblegame.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:87)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:541)
at android.content.Context.getString(Context.java:409)
at com.example.roland.scramblegame.MainActivity.Fill(MainActivity.java:64)
at com.example.roland.scramblegame.MainActivity.onCreate(MainActivity.java:27)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 





Here is my Java file

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;
import java.util.Random;
public class MainActivity extends AppCompatActivity {

String []wordPick;
int track;
Button StartButton, EnterButton;
TextView ScrambleF;
EditText EnterF;
MainActivity run;

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

run = new MainActivity();
wordPick = new String[16];
wordPick = run.Fill(wordPick);
StartButton = (Button)findViewById(R.id.StartB);
EnterButton = (Button)findViewById(R.id.EnterB);

StartButton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
Random rand = new Random();
track = rand.nextInt(16);
ScrambleF = (TextView)findViewById(R.id.Question);
ScrambleF.setText( run.Mix(wordPick[track]) );
}
}
);

EnterButton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
EnterF = (EditText) findViewById(R.id.Answer);
if (EnterF.getText().toString().equalsIgnoreCase(wordPick[track])) {
//Correct
}
else {
//Incorrect
}
}
}
);
}


public String[] Fill (String[] array) {
//Im just going to add random words
if (array == null) {
array = wordPick;
}
array[0] = getString(R.string.W_Bottle);
array[1] = getString(R.string.W_Samurai);
array[2] = getString(R.string.W_Keyboard);
array[3] = getString(R.string.W_Shrimp);
array[4] = getString(R.string.W_Project);
array[5] = getString(R.string.W_Planet);
array[6] = getString(R.string.W_Walnut);
array[7] = getString(R.string.W_PacMan);
array[8] = getString(R.string.W_Shelter);
array[9] = getString(R.string.W_Android);
array[10] = getString(R.string.W_Toilet);
array[11] = getString(R.string.W_Numbers);
array[12] = getString(R.string.W_Anime);
array[13] = getString(R.string.W_Goldfish);
array[14] = getString(R.string.W_Phantom);
array[15] = getString(R.string.W_Jigsaw);

return array;
}

public String Mix (String str) {
//This scrambles up the words in the string array
if(str == null) {
str = wordPick[track];
}
char[] characters = str.toCharArray();
for (int i = 0; i < characters.length; i++) {
int randomIndex = (int)(Math.random() * characters.length);
char temp = characters[i];
characters[i] = characters[randomIndex];
characters[randomIndex] = temp;
}

str = String.valueOf(characters);
return str;
}
}


The manifest

<?xml version="1.0" encoding="utf-8"?>




<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>




And if needed I'll just leave the activity_main.xml here too

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.roland.scramblegame.MainActivity"
android:background="#006699">

<Chronometer
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chronometer"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#ffffff"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="SCRAMBLED WORD"
android:id="@+id/Question"
android:enabled="false"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:background="#427030"
android:padding="5dp"
android:layout_above="@+id/Answer"
android:layout_centerHorizontal="true"
android:layout_marginBottom="70dp" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Answer"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:width="250dp"
android:background="#dddddd"
android:height="45dp"
android:clickable="false"
android:singleLine="true"
android:minHeight="35dp"
android:inputType="text" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/StartButton"
android:id="@+id/StartB"
android:layout_below="@+id/Answer"
android:layout_alignStart="@+id/Answer"
android:layout_marginTop="43dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/EnterButton"
android:id="@+id/EnterB"
android:layout_alignTop="@+id/StartB"
android:layout_alignEnd="@+id/Answer" />

</RelativeLayout>

Answer

I think the error maybe come from the code below in you onCreate:

run = new MainActivity();
wordPick = new String[16];
wordPick = run.Fill(wordPick);

Are you sure you need to create an activity by new Activity()? It's a little strange and not recommended.

try to replace the three lines with :

wordPick = Fill(new String[16]);

Maybe this can help.