Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

problem on: passing parameter that it only reads as null for every value  RSS feed

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mainClass.java


operation.java



Output:
random: 2
random: 4
numThread: 0
totalLength: 0
object: 2
object: 4

Questions:
1- Why the numThread and totalLength prints 0 seems like a null value and not the object?
2- What should i do to make this work? solution for numThread and totalLength.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Sara Brown,

Welcome to CodeRanch!

Please post the complete code so that it would be easy to understand. e.g.
1) I do not see any main method, so I'm assuming that the code given under mainClass.java is inside main method.
2) What is '2' in first for loop of mainClass? Please avoid using magic numbers (or at least give them a meaningful variable names)
3) What is 'size'? Whose size it is?

Anyway, apart from those, lets get to your question - why numThread and Length print 0 (one more thing - please follow naming conventions - class names start with Uppercase letter, and variable names follow camelCaseFormat).

What you are doing here is:

You are taking arguments, and overwriting those by class variables. Since you haven't initialized those (again, this is purely my assumption - because you haven't completely posted the code ), those variables are getting initialized by their default values (Object is null, int is 0 and so on).
So, you are actually assigning 0 and null to input parameters before printing it.

I hope this helps.
 
Sara Brown
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry..this is my program..please do help.

It does not passing the parameter that i hope and keep read as null only the object works properly.

and return numThread in the operation class which i bold, also passing the null value


*p/s: i bold some of the code that might be error..*


in main class



operation class
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sara Brown wrote:only the object works properly


That is because, you are passing an object, and printing it without modifying at all. But as I've mentioned in my previous post, you are modifying other arguments before printing.

Please take a look carefully at my previous post - I've also mentioned the problematic part.
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sara Brown wrote:


What Anayonkar is saying (correctly) is that your two assignments are in reverse order: you are assigning the class member TO the local variable, rather than assigning the local variable TO the class member. To prove the point, change the code to this:


By adding final to the input parameter list you prevent yourself from making the mistake you are making. What error do you get? Why?
 
Sara Brown
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you. it is just my silly mistakes. i am really sorry.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sara Brown wrote:thank you. it is just my silly mistakes. i am really sorry.


You are welcome. It happens I'm glad that you understood the issue.

Besides, I think Steve Luke's latest post contains same issue - which I guess might be a typo (during copy-paste it seems those changes - reversing the assignment order - were missed)

It should be like this:

 
author
Sheriff
Posts: 23568
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the Java in General forum.

Henry
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anayonkar Shivalkar wrote:...Besides, I think Steve Luke's latest post contains same issue - which I guess might be a typo ...



No, it was supposed to have the same error to show that putting final in the parameter declaration shows the error at compile time.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Steve Luke wrote:

Anayonkar Shivalkar wrote:...Besides, I think Steve Luke's latest post contains same issue - which I guess might be a typo ...



No, it was supposed to have the same error to show that putting final in the parameter declaration shows the error at compile time.


Ah... got it. My mistake
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!