• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

I got an error

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone, I am sure that this is a simple fix but i cannot figure this out. I am somewhat new at coding Java. My public static void main(String[] args) is generating a error, void is an invalid for the variable main, and also I think this is an illegal start of expression, correct?
Can any one help to fix this? I have been racking my brain for a few days now and decided to come to yall for help. Also I am testing this for Android App just for fun.
Thanks.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You are not allowed to declare a method within another method.

Henry
 
Kevin D Brown
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
You are not allowed to declare a method within another method.

Henry



I seen that, but every time I try to move it out or change it, I get other errors...

Thanks for the quick reply.
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, think about this logically. You get errors when you knowingly do it the wrong way, and you also get errors when you try to do it the right way. Which set of errors is the one that we should be addressing in this topic?

Hint: not the ones you've posted.

(P.S. When posting errors, copy and paste them verbatim.)
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, what sort of errors do you get when you don't have it nested?
What does your code look like then?

How should this program be executed? The main method is meant to be a starting point for the rest of your application, but you seem to have some Android Activity stuff happening as well, so it might not apply here.

 
Kevin D Brown
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have done this way as well... Now "tv" cannot be resolved...
 
Bear Bibeault
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:
(P.S. When posting errors, copy and paste them verbatim.)

 
Kevin D Brown
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote:Ok, what sort of errors do you get when you don't have it nested?
How should this program be executed? The main method is meant to be a starting point for the rest of your application, but you seem to have some Android Activity stuff happening as well, so it might not apply here.



It should print out within the Android App
 
Kevin D Brown
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

Bear Bibeault wrote:
(P.S. When posting errors, copy and paste them verbatim.)



This what Visual Studio 2015 said when I mouse over tv... "Error: tv cannot be resolved"
 
Kevin D Brown
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have ran this code many different ways... I have one with no errors but does not output...
 
Kevin D Brown
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With is setup "public static void main(String[] args)" I get these errors

this.. Error Cannot use this in a static context
setContentView(tv).. Error: Cannot make a static reference ti the non-static method setContentView(View) from the type Activity

My second setup like this "public void main(String[] args)" I get no errors and it runs but has no output....
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kevin D Brown wrote:With is setup "public static void main(String[] args)" I get these errors

this.. Error Cannot use this in a static context
setContentView(tv).. Error: Cannot make a static reference ti the non-static method setContentView(View) from the type Activity



The error is pretty clear. Static methods don't require an instance, and hence, there isn't a this variable in that scope... Have you learned the difference between static and instance methods yet?

Henry
 
Stefan Evans
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static void main method applies to java standalone applications only. I'm pretty sure that you don't actually want a main method here.
My suggestion would be to start simpler.
Delete the whole "main" method. Only have the onCreate.

Step #1: Make the program start up and display a message.
Does the following work? Play with it until it does.



Step 2:
Create a method: "getMessage()" and use it to generate your message text.



Step 3: Take the code that is currently in your main method, and use it in the getMessage() method to get the text you want .

 
Kevin D Brown
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Stefan Evans and to all. But Stefan Evans suggestion works thank you!!! here is the code...

Stefan Evans wrote:

Step 3: Take the code that is currently in your main method, and use it in the getMessage() method to get the text you want .


 
Marshal
Posts: 80217
423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kevin D Brown wrote:. . .

This is what the code should have looked like:-I am not saying anything about compiler errors or anything like that, but formatting errors are serious too. Because you hadn't indented the code correctly, you could not see what was happening, nor where methods start and finish. That is why you indent code. Can you get VS to do the indentation automatically?
 
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:formatting errors are serious too

Non descriptive variable names are error-prone too. I have in mind: e, i, g (i don't see a reason to declare "i" outside for loop). Don't use "if (randomNumbers.size() <= 10)", instead, use "if (randomNumbers.size() <= g)" <<< but that g should say something more than g only.
 
Kevin D Brown
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the suggestions I'll try them later I'll be at work for the next nine hours
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I noticed something when going through your code correct me if i am wrong but when you define the first method have you tried to place an end bracket to separate it and main this will not disconnect the two like a double end bracket but should fix the error if it is a illegal expression error.
 
Kevin D Brown
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alaric Mustoe wrote:I noticed something when going through your code correct me if i am wrong but when you define the first method have you tried to place an end bracket to separate it and main this will not disconnect the two like a double end bracket but should fix the error if it is a illegal expression error.



Im not sure, after looking at every thing I changed the coding using Set, HashSet Interger and While statements to generate random number. You guys help me out a lot Thank You.
 
It is difficult to free fools from the chains they revere - Voltaire. tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic