The Artima Developer Community
Sponsored Link

Java Answers Forum
new user's error

2 replies on 1 page. Most recent reply: Apr 27, 2004 6:29 AM by Amy Rancourt

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Amy Rancourt

Posts: 2
Nickname: luckygirl
Registered: Apr, 2004

new user's error Posted: Apr 24, 2004 12:42 PM
Reply to this message Reply
Advertisement
I seem to get this error each time I write a program...but I can't remember why I get it or how to fix it. Here is the code:

if (source == add) {
double sales_amt = new Double(sales_field.getText()).doubleValue();
Customer newCustomer = new Customer(company_field.getText(),contact_field.getText(),sales_amt);
newList.add(newCustomer);
if (Customer.findCustomer(newList) == false) //but addCustomer calls findCustomer?????
Customer.addCustomer(newList);
customerList = SalesRegion.getList();
i = customerList.size();
info_display_text += customerList.get(i);

company_field.setText("");
contact_field.setText("");
sales_field.setText("");
}

and I am getting the following errors:

"InputData.java": Error #: 308 : non-static method findCustomer(java.util.ArrayList) cannot be referenced from a static context at line 56, column 20
"InputData.java": Error #: 308 : non-static method addCustomer(java.util.ArrayList) cannot be referenced from a static context at line 57, column 18
"InputData.java": Error #: 308 : non-static method getList() cannot be referenced from a static context at line 58, column 36

Thanks for any help on the code and any tips to help me remember how not to make this error again.

Thanks,
Amy


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: new user's error Posted: Apr 24, 2004 8:05 PM
Reply to this message Reply
You haven't listed enough code for me to be absolutely positive what the error refers to, but I can explain what the general error message means and perhaps you can find the specific error yourself.

Basically, you are trying to call an instance method in the same fashion that you would call a class (static) method. For example, lets imagine that you create a JLabel
JLabel myLabel = new JLabel("I am a label");

If you decide to change the text, you can call the setText(String) method. Here is a right and wrong example.
myLabel.setText("This is the new text"); //RIGHT - called by object
JLabel.setText("This is the new text"); //WRONG! - called from class

The setText(String) method is an instance method because it sets the text of a specific JLabel. You cannot call that method from the class, you must use the name of the object. The first example above will work, but the other will cause an error like the ones that you are getting.

I suspect that you are making a similar error. Here is what I think is the problem.

Customer.findCustomer(newList) should be newCustomer.findCustomer(newList)

Customer.addCustomer(newList) should be newCustomer.addCustomer(newList)

SalesRegion.getList() needs to be someObjectName.getList() where someObjectName is something that you have declared and instantiated elsewhere.

Remember, I'm guessing about quite a bit because I don't have all of the code. I hope this helps.

twc

Amy Rancourt

Posts: 2
Nickname: luckygirl
Registered: Apr, 2004

Re: new user's error Posted: Apr 27, 2004 6:29 AM
Reply to this message Reply
Thanks so much for the help! I get it now. Hopefully it will stick this time ;-)

Flat View: This topic has 2 replies on 1 page
Topic: Core java Previous Topic   Next Topic Topic: How to simulate

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use