2014-11-10 8 views
0

나는이 링크를 따라 가고 있습니다 : mongoDBgridfs. 내가 정확한 코드를 사용하고 있습니다.하지만 blob 메서드를 실행하는 동안 그것은 null 포인터 예외를 제공합니다. 이 줄에mongo db gridfs가 재생 프레임 워크 2.3.x에서 null 포인터 예외를 throw합니다. java

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

이미지가 있지만 이미지 경로가 맞다고 확신합니다. 경로에 실수가 없습니다. 한 가지 더 주목해야 할 것은 : 잘못된 경로를 주면 데이터베이스에 빈 오브젝트를 저장한다는 것입니다. 그래서 데이터베이스 연결 문제도 아닙니다. 나는 이것이 왜 일어나고 있는지 알지 못한다. 문서화에서 무엇을 잘못 했습니까?

@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(); 


}} 

내 컨트롤러 인

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"); 
} 

오는 예외는

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 

이 저를 도와주세요입니다.

+1

첫째 : 사용자에게 뭔가 잘못되었습니다. 컨트롤러에서 새 사용자를 만들고 그림을 업로드하려고합니다. 업로드 기능 안에서는 또 다른 사용자를 생성합니다. 두 번째 : NullPointerException은 파일이 null임을 의미하지 않습니다. 생성 된 사용자가 null인지 확인하십시오! – Peanut

+0

@Peanut 정적으로 메서드를 변경하고 직접 호출, 다시 오류가오고있다. –

+0

사진을 설정하기 전에 "newUser"가 null인지 확인하십시오. (uploadPhoto 함수 내부) – Peanut

답변