Results 1 to 4 of 4
  1. #1
    chaucer345 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Unusual Runtime Error when invoking the getText() method of JTextArea

    Hello all, I'm not sure if I should have posted this in the applet forum, but I could really use some help

    I've been trying to develop a text adventure applet to run in a browser. Everything in my program compiles alright,
    but I can't even start troubleshooting the meat of the program because I can't seem to retrieve the text entered into
    my JTextArea. The applet launches and the GUI looks fine, but when I try to enter something I get the following run-time error
    message from the line that invokes the getText method of my JTextArea:

    java.lang.NullPointerException

    Then I get a variety of error messages saying things about an unknown source.

    I'm really perplexed by what's going on here, I've tried looking up the exception, which seems to indicate that the value returned by userInput.getText() is a null object, but I can't seem to figure out why. I could really use some advice.

    The faulty class of my code is below with a lot of explanatory comments, and I've included the compiler error message below that. I would appreciate any assistance I could get in solving this mystery. Thank you all for reading.

    Java Code:
    /**
     * Program creates a GUI that then adds to a String variable story and displays
     * the string in a TextArea output. It receives input from the user into the 
     * JTextArea userInput and then passes that input off for error checking by
     * author, an instance of the StoryTeller class. It also passes the memory 
     * address of an instance of the Story class called theStory to author so that 
     * author can display the text in the  various methods there based on the user's 
     * input.
     * ------------------------------------------------------
     * Programmer: 
     * Program: Wounded
     * Last modified: March 3, 2013 5:00 PM Central Time
     * ------------------------------------------------------ 
     */
    
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JTextArea;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.TextArea;
    
    public class MainProgram extends JApplet implements ActionListener
    {
    //Declare all the necessary variables and initialize as needed.
    private TextArea userInput;
    private TextArea output;
    private boolean canProceed;
    static final long serialVersionUID = 1; // added in an attempt to fix an error, highly confused as to what it does.
    public String story = "My Little Pony: Friendship is Magic is the property of\n" +
    		"Hasbro, Lauren Faust, and the rest of its creative team. \n" +
    		"Please support the official release. \n\n" +
    		"Your eyes are bleary... you feel drained, so very " +
    		"drained... and dizzy. \nYou tilt your head over to " +
    		"the side of the rickety cot you're lying on and \n" +
    		"manage to throw up. To your surprise, there's a " +
    		"basin waiting for you.\n" +
    		"\"Hey there,\" comes a voice.\n" +
    		"You see an Earth Pony mare in an orange vest sitting at your bedside." +
    		"She gives you a half hearted smile. \"Welcome to the land of the living.\" \n" +
    		"\"Can you tell me your name?\" She asks. \n\n";
    public String input;
    StoryTeller author = new StoryTeller(0, 0, 0, 0, 0, 0);
    Story theStory = new Story();
    
    public void init()
    {
    	//Add the necessary content pane and set the layout.
    	Container contentPane = getContentPane();
    	contentPane.setLayout(new FlowLayout());
    	
    	//Create the JTextArea for user input.
    	JTextArea userInput = new JTextArea(1, 20);
    	userInput.setText("Enter your choice here");
    	contentPane.add(userInput);
    	
    	//Create the essential choose button to signal that
    	//the program should retrieve user input from userInput.
    	JButton chooseButton = new JButton();
    	chooseButton.setText("Choose");
    	chooseButton.addActionListener(this);
    	contentPane.add(chooseButton);
    	
    	//Create the TextArea for the display of story.
    	TextArea output = new TextArea(20, 100);
    	output.setText(story);
    	contentPane.add(output);	
    }
    
    /*Method that realizes when chooseButton has been pressed 
     * and then stores the string entered into userInput
     * in input. Then it checks if it can proceed with that
     * input by calling the author.readInput method which
     * also returns a boolean which is stored in canProceed.
     * If canProceed is true, the program invokes the 
     * author.continueStory method which returns the new story 
     * (among other things discussed in the documentation 
     * for StoryTeller). If canProceed is false, then 
     * an error message is added to story. In either case
     * the text of output is set to story.
    */
    public void actionPerformed(ActionEvent e)
    {
    	String	actionCommand = e.getActionCommand();
    	if (actionCommand.equals("Choose"))
    	{
    		input = userInput.getText();
    		canProceed = author.readInput(input);
    		if (canProceed)
    		{
    		story = author.continueStory(story, theStory);
    		output.setText(story);
    		}
    		else
    		{
    			story = story + "\n Not a valid choice, choose again:\n";
    			output.setText(story);
    		}
    	}
    }
    
    }


    Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at MainProgram.actionPerformed(MainProgram.java:89)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Unusual Runtime Error when invoking the getText() method of JTextArea

    You are shadowing the userInput variable by re-declaring it in the init method. Since only the variable declared in the init method is initialized, you will leave the shadowed class field as null causing your NPE. The solution: don't re-declare the variable inside of the init method, instead initialize the class field inside of init() and then the class field won't be null.

    Note that the key to solving this is to try to find out *which variable on the offending line is null*. Once you find out it is the userInput variable, you must look closely at where you think you are initializing this variable to see why it is not in fact being initialized.
    Last edited by Fubarable; 03-30-2013 at 02:34 AM.

  3. #3
    chaucer345 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Re: Unusual Runtime Error when invoking the getText() method of JTextArea

    Awesome! It worked, now I finally have a running GUI! You rock!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Unusual Runtime Error when invoking the getText() method of JTextArea

    Glad to help and glad you've got it working!

Similar Threads

  1. Invoking method from applet
    By Dissonance in forum Java Applets
    Replies: 0
    Last Post: 07-09-2012, 09:42 PM
  2. Unusual null pointer error
    By PRW56 in forum New To Java
    Replies: 8
    Last Post: 03-22-2012, 06:58 PM
  3. Runtime error in a method
    By Pojahn_M in forum New To Java
    Replies: 8
    Last Post: 08-02-2011, 04:23 AM
  4. Invoking a method
    By Brainz in forum New To Java
    Replies: 6
    Last Post: 12-06-2010, 02:07 PM
  5. Invoking method from String
    By Supamagier in forum Advanced Java
    Replies: 12
    Last Post: 05-29-2009, 09:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •