Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Array of Classes

Posted on 2002-07-18
2
Medium Priority
?
211 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 200 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

609 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