Jump to content

Java Question


Recommended Posts

I've a fair bit of C++ programming in the past, but I'm having to use Java at the moment and I'm stuck. I want to store some related information together, and what I would normally do is create a struct, but I tried using a class instead. I need an array of these classes, but do't know the length of the array in advance so I did the following:

 

class data_object

{

public String code = " ";

public int qty = 0;

}

 

class program

{

private data_object[] data;

 

private some_function()

{

int temp = 5;

data_object[] data = new data_object[temp];'

 

data[0].code = "Hello";

}

}

 

But when I run the program it comes up with a long list of exceptions. What blantly obvious mistake have I made?

Link to comment
Share on other sites

First question would be what are the first couple of errors that you are getting?

 

Your first problem is that a String is an object. Therefore, you need to create a string object.

 

public String code = new String;

 

I know you can declare a string object as follows:

 

public String code = " ";

 

but you may run in problems with the length.

 

Also, I don't think you need to declare an object twice. Your data object has already been declared so when you initialize it you can simply do this:

 

data[] = new data_object[temp];

 

Try that and see what happens

Link to comment
Share on other sites

The best thing to do if you do not know how the size of the array is to use a Vector, or an ArrayList.

 

 

Create the Vector:

java.util.Vector dataVector=new java.util.Vector();

 

Declare the object:

data_object data;

 

Later... you need to instantiate it, and populate it with data.

data=new data_object();

data.code="Code";

data.qty=3;

 

Then you can add it to the Vector.

dataVector.addElement(data);

 

Repeat as often as you like.

 

When you want to retreive the elements from the Vector, you'd do the following:

 

java.util.Enumeration dataEnum=dataVector.elements();

while (dataEnum.hasMoreElements()){

data=(data_object)dataEnum.nextElement();

...

do something with the data...

...

}

 

The java API on Sun's site is a terrific resource for code syntax and method references.

 

Good luck.

Link to comment
Share on other sites

Thanks for the quick reply! I've made the changes you suggested but I'm still getting errors:

 

java.lang.NullPointerException

  at t2_server.read_stock_file<t2_server.java:126>



class data

{

  public String stock_code = new String(); 

  public int quantity;

  public String description = new String();

}





class t2_server

{

    private data[] data_store;



    public int read_stock_file(String filename)

   {

        int data_items = 5;

        data_store = new datat[data_items];

 data_store[0].stock_code = "hello";

    }

}

Link to comment
Share on other sites

here is the working code:

public class dt{



   public static void main (String args[]){

      

      t2_server t = new t2_server();

      t.read_stock_file("whatever");

      System.out.println("Stored : ["+ t.readValue(0) + "]");

   }

}



class data

{

   public String stock_code;

   public int quantity;

   public String description;



   public data(){

       stock_code = null;

       quantity = 0;

       description = null;    

   }



}





class t2_server

{

   private data[] data_store;

   

   public t2_server(){

       int data_items = 5;

       data_store = new data[data_items];

       for (int i=0; i < data_items; i++)

           data_store[i] = new data();

       

   }    

   

   public int read_stock_file(String filename)

   {   

       data_store[0].stock_code = "hello";

       return 0;

   }

   

   public String readValue(int index){

       try{

           return data_store[index].stock_code;    

       }    

       catch( Exception e){

           return null;    

       }

   }

}

 

the answer is that you need to explicitly allocate memory space for each element of the array. when you executed the statement

data_store= new data[data_items]

you were just saying that the data_store variable has the a capacity to reference "data items" number of data objects. those objects still doesnt have any memory associated with them (actually the references are pointed to null by the jvm as specified in the specification).

 

oh, i cleaned up the code a little and made use of constructors. i hope you dont mind. :)

 

ciao!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...