• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calling the method from another method within the same class

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did you call the method? What are the Errors?
 
Nick Rowe
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Nick Rowe
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic