The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Java Output error

MarcoND7MarcoND7 Registered User regular
edited September 2009 in Help / Advice Forum
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.
MarcoND7 on

Posts

  • Jimmy KingJimmy King Registered User regular
    edited September 2009
    Not a Java expert and only occasionally use it for web based stuff, so I could be wrong here. My guess is that your error stems from "add (Criticise);". I suspect you're missing a critical step prior to that. I'm not going to tell you exactly what that step is, though, because that's kind of like doing your homework for you.

    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.

    Jimmy King on
  • Gilbert0Gilbert0 North of SeattleRegistered User regular
    edited September 2009
    With Java, when you look at the stack trace, you basically go down until you find the line that is no longer library calls and is your code. In the above error, that would be

    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.

    Gilbert0 on
  • shadowaneshadowane Registered User regular
    edited September 2009
    You need to initialize something in your applet before you can call add() in your init method. I don't know what it is offhand but it should be easy to find online.

    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.

    shadowane on
  • Elbonian ManElbonian Man Registered User regular
    edited September 2009
    You haven't initialized the Criticise button but you try to add() it in init() so do that.

    Elbonian Man on
  • Jimmy KingJimmy King Registered User regular
    edited September 2009
    So much for teaching the guy how to debug/troubleshoot programs.

    Jimmy King on
  • MarcoND7MarcoND7 Registered User regular
    edited September 2009
    Thanks for the help Guys! Sorry I couldn't thank you before hand, but I wasn't able to get back on the forum for a while. That troubleshooting tip is one I will definately look into later.

    Thanks again!

    MarcoND7 on
    The author is not responsible for any bad puns, jokes, or other jackassy things. Thank you.
  • Jimmy KingJimmy King Registered User regular
    edited September 2009
    Learning to troubleshoot code efficiently and effectively will add at least as much value to you as an employee as writing better code will and give you a skill that many developers don't have. I work with a lot of guys (both at my company and partner companies) that can write great code, but once something doesn't behave as expected or breaks, are completely helpless at tracking down what in the code is causing it. Being able to figure out what went wrong in some back end library is a large part of what has kept me employed for the last couple of years while our 20-30 person dev team has been cut back to 2 full timers, a sys admin who does some jr dev, and a 10 hr/week emergency part timer.

    Jimmy King on
Sign In or Register to comment.