aspose file tools*
The moose likes Java in General and the fly likes problem on: passing parameter that it only reads as null for every value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "problem on: passing parameter that it only reads as null for every value" Watch "problem on: passing parameter that it only reads as null for every value" New topic
Author

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

Sara Brown
Ranch Hand

Joined: Feb 22, 2012
Posts: 55
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.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Sara Brown
Ranch Hand

Joined: Feb 22, 2012
Posts: 55
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

Joined: Dec 08, 2010
Posts: 1509
    
    5

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.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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?


Steve
Sara Brown
Ranch Hand

Joined: Feb 22, 2012
Posts: 55
thank you. it is just my silly mistakes. i am really sorry.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

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:

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18981
    
  40

Moved to the Java in General forum.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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

Joined: Dec 08, 2010
Posts: 1509
    
    5

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
 
Don't get me started about those stupid light bulbs.
 
subject: problem on: passing parameter that it only reads as null for every value