Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting a couple errors on scribble applet

 
C Pfafffinnna
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It compiles fine. When I try to run it in appletviewer, I get the errors listed below. Any help is appreciated....



Errors:
at clearablescribble.stringToColor(clearablescribble.java:98)
at clearablescribble.init(clearablescribble.java:23)
at sun.applet.AppletPanel.run(AppletPanel.java:348)
at java.lang.Thread.run(Thread.java:536)
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Welcome to JavaRanch!
The stack trace (error message) you're showing is cut off at the top; there's at least one more line that says "NullPointerException" or something like that. I'm going to guess that it says "NullPointerException." It tells you the line where the error happens: line 98 in stringToColor(). I don't have the file, so again, I have to guess, but line 89 is probably

and I'm guessing that theString is null when it's passed in. Calling a method on this null pointer gives the NullPointerException.
Now, why is it null? You can see from the stack trace that when the error happens, stringToColor() is being called from innit(). Well, look at this code in init():

foregroundString is an applet parameter, and applet parameters can be unspecified, in which case they're null. That's what I'm guessing is happening here -- you're not passing in one of the parameters, and so when you call stringToColor(), the argument is null. The solution is something like this:

so that the method isn't called if the argument would be null.
One more thing: you're clearly new to languages in which array indices start at zero. It's really not so bad when you get used to it! Here's a version of stringToColor() that makes more idiomatic use of the array -- it's more readable, don't you agree?
 
C Pfafffinnna
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou for your clear and precise explanation, I am still getting one error though, and it may be my own fault... take a look:

clearablescribble.java:23: incompatible types
found : java.lang.String
required: java.awt.Color
Color foreground = foregroundString == null ? "black" : stringToColor(foregroundString);
^
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic