Getting NullPointerException with SmbFileInputStream












0















Trying to modify excel which is in another machine. Passing IP address, username, password and file path to access and modify file, but getting NullPointerException at new SmbFileInputStream(sFile). What is the reason for this?



NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domin", "username", "password");
String path = "smb:\\<IPaddress>\C$\<FolderName>\File%20-%20Input.xlsx";
SmbFile sFile = new SmbFile(path, auth);



try {
SmbFileInputStream inputStream = new SmbFileInputStream(sFile);
Workbook workbook = WorkbookFactory.create(inputStream);

Sheet sheet = workbook.getSheetAt(0);
int rowCount = sheet.getLastRowNum(),i=0;
Cell cell;
for(ForemostReservedDataDO obj : unsavedRecords){
i++;
Row row = sheet.createRow(rowCount+i);
cell = row.createCell(0);
cell.setCellValue(obj.getPolicyNum());
cell = row.createCell(1);
cell.setCellValue("Recreational Value");
}
inputStream.close();

SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
workbook.write(sfos);
workbook.close();
sfos.close();

} catch (EncryptedDocumentException | InvalidFormatException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


Full Stack



SEVERE: Servlet.service() for servlet [spring-dispatcher] in context with path [/Foremost] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at jcifs.smb.ServerMessageBlock.writeString(ServerMessageBlock.java:213)
at jcifs.smb.ServerMessageBlock.writeString(ServerMessageBlock.java:202)
at jcifs.smb.SmbComNTCreateAndX.writeBytesWireFormat(SmbComNTCreateAndX.java:170)
at jcifs.smb.AndXServerMessageBlock.writeAndXWireFormat(AndXServerMessageBlock.java:101)
at jcifs.smb.AndXServerMessageBlock.encode(AndXServerMessageBlock.java:65)
at jcifs.smb.SmbTransport.doSend(SmbTransport.java:439)
at jcifs.util.transport.Transport.sendrecv(Transport.java:67)
at jcifs.smb.SmbTransport.send(SmbTransport.java:655)
at jcifs.smb.SmbSession.send(SmbSession.java:238)
at jcifs.smb.SmbTree.send(SmbTree.java:119)
at jcifs.smb.SmbFile.send(SmbFile.java:775)
at jcifs.smb.SmbFile.open0(SmbFile.java:989)
at jcifs.smb.SmbFile.open(SmbFile.java:1006)
at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:73)
at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:65)
at com.Foremost.Controllers.DataDownController.saveReservedData(DataDownController.java:217)









share|improve this question

























  • Please add the full Stacktrace of the NPE to your question.

    – Florian Albrecht
    Nov 16 '18 at 10:26











  • Thanks for the Stacktrace. Which version of JCIFS are you using?

    – Florian Albrecht
    Nov 16 '18 at 10:34











  • jcifs version is 1.3.17

    – Srikanth
    Nov 16 '18 at 10:38











  • Thanks (you should add that to the question). That is a very old version, you should upgrade to latest version: mvnrepository.com/artifact/org.codelibs/jcifs/2.1.3 (note: new groupId)

    – Florian Albrecht
    Nov 16 '18 at 10:40











  • Apology, next time definitely will mention version. Can you provide me any sample code with latest version (2.1.3) as per my requirement. which i written that's getting compile errors with latest jar (because those are deprecated methods)

    – Srikanth
    Nov 16 '18 at 10:45
















0















Trying to modify excel which is in another machine. Passing IP address, username, password and file path to access and modify file, but getting NullPointerException at new SmbFileInputStream(sFile). What is the reason for this?



NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domin", "username", "password");
String path = "smb:\\<IPaddress>\C$\<FolderName>\File%20-%20Input.xlsx";
SmbFile sFile = new SmbFile(path, auth);



try {
SmbFileInputStream inputStream = new SmbFileInputStream(sFile);
Workbook workbook = WorkbookFactory.create(inputStream);

Sheet sheet = workbook.getSheetAt(0);
int rowCount = sheet.getLastRowNum(),i=0;
Cell cell;
for(ForemostReservedDataDO obj : unsavedRecords){
i++;
Row row = sheet.createRow(rowCount+i);
cell = row.createCell(0);
cell.setCellValue(obj.getPolicyNum());
cell = row.createCell(1);
cell.setCellValue("Recreational Value");
}
inputStream.close();

SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
workbook.write(sfos);
workbook.close();
sfos.close();

} catch (EncryptedDocumentException | InvalidFormatException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


Full Stack



SEVERE: Servlet.service() for servlet [spring-dispatcher] in context with path [/Foremost] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at jcifs.smb.ServerMessageBlock.writeString(ServerMessageBlock.java:213)
at jcifs.smb.ServerMessageBlock.writeString(ServerMessageBlock.java:202)
at jcifs.smb.SmbComNTCreateAndX.writeBytesWireFormat(SmbComNTCreateAndX.java:170)
at jcifs.smb.AndXServerMessageBlock.writeAndXWireFormat(AndXServerMessageBlock.java:101)
at jcifs.smb.AndXServerMessageBlock.encode(AndXServerMessageBlock.java:65)
at jcifs.smb.SmbTransport.doSend(SmbTransport.java:439)
at jcifs.util.transport.Transport.sendrecv(Transport.java:67)
at jcifs.smb.SmbTransport.send(SmbTransport.java:655)
at jcifs.smb.SmbSession.send(SmbSession.java:238)
at jcifs.smb.SmbTree.send(SmbTree.java:119)
at jcifs.smb.SmbFile.send(SmbFile.java:775)
at jcifs.smb.SmbFile.open0(SmbFile.java:989)
at jcifs.smb.SmbFile.open(SmbFile.java:1006)
at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:73)
at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:65)
at com.Foremost.Controllers.DataDownController.saveReservedData(DataDownController.java:217)









share|improve this question

























  • Please add the full Stacktrace of the NPE to your question.

    – Florian Albrecht
    Nov 16 '18 at 10:26











  • Thanks for the Stacktrace. Which version of JCIFS are you using?

    – Florian Albrecht
    Nov 16 '18 at 10:34











  • jcifs version is 1.3.17

    – Srikanth
    Nov 16 '18 at 10:38











  • Thanks (you should add that to the question). That is a very old version, you should upgrade to latest version: mvnrepository.com/artifact/org.codelibs/jcifs/2.1.3 (note: new groupId)

    – Florian Albrecht
    Nov 16 '18 at 10:40











  • Apology, next time definitely will mention version. Can you provide me any sample code with latest version (2.1.3) as per my requirement. which i written that's getting compile errors with latest jar (because those are deprecated methods)

    – Srikanth
    Nov 16 '18 at 10:45














0












0








0








Trying to modify excel which is in another machine. Passing IP address, username, password and file path to access and modify file, but getting NullPointerException at new SmbFileInputStream(sFile). What is the reason for this?



NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domin", "username", "password");
String path = "smb:\\<IPaddress>\C$\<FolderName>\File%20-%20Input.xlsx";
SmbFile sFile = new SmbFile(path, auth);



try {
SmbFileInputStream inputStream = new SmbFileInputStream(sFile);
Workbook workbook = WorkbookFactory.create(inputStream);

Sheet sheet = workbook.getSheetAt(0);
int rowCount = sheet.getLastRowNum(),i=0;
Cell cell;
for(ForemostReservedDataDO obj : unsavedRecords){
i++;
Row row = sheet.createRow(rowCount+i);
cell = row.createCell(0);
cell.setCellValue(obj.getPolicyNum());
cell = row.createCell(1);
cell.setCellValue("Recreational Value");
}
inputStream.close();

SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
workbook.write(sfos);
workbook.close();
sfos.close();

} catch (EncryptedDocumentException | InvalidFormatException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


Full Stack



SEVERE: Servlet.service() for servlet [spring-dispatcher] in context with path [/Foremost] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at jcifs.smb.ServerMessageBlock.writeString(ServerMessageBlock.java:213)
at jcifs.smb.ServerMessageBlock.writeString(ServerMessageBlock.java:202)
at jcifs.smb.SmbComNTCreateAndX.writeBytesWireFormat(SmbComNTCreateAndX.java:170)
at jcifs.smb.AndXServerMessageBlock.writeAndXWireFormat(AndXServerMessageBlock.java:101)
at jcifs.smb.AndXServerMessageBlock.encode(AndXServerMessageBlock.java:65)
at jcifs.smb.SmbTransport.doSend(SmbTransport.java:439)
at jcifs.util.transport.Transport.sendrecv(Transport.java:67)
at jcifs.smb.SmbTransport.send(SmbTransport.java:655)
at jcifs.smb.SmbSession.send(SmbSession.java:238)
at jcifs.smb.SmbTree.send(SmbTree.java:119)
at jcifs.smb.SmbFile.send(SmbFile.java:775)
at jcifs.smb.SmbFile.open0(SmbFile.java:989)
at jcifs.smb.SmbFile.open(SmbFile.java:1006)
at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:73)
at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:65)
at com.Foremost.Controllers.DataDownController.saveReservedData(DataDownController.java:217)









share|improve this question
















Trying to modify excel which is in another machine. Passing IP address, username, password and file path to access and modify file, but getting NullPointerException at new SmbFileInputStream(sFile). What is the reason for this?



NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domin", "username", "password");
String path = "smb:\\<IPaddress>\C$\<FolderName>\File%20-%20Input.xlsx";
SmbFile sFile = new SmbFile(path, auth);



try {
SmbFileInputStream inputStream = new SmbFileInputStream(sFile);
Workbook workbook = WorkbookFactory.create(inputStream);

Sheet sheet = workbook.getSheetAt(0);
int rowCount = sheet.getLastRowNum(),i=0;
Cell cell;
for(ForemostReservedDataDO obj : unsavedRecords){
i++;
Row row = sheet.createRow(rowCount+i);
cell = row.createCell(0);
cell.setCellValue(obj.getPolicyNum());
cell = row.createCell(1);
cell.setCellValue("Recreational Value");
}
inputStream.close();

SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
workbook.write(sfos);
workbook.close();
sfos.close();

} catch (EncryptedDocumentException | InvalidFormatException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


Full Stack



SEVERE: Servlet.service() for servlet [spring-dispatcher] in context with path [/Foremost] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at jcifs.smb.ServerMessageBlock.writeString(ServerMessageBlock.java:213)
at jcifs.smb.ServerMessageBlock.writeString(ServerMessageBlock.java:202)
at jcifs.smb.SmbComNTCreateAndX.writeBytesWireFormat(SmbComNTCreateAndX.java:170)
at jcifs.smb.AndXServerMessageBlock.writeAndXWireFormat(AndXServerMessageBlock.java:101)
at jcifs.smb.AndXServerMessageBlock.encode(AndXServerMessageBlock.java:65)
at jcifs.smb.SmbTransport.doSend(SmbTransport.java:439)
at jcifs.util.transport.Transport.sendrecv(Transport.java:67)
at jcifs.smb.SmbTransport.send(SmbTransport.java:655)
at jcifs.smb.SmbSession.send(SmbSession.java:238)
at jcifs.smb.SmbTree.send(SmbTree.java:119)
at jcifs.smb.SmbFile.send(SmbFile.java:775)
at jcifs.smb.SmbFile.open0(SmbFile.java:989)
at jcifs.smb.SmbFile.open(SmbFile.java:1006)
at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:73)
at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:65)
at com.Foremost.Controllers.DataDownController.saveReservedData(DataDownController.java:217)






java nullpointerexception jcifs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 13:22









Florian Albrecht

1,7351419




1,7351419










asked Nov 16 '18 at 10:24









SrikanthSrikanth

732212




732212













  • Please add the full Stacktrace of the NPE to your question.

    – Florian Albrecht
    Nov 16 '18 at 10:26











  • Thanks for the Stacktrace. Which version of JCIFS are you using?

    – Florian Albrecht
    Nov 16 '18 at 10:34











  • jcifs version is 1.3.17

    – Srikanth
    Nov 16 '18 at 10:38











  • Thanks (you should add that to the question). That is a very old version, you should upgrade to latest version: mvnrepository.com/artifact/org.codelibs/jcifs/2.1.3 (note: new groupId)

    – Florian Albrecht
    Nov 16 '18 at 10:40











  • Apology, next time definitely will mention version. Can you provide me any sample code with latest version (2.1.3) as per my requirement. which i written that's getting compile errors with latest jar (because those are deprecated methods)

    – Srikanth
    Nov 16 '18 at 10:45



















  • Please add the full Stacktrace of the NPE to your question.

    – Florian Albrecht
    Nov 16 '18 at 10:26











  • Thanks for the Stacktrace. Which version of JCIFS are you using?

    – Florian Albrecht
    Nov 16 '18 at 10:34











  • jcifs version is 1.3.17

    – Srikanth
    Nov 16 '18 at 10:38











  • Thanks (you should add that to the question). That is a very old version, you should upgrade to latest version: mvnrepository.com/artifact/org.codelibs/jcifs/2.1.3 (note: new groupId)

    – Florian Albrecht
    Nov 16 '18 at 10:40











  • Apology, next time definitely will mention version. Can you provide me any sample code with latest version (2.1.3) as per my requirement. which i written that's getting compile errors with latest jar (because those are deprecated methods)

    – Srikanth
    Nov 16 '18 at 10:45

















Please add the full Stacktrace of the NPE to your question.

– Florian Albrecht
Nov 16 '18 at 10:26





Please add the full Stacktrace of the NPE to your question.

– Florian Albrecht
Nov 16 '18 at 10:26













Thanks for the Stacktrace. Which version of JCIFS are you using?

– Florian Albrecht
Nov 16 '18 at 10:34





Thanks for the Stacktrace. Which version of JCIFS are you using?

– Florian Albrecht
Nov 16 '18 at 10:34













jcifs version is 1.3.17

– Srikanth
Nov 16 '18 at 10:38





jcifs version is 1.3.17

– Srikanth
Nov 16 '18 at 10:38













Thanks (you should add that to the question). That is a very old version, you should upgrade to latest version: mvnrepository.com/artifact/org.codelibs/jcifs/2.1.3 (note: new groupId)

– Florian Albrecht
Nov 16 '18 at 10:40





Thanks (you should add that to the question). That is a very old version, you should upgrade to latest version: mvnrepository.com/artifact/org.codelibs/jcifs/2.1.3 (note: new groupId)

– Florian Albrecht
Nov 16 '18 at 10:40













Apology, next time definitely will mention version. Can you provide me any sample code with latest version (2.1.3) as per my requirement. which i written that's getting compile errors with latest jar (because those are deprecated methods)

– Srikanth
Nov 16 '18 at 10:45





Apology, next time definitely will mention version. Can you provide me any sample code with latest version (2.1.3) as per my requirement. which i written that's getting compile errors with latest jar (because those are deprecated methods)

– Srikanth
Nov 16 '18 at 10:45












2 Answers
2






active

oldest

votes


















1














Your JCIFS version seems to be outdated and not compatible with the remote system. Upgrade to latest JCIFS (current version: 2.1.3, https://github.com/codelibs/jcifs) or to jcifs-ng (https://github.com/AgNO3/jcifs-ng), from which the linked JCIFS is now a fork of.



Here is some example code on how to use jcifs-ng for reading a file via SMB:



String fileUrl = "smb://netserver/some/path/to/file.xls";

Properties cifsProps = new Properties();
cifsProps.setProperty("jcifs.smb.client.domain", "my.domain.int");
cifsProps.setProperty("jcifs.smb.client.username", USER_NAME);
cifsProps.setProperty("jcifs.smb.client.password", PASSWORD);

Configuration config = new PropertyConfiguration(cifsProps);
BaseContext context = new BaseContext(config);
SmbResource resource = context.get(fileUrl);

if (!(resource instanceof SmbFile)) {
throw new CIFSException("File URL does not point to a file on a network share");
}

try (InputStream in = ((SmbFile) resource).getInputStream()) {
// TODO read from in
} finally {
context.close();
}


For writing a file, well, I think you will be able to figure that out :-)






share|improve this answer


























  • great, thanks @Florian. have small problem, mine is jdk1.7 I need JDK 1.8 for JCIFS 2.1.3. i'm trying to upgrade my project. anyway thank you very much. once i done i will give voting for your answer.

    – Srikanth
    Nov 16 '18 at 11:04











  • I think they removed 1.7 support just recently. Perhaps you could e.g. use jcifs-ng 2.0.5, that should work fine with 1.7 and still be recent enough.

    – Florian Albrecht
    Nov 16 '18 at 11:55











  • Yes i tried with jcifs-ng 2.0.5 getting jcifs.smb.SmbAuthException: Access is denied. but with the same user i'm able open and modify directly. What would be the wrong ?

    – Srikanth
    Nov 19 '18 at 8:31











  • getting access denied, create new question. stackoverflow.com/questions/53387310/…

    – Srikanth
    Nov 20 '18 at 6:20













  • Have one doubt, is it mandatory to share the folder which i want to access? can not we connect with out share?

    – Srikanth
    Nov 20 '18 at 9:39



















0














Most probably, the problem is that your sFile object is null.



Check the path of file you are providing.






share|improve this answer
























  • Well, that also was my first thought, but as sFile = new SmbFile(...), if there is no crucial line of code removed, it cannot be null if that line succeeds.

    – Florian Albrecht
    Nov 16 '18 at 10:28











  • No, if the path is incorrect that in that case there is a possibility that sFile will be null. Can you put a debugger and check whether sFile is null or not?

    – codeLover
    Nov 16 '18 at 10:30











  • I am not the OP. And no, sFile can not be null if it is initialized with a new SmbFile(...). No Java reference can ever be null after initialized with a new instance of a class, unless the constructor call fails with an exception (which would then cause an exception on another line than the OP reported).

    – Florian Albrecht
    Nov 16 '18 at 10:32











  • sFile is not null checked in debug.

    – Srikanth
    Nov 16 '18 at 10:33












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335868%2fgetting-nullpointerexception-with-smbfileinputstream%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Your JCIFS version seems to be outdated and not compatible with the remote system. Upgrade to latest JCIFS (current version: 2.1.3, https://github.com/codelibs/jcifs) or to jcifs-ng (https://github.com/AgNO3/jcifs-ng), from which the linked JCIFS is now a fork of.



Here is some example code on how to use jcifs-ng for reading a file via SMB:



String fileUrl = "smb://netserver/some/path/to/file.xls";

Properties cifsProps = new Properties();
cifsProps.setProperty("jcifs.smb.client.domain", "my.domain.int");
cifsProps.setProperty("jcifs.smb.client.username", USER_NAME);
cifsProps.setProperty("jcifs.smb.client.password", PASSWORD);

Configuration config = new PropertyConfiguration(cifsProps);
BaseContext context = new BaseContext(config);
SmbResource resource = context.get(fileUrl);

if (!(resource instanceof SmbFile)) {
throw new CIFSException("File URL does not point to a file on a network share");
}

try (InputStream in = ((SmbFile) resource).getInputStream()) {
// TODO read from in
} finally {
context.close();
}


For writing a file, well, I think you will be able to figure that out :-)






share|improve this answer


























  • great, thanks @Florian. have small problem, mine is jdk1.7 I need JDK 1.8 for JCIFS 2.1.3. i'm trying to upgrade my project. anyway thank you very much. once i done i will give voting for your answer.

    – Srikanth
    Nov 16 '18 at 11:04











  • I think they removed 1.7 support just recently. Perhaps you could e.g. use jcifs-ng 2.0.5, that should work fine with 1.7 and still be recent enough.

    – Florian Albrecht
    Nov 16 '18 at 11:55











  • Yes i tried with jcifs-ng 2.0.5 getting jcifs.smb.SmbAuthException: Access is denied. but with the same user i'm able open and modify directly. What would be the wrong ?

    – Srikanth
    Nov 19 '18 at 8:31











  • getting access denied, create new question. stackoverflow.com/questions/53387310/…

    – Srikanth
    Nov 20 '18 at 6:20













  • Have one doubt, is it mandatory to share the folder which i want to access? can not we connect with out share?

    – Srikanth
    Nov 20 '18 at 9:39
















1














Your JCIFS version seems to be outdated and not compatible with the remote system. Upgrade to latest JCIFS (current version: 2.1.3, https://github.com/codelibs/jcifs) or to jcifs-ng (https://github.com/AgNO3/jcifs-ng), from which the linked JCIFS is now a fork of.



Here is some example code on how to use jcifs-ng for reading a file via SMB:



String fileUrl = "smb://netserver/some/path/to/file.xls";

Properties cifsProps = new Properties();
cifsProps.setProperty("jcifs.smb.client.domain", "my.domain.int");
cifsProps.setProperty("jcifs.smb.client.username", USER_NAME);
cifsProps.setProperty("jcifs.smb.client.password", PASSWORD);

Configuration config = new PropertyConfiguration(cifsProps);
BaseContext context = new BaseContext(config);
SmbResource resource = context.get(fileUrl);

if (!(resource instanceof SmbFile)) {
throw new CIFSException("File URL does not point to a file on a network share");
}

try (InputStream in = ((SmbFile) resource).getInputStream()) {
// TODO read from in
} finally {
context.close();
}


For writing a file, well, I think you will be able to figure that out :-)






share|improve this answer


























  • great, thanks @Florian. have small problem, mine is jdk1.7 I need JDK 1.8 for JCIFS 2.1.3. i'm trying to upgrade my project. anyway thank you very much. once i done i will give voting for your answer.

    – Srikanth
    Nov 16 '18 at 11:04











  • I think they removed 1.7 support just recently. Perhaps you could e.g. use jcifs-ng 2.0.5, that should work fine with 1.7 and still be recent enough.

    – Florian Albrecht
    Nov 16 '18 at 11:55











  • Yes i tried with jcifs-ng 2.0.5 getting jcifs.smb.SmbAuthException: Access is denied. but with the same user i'm able open and modify directly. What would be the wrong ?

    – Srikanth
    Nov 19 '18 at 8:31











  • getting access denied, create new question. stackoverflow.com/questions/53387310/…

    – Srikanth
    Nov 20 '18 at 6:20













  • Have one doubt, is it mandatory to share the folder which i want to access? can not we connect with out share?

    – Srikanth
    Nov 20 '18 at 9:39














1












1








1







Your JCIFS version seems to be outdated and not compatible with the remote system. Upgrade to latest JCIFS (current version: 2.1.3, https://github.com/codelibs/jcifs) or to jcifs-ng (https://github.com/AgNO3/jcifs-ng), from which the linked JCIFS is now a fork of.



Here is some example code on how to use jcifs-ng for reading a file via SMB:



String fileUrl = "smb://netserver/some/path/to/file.xls";

Properties cifsProps = new Properties();
cifsProps.setProperty("jcifs.smb.client.domain", "my.domain.int");
cifsProps.setProperty("jcifs.smb.client.username", USER_NAME);
cifsProps.setProperty("jcifs.smb.client.password", PASSWORD);

Configuration config = new PropertyConfiguration(cifsProps);
BaseContext context = new BaseContext(config);
SmbResource resource = context.get(fileUrl);

if (!(resource instanceof SmbFile)) {
throw new CIFSException("File URL does not point to a file on a network share");
}

try (InputStream in = ((SmbFile) resource).getInputStream()) {
// TODO read from in
} finally {
context.close();
}


For writing a file, well, I think you will be able to figure that out :-)






share|improve this answer















Your JCIFS version seems to be outdated and not compatible with the remote system. Upgrade to latest JCIFS (current version: 2.1.3, https://github.com/codelibs/jcifs) or to jcifs-ng (https://github.com/AgNO3/jcifs-ng), from which the linked JCIFS is now a fork of.



Here is some example code on how to use jcifs-ng for reading a file via SMB:



String fileUrl = "smb://netserver/some/path/to/file.xls";

Properties cifsProps = new Properties();
cifsProps.setProperty("jcifs.smb.client.domain", "my.domain.int");
cifsProps.setProperty("jcifs.smb.client.username", USER_NAME);
cifsProps.setProperty("jcifs.smb.client.password", PASSWORD);

Configuration config = new PropertyConfiguration(cifsProps);
BaseContext context = new BaseContext(config);
SmbResource resource = context.get(fileUrl);

if (!(resource instanceof SmbFile)) {
throw new CIFSException("File URL does not point to a file on a network share");
}

try (InputStream in = ((SmbFile) resource).getInputStream()) {
// TODO read from in
} finally {
context.close();
}


For writing a file, well, I think you will be able to figure that out :-)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 10:50

























answered Nov 16 '18 at 10:43









Florian AlbrechtFlorian Albrecht

1,7351419




1,7351419













  • great, thanks @Florian. have small problem, mine is jdk1.7 I need JDK 1.8 for JCIFS 2.1.3. i'm trying to upgrade my project. anyway thank you very much. once i done i will give voting for your answer.

    – Srikanth
    Nov 16 '18 at 11:04











  • I think they removed 1.7 support just recently. Perhaps you could e.g. use jcifs-ng 2.0.5, that should work fine with 1.7 and still be recent enough.

    – Florian Albrecht
    Nov 16 '18 at 11:55











  • Yes i tried with jcifs-ng 2.0.5 getting jcifs.smb.SmbAuthException: Access is denied. but with the same user i'm able open and modify directly. What would be the wrong ?

    – Srikanth
    Nov 19 '18 at 8:31











  • getting access denied, create new question. stackoverflow.com/questions/53387310/…

    – Srikanth
    Nov 20 '18 at 6:20













  • Have one doubt, is it mandatory to share the folder which i want to access? can not we connect with out share?

    – Srikanth
    Nov 20 '18 at 9:39



















  • great, thanks @Florian. have small problem, mine is jdk1.7 I need JDK 1.8 for JCIFS 2.1.3. i'm trying to upgrade my project. anyway thank you very much. once i done i will give voting for your answer.

    – Srikanth
    Nov 16 '18 at 11:04











  • I think they removed 1.7 support just recently. Perhaps you could e.g. use jcifs-ng 2.0.5, that should work fine with 1.7 and still be recent enough.

    – Florian Albrecht
    Nov 16 '18 at 11:55











  • Yes i tried with jcifs-ng 2.0.5 getting jcifs.smb.SmbAuthException: Access is denied. but with the same user i'm able open and modify directly. What would be the wrong ?

    – Srikanth
    Nov 19 '18 at 8:31











  • getting access denied, create new question. stackoverflow.com/questions/53387310/…

    – Srikanth
    Nov 20 '18 at 6:20













  • Have one doubt, is it mandatory to share the folder which i want to access? can not we connect with out share?

    – Srikanth
    Nov 20 '18 at 9:39

















great, thanks @Florian. have small problem, mine is jdk1.7 I need JDK 1.8 for JCIFS 2.1.3. i'm trying to upgrade my project. anyway thank you very much. once i done i will give voting for your answer.

– Srikanth
Nov 16 '18 at 11:04





great, thanks @Florian. have small problem, mine is jdk1.7 I need JDK 1.8 for JCIFS 2.1.3. i'm trying to upgrade my project. anyway thank you very much. once i done i will give voting for your answer.

– Srikanth
Nov 16 '18 at 11:04













I think they removed 1.7 support just recently. Perhaps you could e.g. use jcifs-ng 2.0.5, that should work fine with 1.7 and still be recent enough.

– Florian Albrecht
Nov 16 '18 at 11:55





I think they removed 1.7 support just recently. Perhaps you could e.g. use jcifs-ng 2.0.5, that should work fine with 1.7 and still be recent enough.

– Florian Albrecht
Nov 16 '18 at 11:55













Yes i tried with jcifs-ng 2.0.5 getting jcifs.smb.SmbAuthException: Access is denied. but with the same user i'm able open and modify directly. What would be the wrong ?

– Srikanth
Nov 19 '18 at 8:31





Yes i tried with jcifs-ng 2.0.5 getting jcifs.smb.SmbAuthException: Access is denied. but with the same user i'm able open and modify directly. What would be the wrong ?

– Srikanth
Nov 19 '18 at 8:31













getting access denied, create new question. stackoverflow.com/questions/53387310/…

– Srikanth
Nov 20 '18 at 6:20







getting access denied, create new question. stackoverflow.com/questions/53387310/…

– Srikanth
Nov 20 '18 at 6:20















Have one doubt, is it mandatory to share the folder which i want to access? can not we connect with out share?

– Srikanth
Nov 20 '18 at 9:39





Have one doubt, is it mandatory to share the folder which i want to access? can not we connect with out share?

– Srikanth
Nov 20 '18 at 9:39













0














Most probably, the problem is that your sFile object is null.



Check the path of file you are providing.






share|improve this answer
























  • Well, that also was my first thought, but as sFile = new SmbFile(...), if there is no crucial line of code removed, it cannot be null if that line succeeds.

    – Florian Albrecht
    Nov 16 '18 at 10:28











  • No, if the path is incorrect that in that case there is a possibility that sFile will be null. Can you put a debugger and check whether sFile is null or not?

    – codeLover
    Nov 16 '18 at 10:30











  • I am not the OP. And no, sFile can not be null if it is initialized with a new SmbFile(...). No Java reference can ever be null after initialized with a new instance of a class, unless the constructor call fails with an exception (which would then cause an exception on another line than the OP reported).

    – Florian Albrecht
    Nov 16 '18 at 10:32











  • sFile is not null checked in debug.

    – Srikanth
    Nov 16 '18 at 10:33
















0














Most probably, the problem is that your sFile object is null.



Check the path of file you are providing.






share|improve this answer
























  • Well, that also was my first thought, but as sFile = new SmbFile(...), if there is no crucial line of code removed, it cannot be null if that line succeeds.

    – Florian Albrecht
    Nov 16 '18 at 10:28











  • No, if the path is incorrect that in that case there is a possibility that sFile will be null. Can you put a debugger and check whether sFile is null or not?

    – codeLover
    Nov 16 '18 at 10:30











  • I am not the OP. And no, sFile can not be null if it is initialized with a new SmbFile(...). No Java reference can ever be null after initialized with a new instance of a class, unless the constructor call fails with an exception (which would then cause an exception on another line than the OP reported).

    – Florian Albrecht
    Nov 16 '18 at 10:32











  • sFile is not null checked in debug.

    – Srikanth
    Nov 16 '18 at 10:33














0












0








0







Most probably, the problem is that your sFile object is null.



Check the path of file you are providing.






share|improve this answer













Most probably, the problem is that your sFile object is null.



Check the path of file you are providing.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 10:27









codeLovercodeLover

2,2551620




2,2551620













  • Well, that also was my first thought, but as sFile = new SmbFile(...), if there is no crucial line of code removed, it cannot be null if that line succeeds.

    – Florian Albrecht
    Nov 16 '18 at 10:28











  • No, if the path is incorrect that in that case there is a possibility that sFile will be null. Can you put a debugger and check whether sFile is null or not?

    – codeLover
    Nov 16 '18 at 10:30











  • I am not the OP. And no, sFile can not be null if it is initialized with a new SmbFile(...). No Java reference can ever be null after initialized with a new instance of a class, unless the constructor call fails with an exception (which would then cause an exception on another line than the OP reported).

    – Florian Albrecht
    Nov 16 '18 at 10:32











  • sFile is not null checked in debug.

    – Srikanth
    Nov 16 '18 at 10:33



















  • Well, that also was my first thought, but as sFile = new SmbFile(...), if there is no crucial line of code removed, it cannot be null if that line succeeds.

    – Florian Albrecht
    Nov 16 '18 at 10:28











  • No, if the path is incorrect that in that case there is a possibility that sFile will be null. Can you put a debugger and check whether sFile is null or not?

    – codeLover
    Nov 16 '18 at 10:30











  • I am not the OP. And no, sFile can not be null if it is initialized with a new SmbFile(...). No Java reference can ever be null after initialized with a new instance of a class, unless the constructor call fails with an exception (which would then cause an exception on another line than the OP reported).

    – Florian Albrecht
    Nov 16 '18 at 10:32











  • sFile is not null checked in debug.

    – Srikanth
    Nov 16 '18 at 10:33

















Well, that also was my first thought, but as sFile = new SmbFile(...), if there is no crucial line of code removed, it cannot be null if that line succeeds.

– Florian Albrecht
Nov 16 '18 at 10:28





Well, that also was my first thought, but as sFile = new SmbFile(...), if there is no crucial line of code removed, it cannot be null if that line succeeds.

– Florian Albrecht
Nov 16 '18 at 10:28













No, if the path is incorrect that in that case there is a possibility that sFile will be null. Can you put a debugger and check whether sFile is null or not?

– codeLover
Nov 16 '18 at 10:30





No, if the path is incorrect that in that case there is a possibility that sFile will be null. Can you put a debugger and check whether sFile is null or not?

– codeLover
Nov 16 '18 at 10:30













I am not the OP. And no, sFile can not be null if it is initialized with a new SmbFile(...). No Java reference can ever be null after initialized with a new instance of a class, unless the constructor call fails with an exception (which would then cause an exception on another line than the OP reported).

– Florian Albrecht
Nov 16 '18 at 10:32





I am not the OP. And no, sFile can not be null if it is initialized with a new SmbFile(...). No Java reference can ever be null after initialized with a new instance of a class, unless the constructor call fails with an exception (which would then cause an exception on another line than the OP reported).

– Florian Albrecht
Nov 16 '18 at 10:32













sFile is not null checked in debug.

– Srikanth
Nov 16 '18 at 10:33





sFile is not null checked in debug.

– Srikanth
Nov 16 '18 at 10:33


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335868%2fgetting-nullpointerexception-with-smbfileinputstream%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine