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

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -