| Documentation Contents

> ## `java.lang.NullPointerException` Thrown in `java.awt.Graphics.drawString()`

> ###
> Symptoms

> > When running an applet in a browser using the Sun JavaTM Runtime Environment (JRETM), a `java.lang.NullPointerException` is thrown by the `java.awt.Graphics.drawString()` method. The same applet runs under the Microsoft Virtual Machine (VM).

> ### Cause

> > This exception is caused by passing a null string to the `Graphics.drawString()` method in the Sun JRE.

> > The Java class libraries in the Sun JRE have changed over time. Some APIs have been clarified, some have been deprecated, and some have had their implementation altered.

> > The result of passing a null string to the `Graphics.drawString()` method was not well defined, and the Microsoft VM treats null as an empty string. However, the implementation in the Sun JRE does not accept null as a valid string, thus resulting in a `java.lang.NullPointerException`.

> ### Resolution

> > Code defensively to ensure only a non-null string is passed to the `drawString()` method. For example, the following code

> > > `Graphics g = getGraphics();
> > > g.drawString(s, 100, 100);`

> > should be changed to:

> > > `Graphics g = getGraphics();
> > > if (s !== null) {`
> > > `g.drawString(s, 100, 100);
> > > }`

> ### Related Information

> > None.

Copyright © 1993, 2010, Oracle and/or its affiliates. All rights reserved. Please send comments using this Feedback page. | Java Technology