Android Question JSON PARSER

Infotech

Member
Licensed User
My json i null pointer why?



B4X:
[{"no":1,"kodetransaksi":"1211121","namabrg":"HP 115","qty":1,"hrgajual":250000,"diskon":0,"total":250000}]
Error occurred on line: 113 (InputTemplate)
java.lang.NullPointerException: expected receiver of type anywheresoftware.b4a.objects.collections.JSONParser, but got null
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:755)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:345)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.BA$2.run(BA.java:338)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:149)
    at android.app.ActivityThread.main(ActivityThread.java:5061)
    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:794)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
    at dalvik.system.NativeStart.main(Native Method)
 

Infotech

Member
Licensed User
This is code on my class module

B4X:
    Else If job.Success And gg = 2 Then
        If job.GetString = "[]" Then
            no = "1"
        Else
            Dim respons As String = job.GetString
            Log(respons)
            parser.Initialize(respons)'line 113
            rows = parser.NextArray
            For i = 0 To rows.Size - 1
                Log("Rows i #" & i)
                Dim m As Map
                m = rows.Get(0)
                no = m.get("no")
            Next
        End If
    End If
 
Upvote 0

Infotech

Member
Licensed User
thank you reonell, but didn't work

B4X:
Sub JobDone (job As HttpJob)
    If job.Success And gg = 0 Then
        Dim parser As JSONParser
        Dim response As String = job.GetString
        parser.Initialize(response)
        rows = parser.NextArray
        For i = 0 To rows.Size - 1
            Log("Rows #" & i)
            Dim m As Map
            m = rows.Get(i)
            harga = m.get("Harga")
        Next
        Dim job As HttpJob
        job.Initialize("Job1", Me)
        job.PostString(serverurl,"SELECT * FROM TransaksiP ORDER BY kodetransaksi DESC ")
        gg = 1
    Else If job.Success And gg = 1 Then
        If job.GetString = "[]" Then
            kodetransaksi = "1211121"
            Dim job As HttpJob
            job.Initialize("Job1", Me)
            job.PostString(serverurl,"SELECT * FROM TransaksiB ORDER BY no DESC")
            gg = 2
        Else
                   
            Dim response As String = job.GetString
            parser.Initialize(response)
            rows = parser.NextArray
            For i = 0 To rows.Size - 1
                Log("Rows j #" & i)
                Dim m As Map
                m = rows.Get(0)
                kodetransaksi = m.get("kodetransaksi")
            Next
       
       
       
            Dim job As HttpJob
            job.Initialize("Job1", Me)
            job.PostString(serverurl,"SELECT * FROM TransaksiB ORDER BY no DESC")
            gg = 2
        End If
    Else If job.Success And gg = 2 Then
        If job.GetString = "[]" Then
            no = "1"
        Else
            Dim respons As String = job.GetString
            Log(respons)
            parser.Initialize(respons)'line 113
            rows = parser.NextArray
            For i = 0 To rows.Size - 1
                Log("Rows i #" & i)
                Dim m As Map
                m = rows.Get(i)
                no = m.get("no")
            Next
        End If
    End If
    job.Release
End Sub
 
Upvote 0

ronell

Well-Known Member
Licensed User
Longtime User
i try some part of your code... works fine
what is the response of job.getstring?
B4X:
log(job.GetString)
 
Upvote 0

ronell

Well-Known Member
Licensed User
Longtime User
B4X:
dim job as httpjob
job.initialize("job1",Me) '<- maybe change "Me" to the name of the class Module
not sure

seriously .. its really hard to guess, if you can only post your project here and it will be easier to solve the issue
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Check your code; you stil have at least one Get(0)
B4X:
 gg = 2
Else
 Dim response As String = job.GetString
 parser.Initialize(response)
 rows = parser.NextArray
 For i = 0 To rows.Size - 1
   Log("Rows j #" & i)
   Dim m AsMap
   m = rows.Get(0)   '<--- HERE
   kodetransaksi = m.get("kodetransaksi")
 Next
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…