Hi,
I'm getting the error msg, "non-static can't be referenced from a static context", on my Readfile... what is the simplest way of fixing it? Thanks.

import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import javax.mail.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;


public class Bulkloademail {

    public static void main(String[] args) 
    {

    try  {
        String strMethod = "sendMail";
        String fileResults = "Results.csv";
            String fileValues = "Values.csv";
            String strSender = "xxx.com";
            String recp[] = new String[1];
        String body; 

            recp[0] = "xxx.com";

            String file[] = new String[2];

            file[0] = "E_WORK_DIST_CRIT_060713_1_Error.csv";
            file[1] = "E_WORK_DIST_CRIT_060713_1.csv";

        String strresult = Readfile("E_WORK_DIST_CRIT_060713_1_Error.csv");
        body = body + strresult;

            String strSubject = "ECHS - Bulkload Reports ";
            String strMessage = body;


        TestMail mail = new TestMail();         
        mail.postMail(recp,strSubject,strMessage,strSender,file);

            }
            catch(Exception e)
            {
        System.out.println("Sendmail exception");   
            }

    }

    public String Readfile(String file)
        {
        String content;
        FileInputStream fis = new FileInputStream(file);
        int x= fis.available();
        byte b[]= new byte[x];
        fis.read(b);
        content = new String(b);
        return content;

        }


}

Dani AI

Generated

The compile error means a static method (main) is trying to call an instance member. As noted, one quick fix is to make the file-reading routine static so main can call it directly. As pointed out, the alternative is to construct a Bulkloademail object and call its instance method — that is the OOP-friendly approach when the method needs object state.

A safe modern implementation (static helper) avoids manual stream handling and character-set bugs:

static String readFile(String path) throws IOException {
    byte[] bytes = java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(path));
    return new String(bytes, java.nio.charset.StandardCharsets.UTF_8);
}

If the method should remain instance-based, call it on an object:

Bulkloademail loader = new Bulkloademail();
String result = loader.readFile("errors.csv");

Other issues observed in the original post: the message body variable is declared but never initialized (local variables must be initialized before use); the FileInputStream is not closed (use try-with-resources or Files APIs); and using available() to size a read buffer is unreliable. Also ensure the read method either handles or declares IOExceptions.

Checklist for a correct, robust fix:

  • Decide static vs. instance based on whether object state is needed.
  • Initialize local variables (use StringBuilder for repeated concatenation).
  • Use Files.readAllBytes / Files.readString or try-with-resources for streams.
  • Catch or declare checked exceptions (IOException).
  • Close resources or rely on NIO helpers to avoid leaks.

These changes remove the static-context error and make the file-reading code safer and clearer for later maintenance.

Recommended Answers

All 4 Replies

Make the method static.

How do I do that? As you can tell, I'm very new to Java. Thanks!

public static String Readfile(String file)

or else create an instance of

Bulkloademail class and call the method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.