angga
angga

Reputation: 1

Help cannot calling non-static method model in play framework controller?

I've been following play framework 1.1.1 YABE adding comment tutorial then ends up with this problem.

the problem is this error message appears

Compilation error

The file /app/controllers/Application.java could not be compiled. Error raised is : Cannot make a static reference to the non-static method addComment(String, String, boolean, String) from the type Permainan

This is my Application.java controller content

package controllers;

import play.*; 
import play.mvc.*;
import play.libs.*;
import java.util.*; 
import models.*;

public class Application extends Controller { 

  public static void permainanKomentar(Long permainanId, String author, String email, boolean showEmail, String content) {
    Permainan permainan = Permainan.findById(permainanId);
    Permainan.addComment(author, email, showEmail, content); >> (this line) 
    show(permainanId);
  }
}

and for the Permainan.java model

package models;

import java.util.*;
import javax.persistence.*;
import play.data.binding.*;
import play.db.jpa.*;
import play.data.validation.*;

@Entity
public class Permainan extends Model {

  @Required
  public String nama;

  @Required
  @MaxSize(5000)
  @Lob
  public String deskripsi;

  @MaxSize(2000)
  @Lob
  public String material;

  @MaxSize(4000)
  @Lob
  public String syair;

  public Date postedAt;

  @OneToMany(mappedBy="permainan", cascade=CascadeType.ALL)
  public List<Komentar> komentar;

  @ManyToMany(cascade=CascadeType.PERSIST)
  public Set<Tag> tags;

  public Permainan(String nama, String deskripsi, String material, String syair) {

    this.komentar = new ArrayList<Komentar>();
    this.tags = new TreeSet<Tag>();
    this.nama = nama;
    this.deskripsi = deskripsi;
    this.material = material;
    this.syair = syair;
    this.postedAt = new Date();
  }

  public Permainan addComment(String author, String email, boolean showEmail, String content) {
    Komentar newKomentar = new Komentar(this, author, email, showEmail, content).save();
    this.komentar.add(newKomentar);
    this.save();
    return this;
  }
}

Upvotes: 0

Views: 994

Answers (1)

bobylito
bobylito

Reputation: 3392

Java is a case sensitive language, which means you have to be careful with the case of the words you are using. In your example :

Permainan permainan = Permainan.findById(permainanId);

Here you define an instance of the class Permainan which is called permainan (note that you are using the same same for the class and the instance with sligh difference in case).

Permainan.addComment(author, email, showEmail, content);

Here you use Permainan class and not the instance; and there is not static method called addComment for your object.

So I think you should use :

permainan.addComment(author, email, showEmail, content);

Upvotes: 1

Related Questions