"non-static variable cannot be referenced from a static context"?

180 views
Skip to first unread message

Alex Hall

unread,
Jul 13, 2016, 5:52:46 PM7/13/16
to play-fr...@googlegroups.com
Hello list,
I'm new to Play and am learning it for work. I used Java in college some five or six years ago, and have a computer science degree, plus use Python and other languages regularly. Java is taking a bit of re-acquainting, but it's pretty easy to get the basics.

The error in the subject is appearing as I try to work through a tutorial:
https://www.lxpert.com/showarticle/36/Build-your-first-Play-Framework-web-application

The line in question reads like this, in models/Actor.java:
return find.where().eq("id", _id).findUnique();

I know that it means I'm trying to call an instance method on the class, rather than an instance of the class (at least, I'm reasonably sure of that). The problem is that 'find' isn't mine, it's just something the tutorial tells me to use. The whole static versus non-static thing is confusing; some tutorials say to use it, then there are posts about how it's being removed in newer versions, then when you remove too many statics you suddenly get errors and have to put them back in for proper form handling.

If anyone knows where I'm going wrong with this, I'd appreciate a pointer. I'll paste all of Actor.java below, so you have it all, but it's mostly copied exactly from the afore mentioned tutorial. I just changed a few variable names.

//models\Actor.java:
package models;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import play.data.validation.Constraints;
import play.db.ebean.*;
import java.util.*;

@Entity
public class Actor extends Model {

@Id
@GeneratedValue
public Long id;
@Constraints.Required
public String firstName;
@Constraints.Required
public String lastName;
public Short age;
public String role;

public Actor() {
}

public String toString() {
return "ID: "+id+". Name: "+firstName+" "+lastName+".";
}

public Model.Finder<Long, Actor> find = new Model.Finder(Long.class, Actor.class);
public static Actor findByID(Long _id) {
return find.where().eq("id", _id).findUnique();
}

public static List<Actor> findAll() {
return find.all();
}

}
--
Alex Hall
Automatic Distributors, IT department
ah...@autodist.com

Greg Methvin

unread,
Jul 13, 2016, 6:05:03 PM7/13/16
to play-framework
Hi Alex,

It looks like that tutorial uses a very old version of Play (2.1.5). Unfortunately many of the tutorials for earlier versions of Play used statics quite liberally, a practice which we now discourage (and many static APIs are deprecated).

If you want to use the static APIs, you'd need to make "find" static. But if you're starting a new app I'd suggest using Play 2.5 and avoiding statics altogether. They are bad for modularity and testability.

Greg

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/CA%2BQ8_JfLRfYrWrKZ7JztS%2Bi-WH%2B-8CePEyAYymx14FvNDDWAkQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.



--
Greg Methvin
Senior Software Engineer

Alex Hall

unread,
Jul 13, 2016, 6:10:27 PM7/13/16
to play-fr...@googlegroups.com
I'm on 2.5--I installed the latest version two days ago. What I'm wondering is how to go about making this tutorial work? By making classes non-static, I've been able to get pretty far; forms, saving to a database with default settings, passing things to templates--but now I'm stuck. Is there a tutorial written for 2.5 that walks the reader through the basics like this one does? Things are making sense, but I'm not quite ready to try something on my own yet, partly because of the large number of imports to worry about and partly because this whole thing is still new enough to me that the little things aren't quite there for me. I can strike out on my own, using this list for help if the internet doesn't have answers, but if there is a modern tutorial somewhere, I'd like to follow it first. Thanks!


For more options, visit https://groups.google.com/d/optout.



--

Greg Methvin

unread,
Jul 13, 2016, 6:16:36 PM7/13/16
to play-framework
Hi Alex,

To make it work, you'd have to make classes/controllers non-static and inject the proper APIs (e.g. instead of using JPA you'd inject JPAApi in your constructor and call methods on that). You'd probably want to create a repository class of some sort instead of using static methods for your database models.

There's a pretty large list of tutorials for 2.5.x (and 2.4.x, which is fairly similar API-wise) in the documentation: https://www.playframework.com/documentation/2.5.x/Tutorials


For more options, visit https://groups.google.com/d/optout.

Alex Hall

unread,
Jul 14, 2016, 12:06:18 PM7/14/16
to play-fr...@googlegroups.com
Thanks for the link. Unless I'm missing something, these look to be just sample applications with no explanation save code comments? I was hoping for something more explanatory, similar to what I was following but written for 2.4 or 2.5.

As to injection, this is the first I've heard of the concept. I've been reading about it all morning, and while I get the basics, I don't really see how to adopt it in Play. The tutorials page you linked to had one tutorial on the topic, but it was written in Scala and assumed a lot of prior knowledge that I've been trying to find.

Basically, if there's something out there that runs through creating a reasonably basic Play app, explaining what's going on and why the code is what it is, that uses DI, that would probably be a huge help. I haven't yet come across one, but if anyone knows of one, I'd greatly appreciate a link. Sorry I'm asking such basic questions, but as I said, I'm re-learning the finer points of Java at the same time as learning Play, DI, and anything else that comes along in the course of this process.


For more options, visit https://groups.google.com/d/optout.

Will Sargent

unread,
Jul 14, 2016, 12:11:26 PM7/14/16
to play-fr...@googlegroups.com
Hi Alex,

There's a great explanation of dependency injection as a concept at


Thanks,
Will.

Alex Hall

unread,
Jul 15, 2016, 8:39:50 AM7/15/16
to play-fr...@googlegroups.com
Hi all,
Thanks for the links; DI is making more sense. I still don't quite see why packages exist to handle it if all it is is a design concept, but no doubt they're for far more complex applications than what I'm doing.

As to the problem in my app, Greg said I'd want to use DI for JPA (another thing I'm now reading about). I'm still researching, but am on the right track with the following?

Currently, according to the tutorial for Play I was following, in my model class files I want to import javax.persistence.*, or a subset of *. I then mark classes and/or class properties as being part of JPA, with @ID, @entity, and so on.

What I *should* try is not importing persistence classes at all. Rather, I need to make persistence some kind of object that is part of my class constructor. I don't know exactly what that might look like yet, but is this even close to right? Or am I completely off base? Thanks!


For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages