B4J Question make a class of shell ping

bogdanc

Active Member
Licensed User
Longtime User
When I use that code on main file works fine:

B4X:
Sub Ping(ipaddress As String)
Public shl As Shell
    Dim lstParams As List: lstParams.Initialize
    Try
   
        Dim os As String = GetSystemProperty("os.name", "").ToLowerCase
        If os.Contains("win") Then
            lstParams.Add("-n")
        Else
            lstParams.Add("-c")
        End If
        lstParams.Add("1")
        lstParams.Add(ipaddress)
        
        shl.Initialize("shl", "ping", lstParams)
        shl.WorkingDirectory = File.DirApp
        shl.Run(-1)
        Log("here in ping")
    Catch
           
        Log("ping error")
        Log(LastException.Message)
    End Try

   
End Sub



Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
If Success And ExitCode = 0 Then
   Log("Success")
   Log(StdOut)
  
   Dim str As String=StdOut

Dim len As Int=str.Length
Dim ind As Int=str.IndexOf("Average = ")

Dim ping_time As String =  str.SubString2((ind+10), len-4)

Log("takes from ping: " & str.SubString2( (ind+10), len-4) )  
 
Else
   Log("Error: " & StdErr)
End If
'ExitApplication
End Sub

when I want move to a separate class / module as:


B4X:
'Class module
Sub Class_Globals
    Private fx As JFX
End Sub

Public Sub Initialize
End Sub




Sub Ping(ipaddress As String)
Public shl As Shell
    Dim lstParams As List: lstParams.Initialize
    Try
   
        Dim os As String = GetSystemProperty("os.name", "").ToLowerCase
        If os.Contains("win") Then
            lstParams.Add("-n")
        Else
            lstParams.Add("-c")
        End If
        lstParams.Add("1")
        lstParams.Add(ipaddress)
        
        shl.Initialize("shl", "ping", lstParams)
        shl.WorkingDirectory = File.DirApp
        shl.Run(-1)
        Log("here in ping")
    Catch
           
        Log("ping error")
        Log(LastException.Message)
    End Try

   
End Sub



Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
If Success And ExitCode = 0 Then
   Log("Success")
   Log(StdOut)
  
   Dim str As String=StdOut

Dim len As Int=str.Length
Dim ind As Int=str.IndexOf("Average = ")

Dim ping_time As String =  str.SubString2((ind+10), len-4)

Log("takes from ping: " & str.SubString2( (ind+10), len-4) )  
 
Else
   Log("Error: " & StdErr)
End If
'ExitApplication
End Sub

getting error of:
java.lang.NullPointerException
at anywheresoftware.b4j.objects.Shell$1.onProcessFailed(Shell.java:146)
at org.apache.commons.exec.DefaultExecutor$1.run(DefaultExecutor.java:193)
 

bogdanc

Active Member
Licensed User
Longtime User
Hi Erel!

I just removed everything and I left only this ping part.
 

Attachments

  • ping_shell_test.zip
    342.6 KB · Views: 349
Upvote 0

Aristide

Member
B4X:
#Region  Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 400
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
   
    Public testping As pingServers
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
   
    testping.Initialize 'Is here
    testping.Ping("X.X.X.X")
   
   
End Sub
 
Upvote 0

Aristide

Member
You must choose between Class module and Code module. See "Project"menu, "Add new module", ....

For a code module, the code is very near. You don't need to create a private object in main, neither initialize it, your code is in a module code nammed pingServers, the "same" code ( except initialize reserved for class module). You call pingServers.Ping(....)

B4X:
#Region  Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 400
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
  
    'Public testping As pingServers
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
  
    pingServers.Ping("X.X.X.X")
      
End Sub
 
Upvote 0
Top