Link to home
Start Free TrialLog in
Avatar of DoughBoy
DoughBoy

asked on

Array of Classes

Hi All,
 Below is my code that compiles but give a java.nullpointer exception.

 What am I doing wrong? I have an array of classes. If I do not use an array, just a single copy, then the code works. What is wrong with my array or anything else?

-------------------------
public class MenuInfo {
         private int MenuInfoType = 0;
       private String TransactionCode = "";    

          public void init () {
              MenuInfoType = 0;
              TransactionCode = "ABCD";
          }    
       public int getMenuInfoType() {
          return MenuInfoType;
       }  

       public String getTransactionCode() {
          return TransactionCode;
       }  

       public void setMenuInfoType(int iMenuType) {
          MenuInfoType = iMenuType;
       }  

       public void setTransactionCode(String sTransactionCode) {
          TransactionCode = sTransactionCode;
       }    
    } // end class



public void otherfunction() {
MenuInfo[] test = new MenuInfo[2]; //array of classes

//using the line below instead of the line above
//will NOT compile
//MenuInfo[2] test = new MenuInfo();        


try {
    test[0].setTransactionCode("WXYZ");
    test[0].setMenuInfoType(4);
} catch (Exception e) {
    System.out.println(e.toString);
}        

//never gets here
System.out.println("Test.getTranCode: " + test[0].getTransactionCode());
System.out.println("Test.getInfoType: " + test[0].getMenuInfoType());

} // end otherfunction


TIA
DoughBoy
ASKER CERTIFIED SOLUTION
Avatar of bobbit31
bobbit31
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DoughBoy
DoughBoy

ASKER

that did the trick!!!!!
thanks