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.toStr ing);
}
//never gets here
System.out.println("Test.g etTranCode : " + test[0].getTransactionCode ());
System.out.println("Test.g etInfoType : " + test[0].getMenuInfoType()) ;
} // end otherfunction
TIA
DoughBoy
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
test[0].setMenuInfoType(4)
} catch (Exception e) {
System.out.println(e.toStr
}
//never gets here
System.out.println("Test.g
System.out.println("Test.g
} // end otherfunction
TIA
DoughBoy
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks