Android Question JSch : null object reference

DRU

Member
Licensed User
Hi all,

I try this SFTP library with a very little piece of code.


I initialize like this in activity_create
B4X:
If FirstTime Then
        sftp1.Initialize("sftp1", "USER", "PASSWORD", "HOST", 22)
        sftp1.SetKnownHostsStore(File.DirInternal, "hosts.txt")
        End If


i send a command like this
B4X:
sftp1.GetCurrentPath

Callback
B4X:
Sub sftp1_CommandCompleted(Command As String, Success As Boolean, Reply As String)
    Log(Success)
    If Success Then
        ToastMessageShow(Command,False)
        ToastMessageShow(Reply,False)
        Else
        Log(LastException.Message)
            End If
End Sub

But i get this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.jcraft.jsch.ChannelSftp.pwd()' on a null object reference

My credentials are ok, i have tested them with a sftp client.

Have i idea ?
Thank's for your help
 

DRU

Member
Licensed User
Hi Erel,

Here is the log after serveral call of this sub. None of the method is working

B4X:
Sub BtsLstFiles_Click
    sftp1.List(EditText1.Text)
    sftp1.GetCurrentPath
End Sub

java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Vector com.jcraft.jsch.ChannelSftp.ls(java.lang.String)' on a null object reference
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.jcraft.jsch.ChannelSftp.pwd()' on a null object reference
false
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.jcraft.jsch.ChannelSftp.pwd()' on a null object reference
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.jcraft.jsch.ChannelSftp.pwd()' on a null object reference
false
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.jcraft.jsch.ChannelSftp.pwd()' on a null object reference
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.jcraft.jsch.ChannelSftp.pwd()' on a null object reference
false
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.jcraft.jsch.ChannelSftp.pwd()' on a null object reference
 
Upvote 0

DRU

Member
Licensed User
Here is the whole code, hope it will help


B4X:
#Region  Project Attributes
    #ApplicationLabel: PROTIME SMS Alert
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim sftp1 As SFtp
    Dim database As SQL
    Dim lstSms As List
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private BtsLstFiles As Button
    Private ListView1 As ListView
    Private EditText1 As EditText
    Private btnReadCSV As Button
    Private btnFlushData As Button
    Private btnCompare As Button
    Private btnSendSms As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:

    If FirstTime Then
      
        ' DATABASE
        DB.CreateDatabase
      
        ' SFTP
        sftp1.Initialize("sftp1", "XXXXX_SFTP", "7&", "server.domain.fr", 22)
        sftp1.SetKnownHostsStore(File.DirInternal, "hosts.txt")
      
        'AUTRES
        lstSms.Initialize
      
        End If
      
    Activity.LoadLayout("L1")
End Sub

Sub Activity_Resume
    sftp1.Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub sftp1_PromptYesNo (Message As String)'
    'sftp1.SetPromptResult(True)
    Dim res As Int = Msgbox2(Message, "", "Yes", "", "No", Null)
    'The next line might be a bit confusing. It is a condition.
    'The value will be True if res equals to DialogResponse.POSITIVE.
    sftp1.SetPromptResult(res = DialogResponse.POSITIVE)
End Sub

Sub sftp1_ShowMessage (Message As String)
    Msgbox(Message, "")
End Sub

Sub BtsLstFiles_Click
    sftp1.List(EditText1.Text)
    sftp1.GetCurrentPath
End Sub

Sub sftp1_CommandCompleted(Command As String, Success As Boolean, Reply As String)
    Log(Success)
    If Success Then
        'ToastMessageShow(Command,False)
        'ToastMessageShow(Reply,False)
        Else
        Log(LastException.Message)
        End If
End Sub

Sub sftp1_ListCompleted (ServerPath As String, Success As Boolean, Folders() As SFtpEntry, Files() As SFtpEntry)
    If Success=True Then
        ToastMessageShow("success !",False)
    Else
        'ToastMessageShow("Error : " & LastException.Message,True)
        Log(LastException.Message)
        End If
End Sub

Sub btnReadCSV_Click
    PTFILES.ReadCsvFile
    DB.UpdateDailyPatterns
End Sub

Sub btnFlushData_Click
    DB.flushdata
End Sub

Sub btnCompare_Click
    DB.CompareDailyPatterns
End Sub

Sub btnSendSms_Click
    DB.GetSmsToSend
End Sub
 
Last edited by a moderator:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've tested your project.

If you check the unfiltered logs you will see this link:
Unresolved exception class when finding catch block: org.ietf.jgss.GSSException

Something in your server configuration is not supported by Android OS. The internal library, which is not an Android library, tries to create an exception and fails as the exception type is not available in Android.

If I change your server parameters to a server of mine then it works properly.

I thought that we could get the actual error by running it in B4J. However it works properly (with your server) from B4J. I guess that it has something to do with the cipher protocol.
 
Upvote 0

DRU

Member
Licensed User
Thank you very much Erel, for the time you spent for me.
You provide me a very important information. I will try to get some technical detail about this. Hope i'll be able to share a solution with B4x community.
 
Upvote 0
Top