Solved

Array of Classes

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 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

11 Experts available now in Live!

Get 1:1 Help Now