Solved

Array of Classes

Posted on 2002-07-18
2
201 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

930 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now