0

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];

yprez
  • 14,854
  • 11
  • 55
  • 70
user2796705
  • 53
  • 1
  • 4
  • can you please also post the relevant layout xml? – donfuxx Mar 08 '14 at 08:08
  • 2
    Insert a log statement outputting the value of `lv.bulls`. Your find may be returning `null`. – chrylis -cautiouslyoptimistic- Mar 08 '14 at 08:09
  • 1
    2 possibility exists, 1- `gB[t-1]` is null , 2- `lv.bulls` is null. first check `gB[t-1]` by log, if is not null check id of `bulls` – Shayan Pourvatan Mar 08 '14 at 08:11
  • 1
    please post `guess_activity` XML – Shayan Pourvatan Mar 08 '14 at 08:16
  • 1
    do you have a view with id `R.id.tvBulls` in your layout? – donfuxx Mar 08 '14 at 08:19
  • 1
    possible duplicate of [A good way to debug nullPointerException](http://stackoverflow.com/questions/17871825/a-good-way-to-debug-nullpointerexception) – vstm Mar 08 '14 at 08:20
  • 1
    @vstm all NPE related questions gonna be a duplicate of that now or what? :-P – donfuxx Mar 08 '14 at 08:24
  • 1
    @donfuxx: Yes, because Stackoverflow is not "debugging my NPE as a service". The answer to his problem won't be useful to anybody else but him. If he would put more effort into debugging he could find the answer on his own, without sending the community on a wild goose chase full of speculation and "maybe ...". If he reaches a point where he does not understand why a certain framework call returns `null`, **then** he can ask the community and the answer will serve anybody who uses the same functionality. – vstm Mar 08 '14 at 08:31
  • @vstm : The user has at least spotted the line where NPE happened - that is the _first step_ in analyzing the NPE - Also that "certain framework" is plain android api, millions use it, why should an answer not be of use for others? – donfuxx Mar 08 '14 at 08:41

2 Answers2

0

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.

AndyFaizan
  • 1,833
  • 21
  • 30
0

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

nikis
  • 11,166
  • 2
  • 35
  • 45