0

I am writting an android application to download a file from the network , but I keep getting the following error :

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:415) at jcifs.util.transport.Transport.sendrecv(Transport.java:70) at jcifs.smb.SmbTransport.send(SmbTransport.java:619) at jcifs.smb.SmbSession.send(SmbSession.java:240) at jcifs.smb.SmbTree.send(SmbTree.java:111) at jcifs.smb.SmbFile.send(SmbFile.java:721) at jcifs.smb.SmbFile.open0(SmbFile.java:926) at jcifs.smb.SmbFile.open(SmbFile.java:943) at jcifs.smb.SmbFileOutputStream.(SmbFileOutputStream.java:142) at jcifs.smb.SmbFileOutputStream.(SmbFileOutputStream.java:97) at jcifs.smb.SmbFileOutputStream.(SmbFileOutputStream.java:67) at za.co.ver_tex.itqueries.NetworkShareFileCopy.copyFileUsingJcifs(NetworkShareFileCopy.java:80) at za.co.ver_tex.itqueries.ViewQuery.ViewFile(ViewQuery.java:780) at za.co.ver_tex.itqueries.ViewQuery$4.onClick(ViewQuery.java:375) at android.view.View.performClick(View.java:4633) at android.view.View$PerformClick.run(View.java:19330) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5356) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) at dalvik.system.NativeStart.main(Native Method)

Here is my code :

public void copyFromNetwork(String NetworkFile, String DestinationFile) {
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("Domain",
                "User", "Password");
 SmbFile remoteFile;
        try {

remoteFile = new SmbFile("smb:" + NetworkFile, auth);

OutputStream os = new FileOutputStream(DestinationFile);

            InputStream is = null;
is = remoteFile.getInputStream();
 int bufferSize = 5096;

            byte[] b = new byte[bufferSize];
            int noOfBytes = 0;
            while ((noOfBytes = is.read(b)) != -1) {
                os.write(b, 0, noOfBytes);
            }
            os.close();
            is.close();
} catch (Exception ex) {
            Log.w("FileCopy", ex);
        }
    }
jumps4fun
  • 3,994
  • 10
  • 50
  • 96
WhiteWolfza
  • 225
  • 3
  • 7

2 Answers2

0

remoteFile is not created and thats the reason for your NullPointerException. First of all create the object for a class before using it:

SmbFile remoteFile;//<-- HERE
        try {

OutputStream os = new FileOutputStream(DestinationFile);

            InputStream is = null;
is = remoteFile.getInputStream();

NullPointer:

is = remoteFile.getInputStream();// Thrown here!

I dont know SmbFile, but you need something like this:

SmbFile remoteFile = new SmbFile();
nano_nano
  • 12,351
  • 8
  • 55
  • 83
0

Having had a very similar issue, I think it is very likely that the file path is not following the SMB URL requirements. If you debug, and the unc field of the SmbFile instance is null, my suspicions are confirmed. It can be solved by following the specifications in the source code documentation (right after the initial import statements). Here is a selection:

SMB URL Examples

smb://users-nyc;miallen:mypass@angus/tmp/
This URL references a share called tmp on the server angus as user miallen who's password is mypass.

smb://Administrator:P%40ss@msmith1/c/WINDOWS/Desktop/foo.txt
A relativly sophisticated example that references a file msmith1's desktop as user Administrator. Notice the '@' is URL encoded with the '%40' hexcode escape.

smb://angus/
This references only a server. The behavior of some methods is different in this context(e.g. you cannot delete a server) however as you might expect the list method will list the available shares on this server.

smb://myworkgroup/
This syntactically is identical to the above example. However if myworkgroup happends to be a workgroup(which is indeed suggested by the name) the list method will return a list of servers that have registered themselves as members of myworkgroup.

smb:// Just as smb://server/ lists shares and smb://workgroup/ lists servers, the smb:// URL lists all available workgroups on a netbios LAN. Again, in this context many methods are not valid and return default values(e.g. isHidden will always return false).

smb://angus.foo.net/d/jcifs/pipes.doc
The server name may also be a DNS name as it is in this example. See Setting Name Resolution Properties for details.

smb://192.168.1.15/ADMIN$/
The server name may also be an IP address. See Setting Name Resolution Properties for details.

smb://domain;username:password@server/share/path/to/file.txt
A prototypical example that uses all the fields.

smb://myworkgroup/angus/ <-- ILLEGAL
Despite the hierarchial relationship between workgroups, servers, and filesystems this example is not valid.

smb://server/share/path/to/dir <-- ILLEGAL
URLs that represent workgroups, servers, shares, or directories require a trailing slash '/'.

smb://MYGROUP/?SERVER=192.168.10.15
SMB URLs support some query string parameters. In this example the SERVER parameter is used to override the server name service lookup to contact the server 192.168.10.15 (presumably known to be a master browser) for the server list in workgroup MYGROUP.

jumps4fun
  • 3,994
  • 10
  • 50
  • 96