cancel
Showing results for 
Search instead for 
Did you mean: 

NullPointer@uploading a file from local system to alfresco

amit_wmw
Champ in-the-making
Champ in-the-making
Hello,

Below is the code I have written to upload a file from my local system to Alfresco repository..
####################################################

package org.alfresco.sample;

import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.BufferedInputStream;

import org.alfresco.jcr.api.JCRNodeRef;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.alfresco.repo.model.filefolder.FileFolderServiceImpl;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.model.FileInfo;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.repo.content.RoutingContentService;



public class FirstJCRClient

{

public static void main(String[] args) throws Exception
{
// access the Alfresco JCR Repository (here it's via programmatic approach, but it could also be injected)
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:alfresco/application-context.xml");
Repository repository = (Repository)context.getBean("JCR.Repository");

// login to workspace (here we rely on the default workspace defined by JCR.Repository bean)
Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));

try
{
// first, access the company home
Node rootNode = session.getRootNode();
Node companyHome = rootNode.getNode("app:company_home");

FirstJCRClient t= new FirstJCRClient();
t.tCheckin();

}

catch (Exception e)
{
e.printStackTrace();
}
finally
{
session.logout();
System.exit(0);
}
}
public void tCheckin()
{
try{
NodeRef root= new NodeRef(".+://.+/." + "companyHome");

System.out.println("Inside tcheckin-1");
// NodeRef companyHome = getRepositoryContext().getCompanyHome();
File file=new File("C:/Test/Test.doc");
System.out.println("Inside tcheckin-2");
FileFolderService fileFolderService = new FileFolderServiceImpl();
System.out.println("Inside tcheckin-3");
FileInfo fileinfo = fileFolderService.create(root, file.getName(), ContentModel.TYPE_CONTENT);
System.out.println("Inside tcheckin-4");
NodeRef fileRef = fileinfo.getNodeRef();
System.out.println("Inside tcheckin-5");
InputStream contentStream=new BufferedInputStream(new FileInputStream(file), 1025);
ContentService contentService= new RoutingContentService();
ContentWriter writer = contentService.getWriter(fileRef, ContentModel.PROP_CONTENT, true);
writer.putContent(contentStream);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

####################################################################

And below is the error I am getting while executing..

##########################################################33
Error:
———-
10:54:01,245 WARN [remoting.rmi.RmiRegistryFactoryBean] Could not detect RMI registry - creating new one
10:54:04,945 WARN [alfresco.util.OpenOfficeConnectionTester] A connection to OpenOffice could not be established.
10:54:10,499 INFO [domain.schema.SchemaBootstrap] Schema managed by database dialect org.hibernate.dialect.MySQLInnoDBDialect.
10:54:11,157 INFO [domain.schema.SchemaBootstrap] No changes were made to the schema.
10:54:13,110 INFO [repo.admin.ConfigurationChecker] The Alfresco root data directory ('dir.root') is: C:\Alfresco\alf_data
10:54:13,182 INFO [admin.patch.PatchExecuter] Checking for patches to apply …
10:54:13,301 INFO [repo.module.ModuleServiceImpl] Found 0 module(s).
10:54:13,405 ERROR [alfresco.smb.protocol] Error accessing Win32 NetBIOS, check DLL is on the path
10:54:13,419 INFO [service.descriptor.DescriptorService] Alfresco JVM - v1.6.0_07-b06; maximum heap size 63.563MB
10:54:13,419 WARN [service.descriptor.DescriptorService] Alfresco JVM - WARNING - maximum heap size 63.563MB is less than recommended 512MB
10:54:13,419 INFO [service.descriptor.DescriptorService] Alfresco started (Community Network): Current version 2.1.0 (482) schema 64 - Installed version 2.1.0 (482) schema 64
Inside tcheckin-1
Inside tcheckin-2
Inside tcheckin-3
java.lang.NullPointerException
at org.alfresco.repo.model.filefolder.FileFolderServiceImpl.isFolder(FileFolderServiceImpl.java:255)
at org.alfresco.repo.model.filefolder.FileFolderServiceImpl.create(FileFolderServiceImpl.java:673)
at org.alfresco.sample.FirstJCRClient.tCheckin(FirstJCRClient.java:112)
at org.alfresco.sample.FirstJCRClient.main(FirstJCRClient.java:87)

##########################################################33
Its failing exactly at this line of my code "FileInfo fileinfo = fileFolderService.create(root, file.getName(), ContentModel.TYPE_CONTENT);".

Please if you can provide some suggestions here, will be of much appreciation!

Thanks
Amit.
22 REPLIES 22

sangireddi
Champ in-the-making
Champ in-the-making
Hi All
Unable to see files in alfresco ui is not coming in servlets? is an issue of passing ticket?

sangireddi
Champ in-the-making
Champ in-the-making
Hi All
because we r running that servlet inside alfresco but still i need if we run javaclient out side of the alfresco context then what we need to configure to avoid above problem?

ganesh_boil
Champ in-the-making
Champ in-the-making
Hi Sangireddy/Amith,
I'm also newbie to alfresco…. starting doing now…. can u post the full code which u r running sucessfuly to upload a file.

thanks.