aspose file tools*
The moose likes Beginning Java and the fly likes Declaring an array of objects of a class ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Declaring an array of objects of a class ???" Watch "Declaring an array of objects of a class ???" New topic
Author

Declaring an array of objects of a class ???

Douglas Braxton
Ranch Hand

Joined: Jan 28, 2004
Posts: 36
hi all;
i am trying to define an array of objects of class myLine
but get the following compiler error
DrawingApplet818.java [52:1] ';' expected
myLine [ ] l = new myLine[5]();
^
1 error
Errors compiling DrawingApplet818.
see the 4th non-comment line of applets paint() method.
any ideas what i am doing wrong?
thanx
db


[ April 05, 2004: Message edited by: Douglas Braxton ]
[ April 05, 2004: Message edited by: Douglas Braxton ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

I don't think you want the parens at the end of the line in question.
also, consider using a more understandable name for your reference, something like myLineArray. just a suggestion to maybe save you some pain later...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Douglas Braxton
Ranch Hand

Joined: Jan 28, 2004
Posts: 36
Originally posted by fred rosenberger:
I don't think you want the parens at the end of the line in question.
also, consider using a more understandable name for your reference, something like myLineArray. just a suggestion to maybe save you some pain later...


fred
yes i tried that already. but while the the applet will compile it fails at runtime with a

java.lang.NullPointerException execution error
db
:confused
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

I know what you're going through. I had the same growing pains when i was learning java.
Now, i'm not actually compiling/running/testing anything, but i be the problem is something like this.
As you probably know, when you declare a variable, you are creating a reference to an object. the actual object lives off in the RAM somewhere, and your variable name tells you how to get to it.
you have delcared an array. so, you have a handle to an array that can hold 5 things, in your case, a myLine object. here comes the catch. you don't have the 5 objects. so when you say
l[i].setX1(x1);
you saying "use my l reference, go to the first slot. then use the reference there to call the setX1() method". but there is nothing IN that slot!!!
you need to create the myLine() objects in the array. so a loop over the size of the array, putting in dummy values, or instead of making your four separate calls to set[XY][12], why not just call the constructor you wrote?
Let me know if this doesn't make sense. I usually type about 10 times faster than i think, and ALWAYS hit the "add reply" button before i double check everything...
Douglas Braxton
Ranch Hand

Joined: Jan 28, 2004
Posts: 36
Originally posted by fred rosenberger:
I know what you're going through. I had the same growing pains when i was learning java.
Now, i'm not actually compiling/running/testing anything, but i be the problem is something like this.
As you probably know, when you declare a variable, you are creating a reference to an object. the actual object lives off in the RAM somewhere, and your variable name tells you how to get to it.
you have delcared an array. so, you have a handle to an array that can hold 5 things, in your case, a myLine object. here comes the catch. you don't have the 5 objects. so when you say
l[i].setX1(x1);
you saying "use my l reference, go to the first slot. then use the reference there to call the setX1() method". but there is nothing IN that slot!!!
you need to create the myLine() objects in the array. so a loop over the size of the array, putting in dummy values, or instead of making your four separate calls to set[XY][12], why not just call the constructor you wrote?
Let me know if this doesn't make sense. I usually type about 10 times faster than i think, and ALWAYS hit the "add reply" button before i double check everything...


fred
sorry about being so dense. i just do not see how to accomplish getting the array of myLine objects 1- defined, 2- populated and 3- reference usable?
???
thanx
doug
Douglas Braxton
Ranch Hand

Joined: Jan 28, 2004
Posts: 36
Originally posted by Douglas Braxton:


fred
sorry about being so dense. i just do not see how to accomplish getting the array of myLine objects 1- defined, 2- populated and 3- reference usable?
???
thanx
doug

fred
thanks
i found what i needed.
db
Used a search to find my answer in beginners forum
post # 000264
poste: June 8 2000
search arg: object arrays
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Declaring an array of objects of a class ???
 
Similar Threads
JFrame application HELP Needed ! ! ! (STILL NEED HELP)
New College Project
Problem with paint method.
I need experienced replies only regarding coding standards!
Problems with Threads in an Applet