It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Calling the method from another method within the same class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling the method from another method within the same class" Watch "Calling the method from another method within the same class" New topic
Author

Calling the method from another method within the same class

Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
Hi Guys im trying to call my main method from another method. But im having trouble so far i've tried

main();
public static void main(String[] args);
public static void main();

And i keep getting compiler issues expecting semi-colons im not sure what im doing wrong.
I am calling the method from the same class.

regards S

My main method looks like this

Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276



would work.
You dont call a method with its complete signature like 'public static void main(String[] args)' and moreover you will have to ensure that you are giving the method all the parameters it requires.


OCPJP 6
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

How did you call the method? What are the Errors?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
In the end I called the method using main(null);

However the compiler is still not liking the calling the compiler is throwing out several of the same 2 erros below.
Apologies is there are any formatiing errors. The forum doesn't seem to like code pasted from notepad++

This is all the info i get from the compiler
ERRORS
at Find.<init>(Find.java:48)
at Find.main(Find.java:117)

The line 48 error relates to the call main(null); and the other error relates to calling the Find method.
I'll paste the code for my program below, its confusing me a little now.

regards S



Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Do you've files in C:/Documents and Settings/Kieren McDonald/Desktop/Nick/Java/Test/resources this directory? seems your br is null, check the directory.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

See that :48 in the stack trace? That's the line where the exception comes from. When I try it it's the "br.close();" line. That means that br is null.

Since it isn't needed outside the loop I'd make br a local variable, only available within the loop:
That leaves a few other problems, one being a guaranteed StackOverflowError once one of your 4 booleans is true or arrayList.contains(resourceLine). From main, you create a new Find object, which will call main, which will create a new Find object, which will call main, which...


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
The buffered reader reads in all items within the resources folder from the readin path, so yep there are a bunch of files in there. also my original program before the adaption looks like the code in this example.
And this code has been tested and works below. so the location of the buffered reader is fine.


Initially i changed the search being and end variables to match the criteria that i wanted to extract from the resource folder. However the results brought back were both repetitive and in some cases irrelevant. Because of this i used the If statments to only store the data within the array if it does not contain any of the specified symbols OR if it already exists within the array.

Im a little confused now.

regards S


Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
Also the error arises when the program is executed not when it compiles. I realise the error relates to both lines 48 and 117.

Line 48 is the main(null); line within the if check statement.
Line 117 relates to the calling of the Find method within the main method.

Which has nothing to do with the buffered reader. If you are getting path errors when running my program then thats probably either because you aren't referencing the same path.

It looks like its something to do with the way im calling the main and Find methods themselves in these two instances. I cant see how it referenced br.close.
The throw catch scenario is used to readin all items within the folder, if there are no items left the loop closes.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The line numbers you're quoting, and the code, don't match up. Too difficult to guess--either provide an offset, do the line number math for us, or do *something*. And there's no "main(null)" in the code you posted last, so I'm not sure what we're supposed to do with that information.

And if you're getting a stack trace, just post the stack trace, instead of little bits; if you're getting a compilation error, post the exact compilation error.
ravindra patil
Ranch Hand

Joined: Sep 01, 2006
Posts: 230
I think problem is with declaration
BufferedReader br = null;

not sure but put
BufferedReader br = null; inside Constructor or

in your loop where you have written br= new BufferedReader();
so directly declare and initialized there

BufferedReader br= new BufferedReader();

let me know the rsult
Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
The main(null); are removed as the post has developed so has the code. If you look up in the post there are clear references as to where the errors (were) occuring. i.e. the main method reference to the find method. .. and the main(null)reference under the If check statement. I just didnt think it was necessary to keep pasting up references when I had done so many times before.

The code itself is now compiling and running. Thank you for the comments.

The only issue now, which i believe i've posted in a new thread. Is that altho it compiles and runs its still writing irrelevant data to the specified text file. Which is a bit weird.

regards S
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Your post before this one, then, referred to code that no longer existed: re-read what you posted; it doesn't make sense if that code no longer exists.
Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
I see what you mean. however in my original post lines 48 and 117 are referenced correctly. I just took out some of the comment lines in the laterposts to save time, that was the only reason.

I took out the references to the main method because i actually realised that they werent necessary, i mean if theyre in the loop and the instance is not what i want it does not need to go back to the main. It simply needs to iterate through the IF statement until the end of the loop and then read the next line.

Staring at code for too long lol.

I'll make sure that in future posts I post exactly with reference to compiler messages etc.
regards S
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Calling the method from another method within the same class