简体   繁体   English

mongo db gridfs在播放框架2.3.x Java中引发空指针异常

[英]mongo db gridfs throws null pointer exception in play framework 2.3.x java

I am following this link: mongoDBgridfs . 我正在关注此链接: mongoDBgridfs I am using exact code .but while executing blob method it gives null pointer exception. 我正在使用精确的代码。但是在执行blob方法时,它会给出空指针异常。 At this line 在这条线

    newUser.photo = new Blob(file, type);

but the image is present, I am sure that picture path is correct, there is no mistake in path. 但是图像存在,我确定图片路径正确,路径没有错误。 one more thing which is notice is: when I give a wrong path then it saves a empty object in database. 需要注意的另一件事是:当我输入错误的路径时,它将在数据库中保存一个空对象。 So it's not database connectivity problem also. 因此,这也不是数据库连接问题。 I am not getting why this is happening. 我不明白为什么会这样。 Is their a mistake in documentation or what? 他们是文档错误还是什么?

    @Document(collection = "newuser")
    public class NewUser extends Model {

   public String firstName;
   public String lastName;
   public Blob photo;
   DB db;


public static String fileExtension(String fileName) {
    int mid= fileName.lastIndexOf(".");
    return fileName.substring(mid+1, fileName.length());
}
// photo upload handler
public static  void uploadPhoto( java.io.File file) throws IOException {
    System.out.println(file);
    NewUserRepository newUserRepository=new NewUserRepository();
    NewUser newUser=new NewUser();
   // String desc="etc";
   // GridFsHelper.storeFile(desc,file);
    String type = "image/" + NewUser.fileExtension(file.getName());
    System.out.println("type"+type);
   newUser.photo = new Blob(file, type);

//    newUserRepository.save(newUser);
}

public  GridFS getGridFS() {

    return new GridFS(db);
}

// fetch photo
public static void getPhoto(String userId) {
    NewUserRepository newUserRepository=new NewUserRepository();


}}

My controller is 我的控制器是

    public static Result gridFsDemoIndex() {
 java.io.File file=new java.io.File("/Users/rajeshbansal/pics/demo2.jpg");

    try {

        NewUser.uploadPhoto(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return ok("gridfs worked");
}

the exception which comes is 例外是

    java.lang.NullPointerException: null
    at leodagdag.play2morphia.Blob.set(Blob.java:50) ~[play2-morphia-plugin_2.9.1-0.0.6.jar:0.0.6]
    at leodagdag.play2morphia.Blob.<init>(Blob.java:34) ~[play2-morphia-plugin_2.9.1-0.0.6.jar:0.0.6]
    at controllers.gridfsdemo.NewUser.uploadPhoto(NewUser.java:37) ~[na:na]
    at controllers.Application.gridFsDemoIndex(Application.java:27) ~[na:na]
    at 

please help me out. 请帮帮我。

there was problem in database connectivity.i was merging mongo with morphia module but not giving databse settings to morphia.then i decided to do only with mongo db and followed this link 数据库连接出现问题。我正在将mongo与morphia模块合并,但不给morphia提供数据库设置。然后我决定只对mongo db进行操作并遵循此链接

http://howtodoinjava.com/2014/07/23/java-mongodb-getsave-image-using-gridfs-apis/.its done http://howtodoinjava.com/2014/07/23/java-mongodb-getsave-image-using-gridfs-apis/。完成了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM