• Post Reply Bookmark Topic Watch Topic
  • New Topic

passing an object as a parameter  RSS feed

 
nizar malaikah
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello, i want to pass an object of type Software to assign it to a computer from Computer class...

i should note that computer and software are arrays of objects

thats the variable and method to set software in Computer class



note: the user choses the a computer from a list and a software as will

for example the program will show the user 2 computers

0 for computer: apple, Model: mac mini, Speed: 2.8
1 for computer: sony, Model: vaio, Speed: 2.2

the user enters the index he wants then the program will show a list of software to add to the computer selected


the error I'm having is run time error and it points to these 2 lines

1.


2. the method

every thing is running correctly but this step above

Cch= the chosen computer index

Sch= the chosen Software index

why am i getting an error and how to fix it please ??

lastly I'm sorry if I'm writing badly or in a unclear way

 
Mike Degteariov
Ranch Hand
Posts: 145
8
Java Mac MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Nizar,

It appears that your comp array is not initialized.

You have not included this part, but I guess you must have somewhere the line like



Please let me know if that's the case. If so, you need to initialize the array first.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

There is something wrong with using multiple arrays like that. It looks like error‑prone code to me.
What are you going to initialise the array to? If you write
comp = new Computer[999];
you will not resolve the error.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should be careful about variable names; you are using “software” in one place to mean a Software object and in another place to mean a Software[] object. Very confusing.

If private Software[] software; means that is a field, then it is implicitly null until you initialise it. I prefer to do that in the constructor.
I still think the method of adding software with an index to the array is poor. If the array is a field, then using an index means you are exposing internal implementation details which should remain hidden.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Conceptual problem:

hello, i want to pass an object of type Software to assign it to a computer from Computer class...


You never pass an object, you pass a reference to an object. Any number of other references to that object may occur.

Using the correct words is essential to avoiding misconceptions about what is going on in your program.

Bill
 
nizar malaikah
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You should be careful about variable names; you are using “software” in one place to mean a Software object and in another place to mean a Software[] object. Very confusing.

If private Software[] software; means that is a field, then it is implicitly null until you initialise it. I prefer to do that in the constructor.
I still think the method of adding software with an index to the array is poor. If the array is a field, then using an index means you are exposing internal implementation details which should remain hidden.




thank you.. i did what you said and initialized software and it runs without an error ..

but now I'm having a problem with the getSoftware method ! i'm confused because in the UML its


so what should i do ?

getSoftware works and displays [Ldd1318398project4.Software;@257f6796

and i want it to display the entered values
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nizar malaikah wrote:
getSoftware works and displays [Ldd1318398project4.Software;@257f6796

and i want it to display the entered values


The Java library doesn't magically know have to display any array, and in the format desired. You will have to loop through the array, and display the elements as desired.

Alternatively, you can use the java.util.Arrays class -- which will loop through the elements and print out the array based on the element's toString() method.

Henry
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Arrays class has a toString method which you can use.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!