简体   繁体   中英

Null Pointer Exception in string

I am getting a null pointer exception in my code.Please help me to solve it . Here is my code.

package com.example.game;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class GuessActivity extends ListActivity implements OnClickListener
{

EditText GNum;
Button nxt;
int[] a = new int[4];
int[] d = new int[4];
int[] b = {0,0,0,0,0,0,0,0};
int[] c = {0,0,0,0,0,0,0,0};
int[] x = new int[8];
int t = 0;
String [] gN = new String[8];
String [] gB = new String[8];
String [] gC = new String[8];
ListAdapter adapter;
/*ArrayList<HashMap<String, String>> output = new ArrayList<HashMap<String, String>>();
HashMap<String, String> out = new HashMap<String, String>();*/
//String KEY_NUM = "1" , KEY_BULLS="2" , KEY_COWS="3";

private class list 
{
    TextView guess;
    TextView bulls;
    TextView cows;

}

list lv = null;

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.guess_activity);

    GNum = (EditText) findViewById(R.id.etGuessNum);
    nxt = (Button) findViewById(R.id.btNxt);



    int y = getIntent().getExtras().getInt("num");
    for(int i=0;i<4;i++)
    {
        a[i] = y%10;
        y = y/10;
    }

    /*adapter = new SimpleAdapter(this,output,R.layout.list_item,
            new String[]{KEY_NUM , KEY_BULLS , KEY_COWS},new int[]{
            R.id.tvGuessNum , R.id.tvBulls , R.id.tvCows});*/

    nxt.setOnClickListener(this);
    GNum.setOnClickListener(this);
}



@Override
public void onClick(View v) 
{

    if(v.getId() == R.id.btNxt)
    {
        t++;
        if(t>8)
        {
            Toast.makeText(getApplicationContext(), "You Lost the Game",
                    Toast.LENGTH_SHORT).show();
            return;
        }
        else
        {
            gN[t-1] = GNum.getText().toString();
            int l = gN[t-1].length();
            if(l!=4)
            {
                Toast.makeText(getApplicationContext(),"Number should be of 4 digits",
                    Toast.LENGTH_LONG).show();
                return;
            }
            else
            {
                x[t-1] = Integer.parseInt(gN[t-1]);
                for(int i=0;i<4;i++)
                {
                    d[i] = x[t-1]%10;
                    x[t-1] =  x[t-1]/10;
                }


                if(d[0] == a[0])
                    b[t-1]++;
                if(d[0] == a[1] || d[0] == a[2] || d[0] == a[3])
                    c[t-1]++;
                if(d[1] == a[1])
                    b[t-1]++;
                if(d[1] == a[0] || d[1] == a[2] || d[1] == a[3])
                    c[t-1]++;
                if(d[2] == a[2])
                    b[t-1]++;
                if(d[2] == a[1] || d[2] == a[0] || d[2] == a[3])
                    c[t-1]++;
                if(d[3] == a[3])
                    b[t-1]++;
                if(d[3] == a[1] || d[3] == a[2] || d[3] == a[0])
                    c[t-1]++;

                if(b[t-1] == 4)
                {
                    Toast.makeText(getApplicationContext(), "You Win",
                        Toast.LENGTH_SHORT).show();
                    return;
                }
                gB[t-1] = Integer.toString(b[t-1]);
                gC[t-1] = Integer.toString(c[t-1]);

                lv = new list();
                lv.guess = (TextView) findViewById(R.id.tvGuessNum);
                lv.bulls = (TextView) findViewById(R.id.tvBulls);
                lv.cows = (TextView) findViewById(R.id.tvCows);

                lv.guess.setText(gN[t-1]);
                lv.bulls.setText(gB[t-1]);
                lv.cows.setText(gC[t-1]);

                /*out.put(KEY_NUM, gN[t-1]);
                out.put(KEY_BULLS ,String.valueOf(b[t-1]));
                out.put(KEY_COWS , String.valueOf(c[t-1]));
                output.add(t-1 , out);
                setListAdapter(adapter);*/
            }
        }
    }
    else if(v.getId()==R.id.etGuessNum)
    {
        GNum.setText("");
    }
}
}

Here is my log cat.

03-08 02:58:55.760: E/AndroidRuntime(861): FATAL EXCEPTION: main
03-08 02:58:55.760: E/AndroidRuntime(861): Process: com.example.game, PID: 861
03-08 02:58:55.760: E/AndroidRuntime(861): java.lang.NullPointerException
03-08 02:58:55.760: E/AndroidRuntime(861):  at com.example.game.GuessActivity.onClick(GuessActivity.java:138)
03-08 02:58:55.760: E/AndroidRuntime(861):  at android.view.View.performClick(View.java:4438)
03-08 02:58:55.760: E/AndroidRuntime(861):  at android.view.View$PerformClick.run(View.java:18422)
03-08 02:58:55.760: E/AndroidRuntime(861):  at android.os.Handler.handleCallback(Handler.java:733)
03-08 02:58:55.760: E/AndroidRuntime(861):  at android.os.Handler.dispatchMessage(Handler.java:95)
03-08 02:58:55.760: E/AndroidRuntime(861):  at android.os.Looper.loop(Looper.java:136)
03-08 02:58:55.760: E/AndroidRuntime(861):  at android.app.ActivityThread.main(ActivityThread.java:5017)
03-08 02:58:55.760: E/AndroidRuntime(861):  at java.lang.reflect.Method.invokeNative(Native Method)
03-08 02:58:55.760: E/AndroidRuntime(861):  at java.lang.reflect.Method.invoke(Method.java:515)
03-08 02:58:55.760: E/AndroidRuntime(861):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-08 02:58:55.760: E/AndroidRuntime(861):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-08 02:58:55.760: E/AndroidRuntime(861):  at dalvik.system.NativeStart.main(Native Method)

line 138 is in on click method -> if -> else ->else -> lv.bulls.setText(gB[t-1];

Probably because you haven't instantiated lv anywhere. It's initialized as null in the beginning and the same value is used at line 138.

Probably your lv.bulls is null . Please make sure you have TextView with id = tvBulls (case-sensitive) at your layout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM