Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declaring an array of objects of a class ???

 
Douglas Braxton
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12180
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Douglas Braxton
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12180
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic