0

I am getting a "non-static method cannot be referenced from a static context" - which I believe basically means that the object is not getting created. What am I doing wrong?

public void loadData() {
    String csvFile = "/data/patientList.csv";
    BufferedReader br = null;

    try {

        br = new BufferedReader(new FileReader(csvFile));
        br.readLine();
        String line1 = null;
        while ((line1 = br.readLine()) != null) {
            String[] patient = line1.split(",");
            int bedNum = Integer.parseInt(patient[0]);
            Patient patient1 = new Patient(bedNum, patient[1], patient[2], patient[3], patient[4],
                    RESPIRATORY_RATE, HEART_RATE, SYSTOLIC, SPO2);

throws errors here, for example:

patientNameField.setText(Patient.getFistName());
2
  • 1
    Which line has the error? The error in question means that you are calling a member function from within a static function (there's no "this" available). But I can't tell from the code you've posted where the problem is. Not enough information. Commented Oct 29, 2014 at 23:27
  • I get the error wherever i try to use the Patient class objects. What information would be helpful? Commented Oct 29, 2014 at 23:44

1 Answer 1

1

This error sounds like your trying this:

public static void main(String args[]) {
    loadData();
}

public void loadData() { /* method code */ }

This doesn't work, because the method loadData is not static and needs an object to be called on.

Change your code as follows:

public class MyClass { // you can name your class like you want
    public static void main(String args[]) {
        final MyClass instance = new MyClass(); //assuming there is a non-argument constructor
        instance.loadData(); // call "loadData" on a specifc instance of MyClass
    }

    public void loadData() { /* method code */ }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the feedback ... I had a little play around and i now have this PatientJFrame instance = null; try { instance = new PatientJFrame(); } catch (IOException ex) { Logger.getLogger(PatientJFrame.class.getName()).log(Level.SEVERE, null, ex); } instance.loadData(); I am still getting the error when ever i try to use the Patient objects though.
@ClaaziX You're getting the exact same error message? Or a different one?
@ClaaziX Then please update the source code in your question. Necessary information are: the line there the error occurs, the method in there that line is and the called method. The whole implementation of loadData() is not necessary if there is no error in there.
Patient.getFistName() ... what have you just learned :D? This won't work, as well, for the same reason as mentioned in my answer. Use the Patient instance of which you're trying to get the name. Maybe patient1? As a clarification why this is necessary: consider 5 different patients with own instances. Every instance contains the name of the patient it represents. How should Java know which name you want to get by calling Patient.getFistName()? That is why you have to call non-static methods on objects/instances to "tell" Java on which object the method should be performed.
Thanks Tom! :) I figured that something like that might be the case, i simplified it for the purposes of this question - i was actually trying to do a for loop, iterating over all the names to match a particular name and then display all the instances of that particular object....but it's time for bed!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.