Android Question NullPointer Exception when initializing a Canvas

wdegler

Active Member
Licensed User
Longtime User
A NullPointerException error shows at the Cnv.Initialize(Pnl) line in the following sample:

Dim SV As ScrollView
Dim Pnl As Panel
Dim Cnv As Canvas
Dim Pth As Path

SV.Initialize(1000dip)
Pnl.Initialize("")
Activity.AddView(SV,10%x,10%y,20%x,20%y)
Cnv.Initialize(Pnl)
Pth.Initialize(5%x,5%y)
Pth.LineTo(8%x,5%y)
Cnv.DrawPath(Pth,Colors.Black,False,1)
Pnl.Invalidate

Can someone tell me how to correct this attempt to draw a line segment?
 

ronell

Well-Known Member
Licensed User
Longtime User
B4X:
Dim SV As ScrollView
Dim Pnl As Panel
Dim Cnv As Canvas
Dim Pth As Path

SV.Initialize(1000dip)
Pnl.Initialize("")
Activity.AddView(SV,10%x,10%y,20%x,20%y)
Activity.AddView(Pnl,10%x,10%y,20%x,20%y) ' <-- add the panel view in activity
Cnv.Initialize(Pnl)
Pth.Initialize(5%x,5%y)
Pth.LineTo(8%x,5%y)
Cnv.DrawPath(Pth,Colors.Black,False,1)
Pnl.Invalidate
 
Upvote 0

wdegler

Active Member
Licensed User
Longtime User
Thank you for your quick response, ronell!

I meant to include Pnl=SV.Panel instead of Pnl.Initialize("").
Now, I have a different error. Does your answer still apply?
 
Upvote 0

ronell

Well-Known Member
Licensed User
Longtime User
what is the error? and also post the code

code for adding the scrollview in a panel
B4X:
pnl.addview(sw,0,0,pnl.width,pnl.height)
 
Last edited:
Upvote 0

wdegler

Active Member
Licensed User
Longtime User
"java.lang.IllegalArgumentException: width and height must be > 0"

I find that adding Pnl.Width=100 and Pnl.Height=90 ahead doesn't help.
I don't have the error when Pnl is not in a ScrollView.
 
Upvote 0

ronell

Well-Known Member
Licensed User
Longtime User
when setting views layout it must be
B4X:
panel.width = 100dip or panel.width = 100%x

also post the relevant code.. it hard to guess what is the problem
 
Upvote 0

wdegler

Active Member
Licensed User
Longtime User
Dim SV As ScrollView
Dim Pnl As Panel
Dim Cnv As Canvas
Dim Pth As Path

SV.Initialize(1000dip)
Pnl=SV.Panel
Activity.AddView(SV,10%x,10%y,100%x,90%y)
Cnv.Initialize(Pnl)
Pth.Initialize(5%x,5%y)
Pth.LineTo(8%x,5%y)
Cnv.DrawPath(Pth,Colors.Black,False,1)
Pnl.Invalidate
 
Upvote 0

ronell

Well-Known Member
Licensed User
Longtime User
i dont know if the Pnl=SV.Panel will work but lets see
B4X:
Dim SV As ScrollView
Dim Pnl As Panel
Dim Cnv As Canvas
Dim Pth As Path

SV.Initialize(1000dip)
Activity.AddView(SV,10%x,10%y,100%x,90%y)
Pnl.Initialize("") '<- initiliaze the panel
Activity.AddView(Pnl,0,0,50%x,50%y) ' <-- add the panel in activity
' ---- >-Pnl=SV.Panel '<-- dont use.. it will not work
Cnv.Initialize(Pnl)
Pth.Initialize(5%x,5%y)
Pth.LineTo(8%x,5%y)
Cnv.DrawPath(Pth,Colors.Black,False,1)
Pnl.Invalidate

if it does not work.. upload your project as zip and il try to fix it

edit:
B4X:
SV.Panel.Addview(pnl,0,0,sv.width,sv.height) '<--- add panel to scrollview
 
Last edited:
Upvote 0

wdegler

Active Member
Licensed User
Longtime User
Here it is. I thank you very much for your attention to this matter.
 

Attachments

  • WDDSample.zip
    20.4 KB · Views: 197
Upvote 0

wdegler

Active Member
Licensed User
Longtime User
The file you sent me works! :)
My conclusion is that Pnl=SV.Panel, which I've used before with no problem, does not accept a Canvas in the panel of a ScrollView.
Thanks for your help. Now I can move forward again!
 
Upvote 0

ronell

Well-Known Member
Licensed User
Longtime User
just a reminder
if you will create another thread , use the code tags when posting codes so we can easily read it ,
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You don't need to add a new Panel to the ScrollView.
This code works:
B4X:
Sub Globals
    Private scvTest As ScrollView
    Private cvsTest As Canvas
    Private pnlScvTest As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
    scvTest.Initialize(2000dip)
    Activity.AddView(scvTest, 10dip, 10dip, 100%x - 20dip, 100%y - 20dip)
    pnlScvTest = scvTest.Panel
    pnlScvTest.Width = scvTest.Width
    cvsTest.Initialize(pnlScvTest)
   
    cvsTest.DrawLine(0, 0, 200dip, 100dip, Colors.Red, 2dip)
End Sub
What you were missing is that scvTest.Panel.Width is equal to -1 which means fill the parent view.
Setting the width to a value > 0, pnlScvTest.Width = scvTest.Width, solves the problem.
 
Upvote 0

wdegler

Active Member
Licensed User
Longtime User
You don't need to add a new Panel to the ScrollView.
This code works:
B4X:
Sub Globals
    Private scvTest As ScrollView
    Private cvsTest As Canvas
    Private pnlScvTest As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
    scvTest.Initialize(2000dip)
    Activity.AddView(scvTest, 10dip, 10dip, 100%x - 20dip, 100%y - 20dip)
    pnlScvTest = scvTest.Panel
    pnlScvTest.Width = scvTest.Width
    cvsTest.Initialize(pnlScvTest)
  
    cvsTest.DrawLine(0, 0, 200dip, 100dip, Colors.Red, 2dip)
End Sub
What you were missing is that scvTest.Panel.Width is equal to -1 which means fill the parent view.
Setting the width to a value > 0, pnlScvTest.Width = scvTest.Width, solves the problem.
Thank you Klaus for this information. It explains the problem and the solution very well!
 
Upvote 0

ronell

Well-Known Member
Licensed User
Longtime User
codetags.png
 
Upvote 0
Top