java - Null Pointer exeception handler -
i have made function in fouractivity class:
public int getrandomnumber(){ random rnd=new random(); int randomnumber=rnd.nextint(100); if (randomnumber%2==1){ randomnumber=randomnumber+1; return randomnumber; } else return randomnumber; }
i trying access method in class through way. throws java.lang.nullpointerexception
:
int randomnumber; fouractivity fouractivity; randomnumber= fouractivity.getrandomnumber(); toast.maketext(finalactivity.this,"random number is"+randomnumber ,toast.length_short).show();
fouractivity
not instantiated, that's why nullpointerexception
.
if activity, shouldn't using that, instead create , use static method:
fouractivity.getrandomnumber();
Comments
Post a Comment