• 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

Errors calling a method from another class

 
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to make a GUI based form to calculate service costs at Joes Automotive. The program works fine up until I tried to get information from other class files for use in the button action listeners. I've spent several days fighting to get it to this point and am near cross eyed from staring at this code. The only other question I have about this, is regarding the NonRoutinePanel.java class, I'm not sure if my math logic or the try catch is set up right. We rushed through error handling last chapter and right as we finished the chapter and got the assignment we immediately continued onto the next chapter, which hardly allows any time to actually process what is learned. I'm giving it my best shot. It's just that rushing through all of this so fast makes it hard. As of about 2 years ago I had never heard of visual basic, java as a programming language, or c# or any of this. As far as I knew, java was a little program that you kept up to date to make stuff work. I've taken a crash course in basic website coding, a course that worked with a program called Alice, the bare minimums of visual basic, and some java. I'm trying, it's just really fast paced and I went from having never heard of any of this to where I'm at now. Anyway, any help is much appreciated.

Errors



JoesAutomotiveDemo.java:108: error: non-static method getRoutineTotal() cannot be referenced from a static context
subtotal = RoutinePanel.getRoutineTotal() +
^
JoesAutomotiveDemo.java:109: error: non-static method getCleaningTotal() cannot be referenced from a static context
CleaningPanel.getCleaningTotal() +
^
JoesAutomotiveDemo.java:110: error: non-static method getNonRoutineTotal() cannot be referenced from a static context
NonRoutinePanel.getNonRoutineTotal();
^
JoesAutomotiveDemo.java:134: error: non-static method clear() cannot be referenced from a static context
RoutinePanel.clear();
^
JoesAutomotiveDemo.java:135: error: non-static method clear() cannot be referenced from a static context
CleaningPanel.clear();
^
JoesAutomotiveDemo.java:136: error: non-static method clear() cannot be referenced from a static context
NonRoutinePanel.clear();
^
6 errors




JoesAutomotiveDemo.java


RoutinePanel.java


NonRoutinePanel.java



CleaningPanel.java
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JoesAutomotiveDemo.java Those methods are instance methods and you are using them as if they were static methods.
You need to call them on appropriate instances.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nikki Smith wrote:Anyway, any help is much appreciated.


Well, the error is pretty clear.

It not only tells you where the error is, but precisely what it is:
You are using the name of a class (RoutinePanel) to call an instance method (getRoutineTotal()).

The construct
  RoutinePanel.getRoutineTotal()
is used to call static methods, and ONLY static methods; which getRoutineTotal() plainly isn't.

You do, however, have a RoutinePanel variable in your JoesAutomotiveDemo class, so try using that.

HIH

Winston

PS: I gave you a cow for a nicely formatted, well-thought-out question. I wish more people were like you.
 
Nick Smithson
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:
You do, however, have a RoutinePanel variable in your JoesAutomotiveDemo class, so try using that.


That almost worked. I'm still a bit shaky on some concepts and terminology, mainly because it get chucked at us super fast.

These are the changes I've made:

totals



clear


but I'm still getting this error:

JoesAutomotiveDemo.java:113: error: unreported exception Exception; must be caught or declared to be thrown
nonRoutine.getNonRoutineTotal();
^
1 error



I have a try/catch statement in the NonRoutinePanel.java class file. I was a bit worried about that causing problems, but the error says the problem lies in the Demo file, so I'm not understanding why this method call is behaving differently than the others. Thank you for the help by the way.


PS: I gave you a cow for a nicely formatted, well-thought-out question. I wish more people were like you.



Thank you.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NonRoutinePanel#getNonRoutineTotal() is declared to throw Exception.
That means, whenever you use it it must be enclosed in a try-catch or within a method that also declares it throws the exception (or its superclass).

You don't need throws in this method.

By the way, the code above could be better. You are throwing an exception in try just to catch and handle it in catch.
That is using an exception for a flow control and should be avoided.
A simple if would suffice.
 
Nick Smithson
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:NonRoutinePanel#getNonRoutineTotal() is declared to throw Exception.
That means, whenever you use it it must be enclosed in a try-catch or within a method that also declares it throws the exception (or its superclass).

You don't need throws in this method.


Still a bit confused with that. Removing the throws from the method header fixed it. So, if throws is in a method header, then anytime you call that method elsewhere, because throws is in the method header, that means you have to use a try/catch around for the calling statement?

Paweł Baczyński wrote:By the way, the code above could be better. You are throwing an exception in try just to catch and handle it in catch.
That is using an exception for a flow control and should be avoided.
A simple if would suffice.


Yeah, I know. x.o Not sure why, but it's how we're being instructed to do that for now. It was the same in our last assignment. I think he just wants to actually see us using try/catch to try to get the idea across with a hands-on application. It's bad practice, but using an if would defeat using try/catch, and most of the programs we've been learning to make have been small compared to what I'd imagine actual programs on the internet would look like. I think that's why he puts such requirements in place, otherwise we'd just use if statements all the time and would never get to use a try/catch. At least, I don't think so.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nikki Smith wrote:So, if throws is in a method header, then anytime you call that method elsewhere, because throws is in the method header, that means you have to use a try/catch around for the calling statement?


That is correct as long as the exception is not a RuntimeException.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic