I am facing issue of NullPointerException. In my app I have two classes MainActivity and Test. I am creating object main of MainActivity inside Test class. When I call display method of MainActivity inside Test class main.display(); I get the NullPointerException. Please check my sample code below and recommend me changes where I am doing wrong.
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // Fixed
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // orientation
setContentView(R.layout.training_ten_position);
Test test = new Test(10);
}
public MainActivity() {
super();
}
public void display() {
}
}
public class Test {
.
.
.
MainActivity main;
public Test(int duration) {
// Initializing data member
main = new MainActivity();
}
public void delay() {
.
.
.
main.display();
}
startActivity