My Java code crashes when I try to call a method from an array of objects -


i’m learning java , trying have array of class. when call methed array crashes. works fine if not array

public void oncreate(bundle savedinstancestate) {   super.oncreate(savedinstancestate);   cdate test=new cdate();   test.setday(0);   mappoitments = new cdate[24];   // crashes why?????   mappoitments[0].setday(0);  

you haven't filled array objects. have to:

  cdate[0] = test; 

otherwise have null @ index 0, , cannot invoke on null.

and next time ask question, give needed details:

  • what exception message , stacktrace. "crashes" means nothing
  • tell variables not initialized in code snippet. can see 1 answer telling fix local declaration, instance variable.

Comments