• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method Call  RSS feed

 
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im calling a method from within a class like so:




The class is xmlWriter & the method is updateFile

updateFile passes Environment environment (another class) and Document doc (document builder)

but I am unsure about it passing these through the parameters in the method call..is it correct?

Also underneath that method call for the update could I then do another method call for the export method of the file?
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the object reference is called xmlWriter. At least if you are using the normal naming conventions, that is. If you really have a class called xmlWriter you should change it to XmlWriter or XML_Writer or similar.
There is a big difference. If you are calling it on the name of the class, then it must be a static method, which means it can't access instance information, e.g. name of current file to write to. If it is a reference, then you are probably using an instacne method which can access such information.
No, updateFile takes environment (probably not a class but a reference pointing to information about your environment) and a reference to your Document. No, it is not passing things through its method call. You are sending things to that method.

What it does with them when it gets them—no idea. You would have to read the documentation for the update method to find out. I don't know where to find that. There is an XmlWriter interface, but I am almost 100% sure that your XmlWriter is something different.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for some reason I can't edit my main post no longer.

I have posted my code below to show why I have called it xmlWriter - also I may not of put it across very well my issue




Off this code I am getting this error

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The local variable environment may not have been initialized
The local variable doc may not have been initialized


That is my issue - apologies for any problems/misunderstanding I have caused
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alice Hampton wrote:
Off this code I am getting this error

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The local variable environment may not have been initialized
The local variable doc may not have been initialized

That is my issue - apologies for any problems/misunderstanding I have caused


It is not a good idea to run code that doesn't compile ... please read this for reasons why...

http://www.coderanch.com/how-to/java/FixAllCompilerErrorsBeforeRunningTheApplication

I recommend that you fix the compiler errors first.


And BTW, the error messages are pretty clear. Local variables must be initialized before it can be used.

Henry
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Henry says, the original question doesn't actually tell us what problem you were facing.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I fixed the issues I was getting by putting my code back to how it was but I have other errors now so I guess I need to sort through them and hopefully the project will work - cheers for the help guys.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't have such errors. If you have, it suggests you are writing too much code before compiling. If you write more than one method without compiling, or more than ten lines (including blanks or lines with one brace only) without compiling, you are simply making a rod for your own back.
Compile often, find the errors and sort them out. That way you won't be struggling with errors by the dozen. And if you are using Eclipse, push the execute arrow frequently. It will run your code and tell you what sort of errors it has found.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the tip Campbell I will keep that in mind as it sounds good practise!
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome And if Eclipse says, “There are compiler errors. Do you wish to proceed?” the answer is always no.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you said I shouldn't be getting the errors - I'm only getting them because I have set it up so environment and doc is now null

So my updateFile method passes two null values



Do I need to make it so that Document doc equals something other than null as well as environment I'm assuming?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you did not have environment set to null. You didn't set it to anything. That is even less than setting it to null.

You can try setting doc to null and seeing what sort of Exception you get. If that method needs an environment and a document it needs an environment and a document. Both must have real values. It needs the environment to know how to read (character encoding etc., I presume) and it needs the document to read from. If there isn't a document, what is it going to read? I presume you have some source for your document.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why has environment changed type? It used to be Environment, now it is Node.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's always been that - I've never changed it, I just have not displayed that piece of code
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I must have read it wrongly.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No worries, thank you for all the replies you have given
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!