开发者

New annoying NPE on Yabe (Creating a custom editor)

开发者 https://www.devze.com 2023-04-01 07:04 出处:网络
I\'m winding throught this Yabe tutorial and have been happily ge开发者_StackOverflow中文版t bugs and solving them on my own.... until now.

I'm winding throught this Yabe tutorial and have been happily ge开发者_StackOverflow中文版t bugs and solving them on my own.... until now.

in

http://localhost:9000/@documentation/guide9

This is the part about customizable edit window..

For whatever reason, when I post a new message, via

http://localhost:9000/admin/new

I receive a null pointer around tags...

In /app/controllers/Admin.java (around line 48)
44:             post.content = content;
45:             post.tags.clear();
46:         }
47:         //Set tags list
48:         for(String tag : tags.split("\\s+")){
49:             if(tag.trim().length() > 0) {
50:                 post.tags.add(Tag.findOrCreateByName(tag));
51:             }
52:         }
53:         // Validate
54:         validation.valid(post);

I looked at Admin.java and Tag.java and compared them line for line with the samples and tests copy. The only difference is an inclusion of validation on aAdmin.java for what I imagine is some test scripts written later down the road..

Any ideas?

here is my admin...

package controllers;

import play.*;
import play.mvc.*;

import java.util.*;

import models.*;

@With(Secure.class)
public class Admin extends Controller {

@Before
static void setConnectedUser() {
    if(Security.isConnected()) {
        User user = User.find("byEmail", Security.connected()).first();
        renderArgs.put("user", user.fullname);
    }
}

public static void index() {
    List<Post> posts = Post.find("author.email", Security.connected()).fetch();
    render(posts);
}

public static void form(Long id) {
    if(id != null) {
        Post post = Post.findById(id);
        render(post);
    }
    render();
}

public static void save(Long id, String title, String content, String tags) {
    Post post;
    if(id == null) {
        // Create post
        User author = User.find("byEmail", Security.connected()).first();
        post = new Post(author, title, content);
    } else {
        // Retrieve post
        post = Post.findById(id);
        post.title = title;
        post.content = content;
        post.tags.clear();
    }
    //Set tags list
    for(String tag : tags.split("\\s+")){
        if(tag.trim().length() > 0) {
            post.tags.add(Tag.findOrCreateByName(tag));
        }
    }
    // Validate
    validation.valid(post);
    if(validation.hasErrors()) {
        render("@form", post);
    }
    //Save
    post.save();
    index();
}
}

here is my tag.java

package models;

import java.util.*;
import javax.persistence.*;

import play.db.jpa.*;
import play.data.validation.*;

@Entity
 public class Tag extends Model implements Comparable<Tag> {

@Required
public String name;

private Tag(String name) {
    this.name = name;
}

public static Tag findOrCreateByName(String name) {
    Tag tag = Tag.find("byName", name).first();
    if(tag == null) {
        tag = new Tag(name);
    }
    return tag;
}

public static List<Map> getCloud() {
    List<Map> result = Tag.find(
        "select new map(t.name as tag, count(p.id) as pound) from Post p join p.tags as t group by t.name"
    ).fetch();
    return result;
}

public String toString() {
    return name;
}

public int compareTo(Tag otherTag) {
    return name.compareTo(otherTag.name);
}

 }


In the form that calls the save() method you might be missing an input with name 'tags'. Something like:

<input id="tags" name="tags" type="text" value="" />

In the tutorial there is a template with:

 <p>
        #{field 'tags'}
        <label>Enter some tags:</label>
        <input type="text" size="50" 
            name="${field.name}" value="${post?.tags?.join(' ')}" />
        #{/field}
    </p>

Check that you have it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号