我有一个空指针异常的问题。请指定一些一般情况。

这是我的代码:

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


这是堆栈跟踪:

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)


第66行位于onClick方法中:d[i] = x % 10

最佳答案

看起来EditText(R.id.etGuessNum)中没有值。

您应该清理您的输入以避免空引用。我还建议您检查一下,看看他们输入的值是一个有效数字。

我还将对Java的naming conventions进行快速阅读。

07-26 06:46