• Post Reply Bookmark Topic Watch Topic
  • New Topic

I got an error  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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
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

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

Henry
 
Kevin D Brown
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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.
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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: 1838
10
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I have done this way as well... Now "tv" cannot be resolved...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
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
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: 1838
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 56600
172
  • Mark post as helpful
  • send pies
  • 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: 4930
334
BSD
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!