• 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

Help With My Android Calculator Project

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
###I am looking to create an android calculator - I'm a new kid on the block. I am Having some issues- I am getting 4 * error ; expected.  Here's my code:

   
   MAIN JAVA ACTIVITY>###
   
 
   
   
   
   
 

###My XML layiut file looks like this.###

   
 
   
   
###This is my manifest file. I just pasted this so you can see what is going on.###

       
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please post the full error text (is this compiling error or during runtime?), as well as showing the lines they occur on.
 
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post the full output of the compiler - it contains the crucial information in which lines those problems occurred.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
Which line does the error occur on?
 
H White
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just got home and had a look at it. It's showing the error on the java code between line 204 - 216.
 
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are multiple problems - check the output from the compiler, you should see:

DescriptionResourceLocation
sqrt cannot be resolved to a variableMainActivity.javaline 205
Incompatible operand types Button and booleanMainActivity.javaline 208
Syntax error on token "double", delete this tokenMainActivity.javaline 204
sqrt cannot be resolved to a variableMainActivity.javaline 204
sqrt cannot be resolved to a variableMainActivity.javaline 203
Incompatible operand types Button and booleanMainActivity.javaline 213
Syntax error on token "double", delete this tokenMainActivity.javaline 209
Syntax error, insert ";" to complete StatementMainActivity.javaline 36
power cannot be resolved to a variableMainActivity.javaline 209

 
H White
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've corrected by removing the advanced functions but still getting the errors or reached end of file while parsing:

 
Ron McLeod
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

H White wrote:I've corrected by removing the advanced functions but still getting the errors or reached end of file while parsing


Please post the complier error messages.
 
H White
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Errors

1: Error ')' expected
2. Error: reached end of file while parsing
3. Execution Failed for task :app:compileDebugJavaWithJavac
 
Ron McLeod
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

H White wrote:Errors

1: Error ')' expected
2. Error: reached end of file while parsing
3. Execution Failed for task :app:compileDebugJavaWithJavac


What about the line numbers?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does line 128‑129 mean? Read that bit carefully.
Don't use == true or == false which are both poor style and error‑prone.
Not
if (b == true) ...
but
if (b) ...
Not
if (b == false) ...
but
if (!b) ...
 
H White
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please see code below. The error is at line 163. It states: Error:(202, 15) error: ')' expected

and line 21 & 22. It states : Error:(202, 16) error: reached end of file while parsing





[moderator edit: disabled smilies]
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's the trouble with copy/paste programming. You're missing a matching closing parenthesis on a bunch of lines: 70, 77, 84, 92, ... and so on.

When you find yourself doing copy/paste and changing just a little bit of the copy/pasted code, stop yourself and ask: "How can I extract this to a single method and parameterize it instead?"


My bad. I mis-read what you were trying to do.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would have been easier to spot the error if you had broken up the "}}}" on line 202 to separate lines:

Ok, so I still get to get up on a soapbox after all...  

This is the trouble with HUGE methods with many, many lines of code and a lot of nested structures. You have a tendency to lose track of where you are in your nesting and matching closing parens or braces with their opening ones becomes error-prone.

And actually, the comment that I struck out in my previous post still applies to this huge method: When you find yourself doing copy/paste and changing just a little bit of the copy/pasted code, stop yourself and ask: "How can I extract this to a single method and parameterize it instead?"
 
H White
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've corrected it with the parentheses - I am now having this error at the last prentheses
Error:(201, 5) error: class, interface, or enum expected
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does your correction look exactly like this?

You need the semicolon at the end.
 
H White
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After putting semi-colon at the end. I'm still getting Error:(201, 1) error: class, interface, or enum expected
 
Ron McLeod
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post your complete code for the MainActivity class.
 
H White
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No errors now...But when I try to run it on the android physical device... it says unfortunately, calc has stopped
 
Ron McLeod
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check LogCat to see why is stopped.
 
H White
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
12-04 06:14:08.946 8186-8186/com.example.howar.calc D/AndroidRuntime: Shutting down VM
12-04 06:14:08.946 8186-8186/com.example.howar.calc E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: com.example.howar.calc, PID: 8186
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.howar.calc/com.example.howar.calc.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                         at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:154)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                         at com.example.howar.calc.MainActivity.onCreate(MainActivity.java:193)
                                                                         at android.app.Activity.performCreate(Activity.java:6662)
                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                         at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:154) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
 
Ron McLeod
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see this in the logcat output:

Caused by: java.lang.NullPointerException:
   Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
   on a null object reference at com.example.howar.calc.MainActivity.onCreate(MainActivity.java:193)

I'm not sure what you code looks like now, but most likely this is line 193:

I'm not sure if this would result in a et1 not being set properly, but your text widget is an EditText in your code:
but have it defined as a TextView in your layout:
 
Ron McLeod
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I gave it a quick test - seems to start after fixing the text widget.

 
H White
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much

Thanks a million

 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do we have a “Please use sunglasses to read this post” warning?

Those colours are a little overpowering............................... And the full stop key on my PC is misbehaving.
 
Ranch Hand
Posts: 606
11
Android Python Open BSD VI Editor Slackware
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You use a lot of if statements, that is good to start to understand the logic,  but please consider that in future you want to make the class smaller, especially if the content grows.
For instance I would suggest you to have a look at the

State pattern,

that is a way to split your class in others small ones and make your code more readable, logic and able to manage features growth,
in fact all the "if" you use can be arranged in this way https://sourcemaking.com/design_patterns/state
maybe is a bit too soon, but I would start to have a glance if your aim is to work as android programmer.

Furthermore your colors are a bit exotic indeed:) but is good if you have pay the bill for multiple drinks on the Jamaica beaches I reckon
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
Those colours are a little overpowering.



It's the perfect advert for why devs shouldn't be doing GUI design.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:. . .  shouldn't be doing GUI design.

 
Life just hasn't been the same since the volcano erupted and now the air is full of tiny ads.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic