struct in java for an array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: struct in java for an array

  1. #1
    Join Date
    Nov 2004
    Posts
    5

    struct in java for an array

    What's the struct (c++) equivalent in Java?

    here's my im doing.

    class Text
    {
    char character;
    int freq;
    }

    public static void main(String[] args) throws IOException
    {
    BufferedReader key = new BufferedReader(new InputStreamReader(System.in));
    Text[] letter = new Text[2];

    I want to assgined letter[0].character = 'a'; and letter[0].freq = '3' but it doesn't allow me to do that when I compile it?

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    have you tried making the variables public?

  3. #3
    Join Date
    Nov 2004
    Posts
    5
    By default, they should be.

  4. #4
    Join Date
    Sep 2004
    Posts
    150
    Assuming what you have there is just a snippet of code and not the whole thing, the problem is this:

    You are defining 'letter' to be an array of type Text.. but each element of the 'letter' array must be initialized or you will get a null pointer exception when you try to use them.

    The 'letter' array is ready to store some Text objects but it hasn't had any put in it, it's essentially an empty array.

    The fix would be to add:

    letter[0] = new Text();
    letter[1] = new Text();

    Then you can use each element of letter like you are trying to do.

  5. #5
    Join Date
    Nov 2004
    Posts
    5
    Thank you for the reply.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center