Rotating label error

I have been using this workaround for rotating a label using the paintable canvas for a while.

from java.awt import Color
from java.lang import Math
from java.awt import RenderingHints
from java.awt import GradientPaint
from java.awt.geom import Rectangle2D

g = event.graphics
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
fm = g.getFontMetrics()


#g.setColor(event.source.background)

#g.fillRect(0,0,event.width,event.height)


stringWidth = fm.stringWidth(event.source.text)
g.setColor(event.source.foreground)
g.rotate(-Math.PI/2)
g.drawString(event.source.text,-stringWidth,fm.getHeight())

now in 8.0.13
getting an error…

Traceback (most recent call last):
File “event:repaint”, line 17, in
java.lang.NullPointerException
java.lang.NullPointerException: java.lang.NullPointerException

caused by NullPointerException

Ignition v8.0.13 (b2020060815)
Java: Azul Systems, Inc. 11.0.6

I do know in my application the custom property ‘text’ is empty at times.

Thanks for the help.

You have to code defensively in paint events, with heavy use of try:except: or checks for null. Be aware that you can be called to paint for situations that have an incomplete g, like missing font or fontMetrics. Paint is likely to be called again immediately after g is complete, so it is best to simply draw nothing when encountering such errors.

Any other errors should be logged and handled defensively, drawing whatever is still possible, and never letting an exception “escape” from your paint method.

1 Like