Null Pointer Exception

Coding + IDE + Code-Editor + Syntax + User-Functions + Anything else

Null Pointer Exception

Postby Henning » Fri Jun 01, 2012 2:00 pm

Hi,

The Print line throws a NULL Pointer Exception when run!?
Code: Select all
Dim BCnt As Integer
Dim Addr As Integer
Dim RecType As Byte
....
    For x = 0 To LineCount - 1
      BCnt = Val("0x0" & Mid(Buffer[x], 1, 2))
      Addr = Val("0x0" & Mid(Buffer[x], 3, 4))
      RecType = Val("0x0" & Mid(Buffer[x], 7, 2))
      pgmcode = Mid(Buffer[x], 9, Len(Buffer[x]) - 10)
      Print(String(x) & ": " & String(BCnt) & ", " & String(Addr) & ", " & String(RecType) & "\n")
      ....


Have I missed something? Mid to extract a substring is not in the reference???

/Henning
Henning
 
Posts: 523
Joined: Fri Aug 13, 2010 6:29 pm

Re: Null Pointer Exception

Postby Henning » Fri Jun 01, 2012 2:08 pm

<<If I try to enter it as lower case (mid) it reverts to (Mid) when I leave the line in the editor...

This is a "bug" in the code editor. Will fix this.

There are three Mid

+ Mid assignment as you expect

+ Mid read out

+ and QString . mid (native Qt mid function)
Henning
 
Posts: 523
Joined: Fri Aug 13, 2010 6:29 pm

Re: Null Pointer Exception

Postby Slowdown » Fri Jun 01, 2012 2:19 pm

Code: Select all
String = Mid(String, StartPos, NumOfChar)

Is the way to go and indeed it's Mid
Kind regards,
Slowdown

OSX 10.8.x, VBox Xp, Ubuntu 32/64 bit, EOS 64 bit
Slowdown
 
Posts: 465
Joined: Sat May 01, 2010 7:11 pm
Location: Netherlands

Re: Null Pointer Exception

Postby Henning » Fri Jun 01, 2012 4:05 pm

From the Q7Basic's Language Reference:
Code: Select all
Keyword Mid
Mid(STRINGVARIABLE, Position As Integer, Length As Integer) = {STRINGEXPRESSION | ASCII-Code}

Replaces text inside a string by another text.

Sub Main()
Dim txt As String, replacement As String, originaltxt As String

replacement = "The power of Q7Basic"
originaltxt = "***********************"
Dim i As Integer
For i = 1 To Len(replacement)
Mid(originaltxt, 2, i) = replacement
MsgBox(originaltxt)
Next
End Sub


From the Reference Classes QString:
Code: Select all
Function mid(Position As Integer, N As Integer) As String


Confusing...

And I still don't understand why a NULL Pointer Exception on the line Print( .... ).

/Henning
Henning
 
Posts: 523
Joined: Fri Aug 13, 2010 6:29 pm

Re: Null Pointer Exception

Postby Slowdown » Fri Jun 01, 2012 7:04 pm

I have used the Function several times but indeed the manual tell's something different.
But the code you posted also works :o
For replace i use,
Function Replace(Argument As String, Search As String, Replace As String, CaseSensitive As Boolean = True) As String

Replace(...) changes all occurrences of a string inside another string to a new string.

Dim s = "Das ist alles was wir brauchen. Fang nochmal von vorne an."
Dim search = "vorne"
Dim replace = "hinten"
MsgBox(Replace(s, search, replace))

And I still don't understand why a NULL Pointer Exception on the line Print( .... ).

One of the variables has no value you can check this by using stdout.

Code: Select all
Dim MyInt As Integer
MyInt = Null
Print String(MyInt)

Generate the same error you now get.
Kind regards,
Slowdown

OSX 10.8.x, VBox Xp, Ubuntu 32/64 bit, EOS 64 bit
Slowdown
 
Posts: 465
Joined: Sat May 01, 2010 7:11 pm
Location: Netherlands

Re: Null Pointer Exception

Postby Henning » Fri Jun 01, 2012 9:15 pm

The problem is converting a Hex String to an Integer.

In VB that is "a piece of cake", Val("&H" & HexString). In Q7b there is no "&H", and Val( "0x0" & HexString) is not functioning... Therefor the integer variable is set to NULL.

HELP!!! :?

/Henning
Henning
 
Posts: 523
Joined: Fri Aug 13, 2010 6:29 pm

Re: Null Pointer Exception

Postby Slowdown » Sat Jun 02, 2012 12:23 pm

There is a Function Integer>Hex but no Hex>Integer.
I think that Bernd is that one forgotten.
HELP!!!

to the rescue,
Code: Select all
Event Init()
  MyInt = HexToInt("&h8f")
  MsgBox("Hex to Dec", String(MyInt))
End Event

Function HexToInt(HexValue As String) As Integer
  Dim Lus As Integer
  Dim Power As Integer
  Dim CharValue As String
  Dim Result As Integer

  Result = 0
  HexValue = UCase(Trim(HexValue))
  ' Remove the two left characters &H
  HexValue = Right(HexValue, Len(HexValue) - 2)
  ' now we cycle from right to left taking one character (value) at a time
  For Lus = Len(HexValue) To 1 Step - 1
    Power = Len(HexValue) - Lus
    CharValue = Mid(HexValue, Lus - 1, 1)
    Select Case CharValue
      Case "A"
        CharValue = "10"
      Case "B"
        CharValue = "11"
      Case "C"
        CharValue = "12"
      Case "D"
        CharValue = "13"
      Case "E"
        CharValue = "14"
      Case "F"
        CharValue = "15"
    End Select
    Result = Result +(Integer(CharValue) * CMath.pow(16, Power))
  Next
  Return Result
End Function

Did test some conversions and they worked.
Kind regards,
Slowdown

OSX 10.8.x, VBox Xp, Ubuntu 32/64 bit, EOS 64 bit
Slowdown
 
Posts: 465
Joined: Sat May 01, 2010 7:11 pm
Location: Netherlands

Re: Null Pointer Exception

Postby berndnoetscher » Mon Jun 04, 2012 7:48 am

<<If I try to enter it as lower case (mid) it reverts to (Mid) when I leave the line in the editor...

This is a "bug" in the code editor. Will fix this.

There are three Mid

+ Mid assignment as you expect

+ Mid read out

+ and QString . mid (native Qt mid function)
berndnoetscher
Site Admin
 
Posts: 344
Joined: Thu Mar 25, 2010 9:57 am

Re: Null Pointer Exception

Postby berndnoetscher » Mon Jun 04, 2012 7:50 am

<<And I still don't understand why a NULL Pointer Exception on the line Print( .... ).


Printing a Null value is not permitted here. I could change it though. What do you guys think?
berndnoetscher
Site Admin
 
Posts: 344
Joined: Thu Mar 25, 2010 9:57 am

Re: Null Pointer Exception

Postby Henning » Mon Jun 04, 2012 8:59 am

The error originates from a Redim Preserve on a Byte Array.

I'm converting an Intel-hex file for an Atmel Mega u-processor programmer to binary to reduce size. It is then used to update firmware in my HW devices over narrowband radio.

Code: Select all
    i = PgmSize Mod 128
    x = PgmSize
    If i <> 0 Then
      i = PgmSize \ 128
      i = i + 1
      PgmSize = i * 128
    End If
    ReDim Preserve PgmBuffer[PgmSize]
'***
    If x <> PgmSize Then
      For i = x + 1 To PgmSize - 1
        PgmBuffer[i] = 255
      Next
    End If
'***

PgmBuffer needs to be exactly a power of 128 bytes.
Before adding the code betwen '***. the expanded part of Byte Array PgmBuffer[] is not by default initialized to a zero (0) value, but Null.
This is ported from VB, there the default value is "0".

Always, if not explicity set, by default initialize numeric variables to a zero value would IMO be prefered. Catching Null's in Print could be useful when testing.

/Henning
Henning
 
Posts: 523
Joined: Fri Aug 13, 2010 6:29 pm

Next

Return to Questions & Answers

Who is online

Users browsing this forum: No registered users and 0 guests

cron