Franco Franco - 4 months ago 9
Android Question

Cannot fix Java null pointer error

I have some code that when I run it returns a null pointer error. This is the code and error from the logcat:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.res.Resources;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import java.lang.String;
import java.util.Random;


public class MainActivity extends AppCompatActivity {

private static final Random r_generator = new Random();
String textViewString;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
String[] myString = res.getStringArray(R.array.colorArray);
String q = myString[r_generator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.color_text);
tv.setText(q);

Button green= (Button) findViewById(R.id.green_button);
green.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
if (!textViewString.equals("Green")) return;
Resources res = getResources();
String[] myString = res.getStringArray(R.array.colorArray);
String q = myString[r_generator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.color_text);
tv.setText(q);
}
});

Button blue = (Button) findViewById(R.id.blue_button);
blue.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
if (!textViewString.equals("Blue")) return;
Resources res = getResources();
String[] myString = res.getStringArray(R.array.colorArray);
String q = myString[r_generator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.color_text);
tv.setText(q);
}
});

Button red = (Button) findViewById(R.id.red_button);
red.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
if (!textViewString.equals("Red")) return;
Resources res = getResources();
String[] myString = res.getStringArray(R.array.colorArray);
String q = myString[r_generator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.color_text);
tv.setText(q);

}
});

}
}


I have tried looking at other illegal state error questions but I am still lost on what is going wrong with my individual case.

This is the error code:

E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.cedric.learnthecolors.MainActivity$1.onClick(MainActivity.java:31)
at android.view.View.performClick(View.java:4476)
at android.view.View$PerformClick.run(View.java:18795)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:177)
at android.app.ActivityThread.main(ActivityThread.java:5496)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
at dalvik.system.NativeStart.main(Native Method)


This is my layout file:

<LinearLayout 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"
android:theme="@android:style/Theme.Holo.Light"
tools:context="com.example.cedric.learnthecolors.MainActivity"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/color_text"
android:text="@string/Green"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="68sp"
android:gravity="center"
android:textAllCaps="true"/>

</LinearLayout>

<LinearLayout android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">

<Button
android:id="@+id/green_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick"
android:background="@drawable/green_button"/>

<Button
android:id="@+id/blue_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick"
android:background="@drawable/blue_button"/>

<Button
android:id="@+id/red_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick"
android:background="@drawable/the_red_button"/>

</LinearLayout>

<LinearLayout android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">

<Button
android:id="@+id/yellow_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick"
android:background="@drawable/yellow_button"/>

<Button
android:id="@+id/white_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick"
android:background="@drawable/white_button"/>

<Button
android:id="@+id/orange_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick"
android:background="@drawable/orange_button"/>

</LinearLayout>

<LinearLayout android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">

<Button
android:id="@+id/brown_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick"
android:background="@drawable/brown_button"/>

<Button
android:id="@+id/pink_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick"
android:background="@drawable/pink_button"/>

<Button
android:id="@+id/purple_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick"
android:background="@drawable/purple_button"/>

</LinearLayout>

</LinearLayout>


Thanks so much!!!

Answer

The problem is this line:

!textViewString.equals()

Because you've not given the value to the textViewString anywhere before using it. Assign it to an empty string at least, or simply delete that line since at the moment it's not doing you much good, I don't really get it's point. Since you're nowhere changing it's value, none of your buttons will work (even without the exception).