Solved

Array of Classes

Posted on 2002-07-18
2
208 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:DoughBoy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 50 total points
ID: 7163094
you have to instantiate each of the elements in the array:

ie)
for (int i=0;i<test.length;i++) {
   test[i] = new MenuInfo();
}
0
 

Author Comment

by:DoughBoy
ID: 7163188
that did the trick!!!!!
thanks
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question