0

I am having a problem with a null pointer exception. Please specify some general cases of it.

Here is my code:

public class GuessActivity extends ListActivity implements OnClickListener
{
EditText GNum;
Button nxt;
int[] a = new int[4];
int[] d = new int[4];
int t = 0;
ArrayList<HashMap<String, String>> output = new ArrayList<HashMap<String, String>>();
HashMap<String, String> out = new HashMap<String, String>();
String KEY_NUM , KEY_BULLS , KEY_COWS;


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);
    a = getIntent().getExtras().getIntArray("num");

    nxt.setOnClickListener(this);
}


@Override
public void onClick(View v) 
{
    if(v.getId() == R.id.btNxt)
    {
        String gN = GNum.getText().toString();
        int l = gN.length();
        int b =0;
        int c =0;
        if(l!=4)
        {
            Toast.makeText(getApplicationContext(),"Number should be of 4 digits",
                    Toast.LENGTH_LONG).show();
        }
        else
        {
            t++;
            int x = Integer.parseInt(gN);

            for(int i=0;i<4;i++)
            {
                d[i] = x%10;
                x = x/10;
            }
            if(t>8)
                Toast.makeText(getApplicationContext(), "You Lost the Game",
                        Toast.LENGTH_SHORT).show();
            else
            {

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

                if(b == 4)
                    Toast.makeText(getApplicationContext(), "You Win",
                        Toast.LENGTH_SHORT).show();
            }
            out.put(KEY_NUM , gN);
            out.put(KEY_BULLS ,String.valueOf(b));
            out.put(KEY_COWS , String.valueOf(c));

            output.add(out);


            ListAdapter 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 });

            setListAdapter(adapter);
            ListView lv = getListView();
        }
    }
}
}

Here is the stack trace:

03-04 06:32:58.960: W/dalvikvm(844): threadid=1: thread exiting with uncaught exception (group=0xb3ab2ba8)
03-04 06:32:58.970: E/AndroidRuntime(844): FATAL EXCEPTION: main
03-04 06:32:58.970: E/AndroidRuntime(844): Process: com.example.game, PID: 844
03-04 06:32:58.970: E/AndroidRuntime(844): java.lang.NullPointerException
03-04 06:32:58.970: E/AndroidRuntime(844):  at com.example.game.GuessActivity.onClick(GuessActivity.java:66)
03-04 06:32:58.970: E/AndroidRuntime(844):  at android.view.View.performClick(View.java:4438)
03-04 06:32:58.970: E/AndroidRuntime(844):  at android.view.View$PerformClick.run(View.java:18422)
03-04 06:32:58.970: E/AndroidRuntime(844):  at android.os.Handler.handleCallback(Handler.java:733)
03-04 06:32:58.970: E/AndroidRuntime(844):  at android.os.Handler.dispatchMessage(Handler.java:95)
03-04 06:32:58.970: E/AndroidRuntime(844):  at android.os.Looper.loop(Looper.java:136)
03-04 06:32:58.970: E/AndroidRuntime(844):  at android.app.ActivityThread.main(ActivityThread.java:5017)
03-04 06:32:58.970: E/AndroidRuntime(844):  at java.lang.reflect.Method.invokeNative(Native Method)
03-04 06:32:58.970: E/AndroidRuntime(844):  at java.lang.reflect.Method.invoke(Method.java:515)
03-04 06:32:58.970: E/AndroidRuntime(844):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-04 06:32:58.970: E/AndroidRuntime(844):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-04 06:32:58.970: E/AndroidRuntime(844):  at dalvik.system.NativeStart.main(Native Method)

Line 66 is in the onClick method: d[i] = x % 10

7
  • 2
    Have you checked which value is null at the line of your where your exception is thrown? Commented Mar 4, 2014 at 11:56
  • "Reply asap" -> no good. Commented Mar 4, 2014 at 11:57
  • general cases. A null pointer exception happens when you try to use ("dereference") an object variable which has had no object assigned to it (it is "uninitialised"). All objects variables in Java are set to null until you do initialise them. Object myObject; myObject.toString(); gives NullPointerException. Object myObject = new Object();myObject.toString(); works fine. BTW, learn to use the debugger, then finding these is really easy. Commented Mar 4, 2014 at 11:58
  • what is value of x? Commented Mar 4, 2014 at 11:59
  • 4
    Also, don't be lazy. Use proper names, with proper naming conventions, for your code. You will not be able to debug your own code in 6 months because it is so hard to read. Commented Mar 4, 2014 at 12:01

3 Answers 3

2

Looks like the EditText (R.id.etGuessNum) doesn't have a value in it.

You should sanitize your input to avoid null references. I would also advise you check to see the value that they have entered is a valid number.

I would also have a quick read through naming conventions for Java.

Sign up to request clarification or add additional context in comments.

4 Comments

wouldn't this throw a NumberFormatException instead of a NullPointerException?
Also, he checks for the length of the EditText. It would print a toast.
@Manu - if the length isn't 4, it would step in to the else block.
@RossDrew You're right. It must have been on another line.
0
  1. Your 3 Strings KEY_NUM, KEY_BULLS, KEY_COWS are null.

  2. Also, are you sure a is not null? a = getIntent().getExtras().getIntArray("num"); might return null.

  3. Change

    if(l!=4)
    {
        Toast.makeText(getApplicationContext(),"Number should be of 4 digits",
                Toast.LENGTH_LONG).show();
    }
    

    to

    if(l!=4)
    {
        Toast.makeText(getApplicationContext(),"Number should be of 4 digits",
                Toast.LENGTH_LONG).show(); 
        return;
    }
    

4 Comments

the NPE is before that.
Isn't the problem on line d[i] = x%10;
It can't be, because d, i and x are ints (or arrays of int), which don't throw a NPE
That's what he says in the description.
-1

change this:

String gN = GNum.getText().toString(); //can be null

to:

String gN = GNum.getText().toString(); //can be null
if(gN == null) 
return;

int l = gN.length(); //if edit text is empty this will be null

2 Comments

getText never returns null. Check the documentation
and Integer.parseInt(null) would throw a different exception to the one shown in this case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.