2

I'm getting the "non static method cannot be referenced from a static context" error from this line:

createStudent("stu00001", new Date(631152000000)), "m", "WB", new Type_Name("Bob", "", "Smith"));

How do you form 'Date' properly? I've had a look on the API's and tried different things but I still get an error for date.

package grade_db;

import bean.Student;
import bean.Type_Name;
import bean.University;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
/**
 *
 * @author Sam
 */

public class Main {

EntityManager em;
EntityManagerFactory emf;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("db/grades.odb");

        EntityManager em;
        em = emf.createEntityManager();

        createStudent("stu00001", new Date(631152000000)), "m", "WB", new Type_Name("Bob", "", "Smith"));

        em.close();
        emf.close();
    }


    public Student createStudent(String student_id, Date dob, String gender, String nationality, Type_Name name){
    Student stu = new Student();
    stu.setDob(dob);
    stu.setGender(gender);
        stu.setName(name);
    stu.setNationality(nationality);

        stu.setCampus_id("cam00001");
        stu.setCourse_id(null);
        stu.setStudent_id(student_id);

    em.persist(stu);
    return stu;
}
}
Aaron
  • 55,518
  • 11
  • 116
  • 132

3 Answers3

5

The problem is that you're trying to call the instance method createStudent() from a static context in main(). If you change your createStudent() method to be static, you should be good to go:

public static Student createStudent(String student_id, Date dob, String gender, String nationality, Type_Name name) {
    // ... And so on 
}

EDIT: OP pointed out that this change alone gives him another error when accessing the variables em and emf. To fix that, you'd need to make those variables static, too:

static EntityManager em;
static EntityManagerFactory emf;

At that point, everything in your class is static. Assuming this is a simple one-off or example -- which I'm comfortable assuming since the class is called Main -- making everything static is just fine. In all, the code would look like this:

package grade_db;

import bean.Student;
import bean.Type_Name;
import bean.University;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
/**
 *
 * @author Sam
 */

public class Main {

    static EntityManager em;
    static EntityManagerFactory emf;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("db/grades.odb");

        EntityManager em;
        em = emf.createEntityManager();

        createStudent("stu00001", new Date(631152000000)), "m", "WB", new Type_Name("Bob", "", "Smith"));

        em.close();
        emf.close();
    }

    public static Student createStudent(String student_id, Date dob, String gender, String nationality, Type_Name name){
        Student stu = new Student();
        stu.setDob(dob);
        stu.setGender(gender);
        stu.setName(name);
        stu.setNationality(nationality);

        stu.setCampus_id("cam00001");
        stu.setCourse_id(null);
        stu.setStudent_id(student_id);

        em.persist(stu);
        return stu;
    }
}
sigpwned
  • 6,957
  • 5
  • 28
  • 48
  • 1
    Thanks! When I do this though I get the same 'non static method' error with "em.persist(stu);" –  Apr 12 '13 at 19:29
  • Updated the response. Short answer: the variables need to be static, too. – sigpwned Apr 12 '13 at 19:32
  • That worked great, thanks. Do you have any advice on calling the method 'createStudent'? I keep getting errors like "insert ;" and date format errors. It should be createStudent(String, Date, String, String, Type_Name) (TypeName is just three strings for first, middle & last name) –  Apr 12 '13 at 19:46
2

Make the method

       public static Student createStudent(String student_id, Date dob, String gender, String nationality, Type_Name name){
Student stu = new Student();

static

Seid.M
  • 185
  • 7
0

this is because CreateStudent is a member method of an object. Main is static and is not part of an object.

You need to create a new Main object, or make student static in order for this to work.

If you make createStudent static it will work.

public static Student createStudent(String student_id, Date dob, String gender, String nationality, Type_Name name)
Menelaos
  • 23,508
  • 18
  • 90
  • 155