Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    14
    Rep Power
    0

    Updating JTextArea using Swing Timers


    I'm coming over from C# and trying to learn the in's and out's of Java. I'll admit I'm still fairly new to Java and am still learning programming theory. I'm not a newbie but still far from being a seasoned vet.

    What I'm trying to do is update a JTextArea by using a Swing.Timer. The JTextArea is setup in one class and the timer in another. When the timer goes off I am trying to call setText() but I end up getting a null pointer exception. I think I have narrowed this down to being the fact that my JTextArea is null but the solution eludes me. Maybe it's something else I'm doing wrong and I have not caught the issue yet. Here's a peak at the code I'm working with(this is my first post so if this isn't setup right just let me know and I will re-post in the preferred style but I tried to cut out as much as I could think of that did not relate).

    Gooey.java classFile:
    Code:
    public class Gooey {
    
    	EventManager EventManager;
    	JTextArea mainTextArea;
    	
    	public static void Gooey()
    	{		
    		//creating an instance of the event manager
    		EventManager EventManager = new EventManager();
    		
    		
    		//setup and align the text pane or mid-pane
    		JTabbedPane tabbedTextPane = new JTabbedPane(); 			//creating the tabbed text pane in the middle
    		tabbedTextPane.setPreferredSize(new Dimension(400,400));	//giving it a preferred size
    		tabbedTextPane.setBorder(blackBorder);						//giving it a border
    		g.gridwidth = 2;											//setting how many cells the text pane takes up
    		g.gridx = 0;												//setting it's position to column 1
    		g.gridy = 1;												//setting it's position to row 2
    		mainPane.add(tabbedTextPane, g);							//adding the tabbed pane to the main gooey
    		JTextArea mainTextArea = new JTextArea();						//creating a text area for display game events
    		JScrollPane mainScrollPane = new JScrollPane(mainTextArea);	//create a scroll pane and then adding the text area to it
    		mainTextArea.setEditable(false);							//making the text area read-only
    		tabbedTextPane.add("Current Events", mainScrollPane);		//adding the scroll pane(with mainTextPane) to the main tabbed pane	
    
    		
    		//add the main pane to the frame
    		frame.getContentPane().add(mainPane);
    		
    		//display the window
    		frame.pack(); //sizes the frame so that all content fits			
    		
    		frame.setVisible(true); //displays the window	
    		
    		EventManager.timer.start();		
    		
    	}
    
    	public void setEventText()
    	{		
    		mainTextArea.setText(getEventText());		
    	}
    
    	public String getEventText()
    	{		
    		return EventManager.eventText;
    	}	
    	
    	public static void main(String[] args)
    	{
    		//Schedule a job for the event-dispatching thread: (not sure what this means)
    		//creating and showing the gooey
    		javax.swing.SwingUtilities.invokeLater(new Runnable(){
    			public void run() {				
    				Gooey();
    			}
    		});		
    	}
    }

    EventManager.java classFile:
    Code:
    public EventManager()
    	{
    		speed = 1000;
    		
    		timer = new Timer(speed, new ActionListener() {
    			public void actionPerformed(ActionEvent updateEventText)
    			{		
    				Gooey.setEventText();
    				System.out.println("test");
    				timer.restart();
    			}
    		});
    	}
    The error is:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at EventManager$1.actionPerformed(EventManager.java:19)
    at javax.swing.Timer.fireActionPerformed(Unknown Source)
    at javax.swing.Timer$DoPostEvent.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(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.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    Now the "System.out.println("test");" code works correctly so I know my timer is working but as far as setting the text to the JTextArea I just keep getting a nullpointerexception and am struggling to understand why. Any thoughts?
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Code:
    Gooey.setEventText();
    That is the syntax for calling a static method in the Gooey class.
    The method setEventText() is NOT defined as static so that code should not compile without an error.
    How can you execute the code that is posted? It should not compile!!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    14
    Rep Power
    0
    I can't answer how that compiles but it does. It also executes. Maybe this is where my issue lies though? Should I restructure my code and create an instance of Gooey instead of using it as static?
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    create an instance of Gooey instead of using it as static
    Yes, try that.
    If you still have problems, make a small, complete program that compiles, executes and shows the problem and post that.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    14
    Rep Power
    0
    I'll start there and re-post, hopefully with success.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    14
    Rep Power
    0
    Still no success... same error. I created my Gooey() instance and I stuck a "System.out.println("test")" in the setEventText() method to test if that method is being called correctly but ended up with the same error and the println() never executes. For some reason that method is not being called correctly and errors out...?

    Code:
    public void setEventText()
    	{		
    		//mainTextArea.setText(getEventText());
    		System.out.println("test");
    	}
    Code:
    public EventManager()
    	{
    		speed = 1000;
    		
    		timer = new Timer(speed, new ActionListener() {
    			public void actionPerformed(ActionEvent updateEventText)
    			{		
    				Gooey.setEventText();				
    				timer.restart();
    			}
    		});
    	}
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Please make a small, complete program that compiles, executes and shows the problem and post that.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    14
    Rep Power
    0
    My mistake here.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    14
    Rep Power
    0
    Gahhh!!! I forgot to create an instance of the eventManager in that post above. Here is what the actual code looks like now and now I get the same error as before. Sorry about that, I'm a bonehead. I'll try to remove that post if I can to avoid confusion.

    Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    class HelloWorldApp2
    {
    	JTextArea textArea;
    	EventManager EventManager;
    	
    	public HelloWorldApp2()
    	{
    	JFrame frame = new JFrame("test");
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    	EventManager EventManager = new EventManager();
    	
    	JPanel pane = new JPanel();
    	
    	pane.setPreferredSize(new Dimension(200,200));
    	
    	JTextArea textArea = new JTextArea();
    	textArea.setPreferredSize(new Dimension(200,200));
    	
    	pane.add(textArea);
    	
    	frame.getContentPane().add(pane);
    	
    	frame.pack();
    	
    	frame.setVisible(true);
    	
    	EventManager.timer.start();
    	
    	}
    	
    	public void setEventText()
    	{
    		textArea.setText("test");
    	}
    	
    	public static void main(String[] args)
    	{
    		HelloWorldApp2 testClass = new HelloWorldApp2();
    	}
    }
    
    class EventManager
    {
    	
    	String eventText = "Welcome";
    	Timer timer;
    	int speed;
    	ActionListener updateEventText;
    	HelloWorldApp2 testClass;
    	
    	public EventManager()
    	{
    		speed = 1000;
    	
    		timer = new Timer(speed, new ActionListener() {
    			public void actionPerformed(ActionEvent updateEventText)
    			{		
    				testClass.setEventText();				
    				timer.restart();
    			}
    		});
    	}
    }
    Error:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at EventManager$1.actionPerformed(testClass.java:65)
    at javax.swing.Timer.fireActionPerformed(Unknown Source)
    at javax.swing.Timer$DoPostEvent.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(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.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Error:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at EventManager$1.actionPerformed(testClass.java:65)
    What variable is null on line 65? Find the variable and then backtrack to see why it does not have a valid value.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    14
    Rep Power
    0
    Yea I started there and that line is:

    Code:
    testClass.setEventText();
    Which is:
    Code:
    public void setEventText()
    	{
    		textArea.setText("test");
    	}
    I'm assuming that maybe I am not referencing the correct JTextArea and that is why it is null?
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    What variable has the null value?
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    14
    Rep Power
    0
    That's where I'm confused as well. I'm not sure what variable could have the null value?
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    14
    Rep Power
    0
    Or maybe I'm not referencing testClass correctly here?
  28. #15
  29. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    not sure what variable could have the null value
    Use the println() method to print out the values of all the variables used on line 65.
    The print out will show you which one is null.
    Last edited by NormR; December 4th, 2012 at 02:34 PM.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo