-1

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!!!

Franco
  • 1
  • 4

4 Answers4

2

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).

Vucko
  • 7,240
  • 2
  • 25
  • 44
1

You are not setting the onClickListener for your red button, you are reassigning the listener to the blue button.

Button red = (Button) findViewById(R.id.red_button);
blue.setOnClickListener(new View.OnClickListener() {

Have you also defined the onClick property in the xml layout file? If so then you need to ensure you have a method matching the name you have specified. For example if you have entered

onClick="buttonClicked"

You need to define a method called

public void buttonClicked() { /* OnClick functionality here */ }

See: @https://stackoverflow.com/a/38547338/1540698 from @vucko for your second issue.

Community
  • 1
  • 1
Matthew Cawley
  • 2,680
  • 1
  • 27
  • 42
  • How do you know @Vucko? Can you explain how this may not be an issue given the lack of information from the poster? – Matthew Cawley Jul 25 '16 at 08:40
  • Looking at the adjusted information from the user, it looks like this may very well be an issue as the poster is assigning a onClick listener via xml and then re-assigning one via code. As the code for the red button was not being assigned, this left a dead method call in the onClick in the XML. – Matthew Cawley Jul 25 '16 at 08:41
  • I know, as you can see my comment, I noticed that 22 min before you wrote this answer. But that was only one part, the main problem is the `textViewString`. – Vucko Jul 25 '16 at 08:48
  • 1
    @Vucko - Good spot, probably explains why it crashes with 2 different errors then. The null pointer on the one you spotted and the illegal state from the one I spotted :D I have up voted your answer :) – Matthew Cawley Jul 25 '16 at 08:54
  • Thanks, mate. I had spotted both tho, only he fixed the first one from my comment, so I didn't mention it in the answer :D – Vucko Jul 25 '16 at 09:00
0

Try to change the theme and check if it works.

 android:theme="@android:style/Theme.Holo.Light"
Gent
  • 5,805
  • 1
  • 35
  • 40
0

In your Layout, you say:

android:onClick="onClick"

It appears that you did not include all of the code for the Activity but I'd bet that it does not define a method named onClick

All you need to do is define the method.

G. Blake Meike
  • 6,482
  • 3
  • 22
  • 39