Hey guys, I am learning programming and I am trying to make a simple java program. I seem to be getting an error, but the sub today doesn't know java.
I was wondering if you could take a look and see if you can see anything.
// The "TVShows" class.
import java.applet.*;
import java.awt.*;
public class TVShows extends Applet
{
Label Question;
TextField Show;
String ShowName;
Label result;
TextField Opinion;
Button Criticise;
// Place instance variables here
public void init ()
{
Question = new Label ("What is your favourite TV show?");
Show = new TextField (25);
result = new Label ("The computer's opinion:");
Opinion = new TextField (25);
add (Question);
add (Show);
add (result);
add (Opinion);
add (Criticise);
// Place the body of the initialization method here
} // init method
public boolean action (Event e, Object o)
{
ShowName = Show.getText ();
if (e.target instanceof Button)
{
if (ShowName == "Firefly")
{
Opinion.setText ("YES! You are AWESOME!");
}
else
{
Opinion.setText (ShowName + ". Really? It's a terrible show.");
}
}
return true;
// Place the body of the drawing method here
} // paint method
} // TVShows class
The output, instead of a couple of text fields, is the following:
java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at TVShows.init(TVShows.java:26)
at ready.AppletRunner.run(AppletRunner.java:209)
at java.lang.Thread.run(Unknown Source)
Thanks for all your help guys.
The author is not responsible for any bad puns, jokes, or other jackassy things. Thank you.
Posts
basically, though, the error is saying that you've got a pointer to an object, but that object doesn't exist and is null. That error is coming from java.awt.Container.addImpl, which is come from java.awt.Container.add, which is being called at line 26 of TVShows.java inside TVShows.init. Basically, an object which does not exist yet was passed to add().
Take a look at your Criticise button and see if you missed something there.
java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source) --library
at java.awt.Container.add(Unknown Source) --library
at TVShows.init(TVShows.java:26) <
YOUR CODE.
at ready.AppletRunner.run(AppletRunner.java:209)
at java.lang.Thread.run(Unknown Source)
The 26, is the line number where it had an issue. So look at the TVShows.java file at line 26 and that is the problem.
Like Jimmy, I also see the error but I'd rather tell you how too look at the stack trace to figure it out yourself.
edit: The above is more helpful in the future.
edit2: Also, a NullPointerException in java means you are trying to do something to an object that hasn't been initialized. Usually this occurs inside your code, but clearly the Applet class expects you to initialize something on your own before calling add.
Thanks again!