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
nullat the line of your where your exception is thrown?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.x?