Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By DarrylBurke
  • 1 Post By JosAH

Thread: Editing a JTextArea (WindowBuilder Eclipse Indigo) via another class

  1. #1
    HaveANiceDay is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Question Editing a JTextArea (WindowBuilder Eclipse Indigo) via another class

    Hey guys,
    me and my brother are programming a small simulator.
    We have some experience with programming in Java but now we want to try to program a GUI with JFrame.
    To show you the problem we created a simplified version of the program.
    It all starts with the button (btnTextschreiben). This button runs the function sendordertorandomclass().
    This function runs a randomfunction() in another class (randomclass). This function tries to submit the string "write this text in the text area" to the write() function of the window class which should setText() of the text area.

    If we run the program, we get a NullPointerException. You can find the whole error message at the bottom.

    In our program we want to use this to write certain events into an text area in the GUI.
    Just like a debug console, just with events happening in the simulator.

    GUI CLASS
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JButton;
    import javax.swing.JTextArea;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    
    public class window extends JFrame {
    
    	private JPanel contentPane;
    	private randomclass arandomclass;
    	private JTextArea textarea;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					window frame = new window();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public window() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		contentPane.setLayout(new BorderLayout(0, 0));
    		setContentPane(contentPane);
    		
    		JButton btnTextschreiben = new JButton("write text");
    		btnTextschreiben.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				sendordertorandomclass();
    			}
    		});
    		contentPane.add(btnTextschreiben, BorderLayout.NORTH);
    		
    		JTextArea textArea = new JTextArea();
    		contentPane.add(textArea, BorderLayout.CENTER);
    	
    		arandomclass = new randomclass(this);
    		
    	}
    	
    	private void sendordertorandomclass(){
    		arandomclass.randomfunction();
    	}
    	
    	public void write(String text){
    		textarea.setText(text);
    	}
    }
    RANDOM CLASS
    Java Code:
    public class randomclass {
    	
    	private window theframe;
    	
    	
    	public randomclass(window givenframe){
    		this.theframe = givenframe;
    	}
    	
    	public void randomfunction(){ //if the button gets pressed, this function shall communicate to write a certain text in the text area
    		theframe.write("write this text in the text area");
    	}
    }
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at window.write(window.java:66)
    at randomclass.randomfunction(randomclass.java:12)
    at window.sendordertorandomclass(window.java:62)
    at window.access$0(window.java:61)
    at window$2.actionPerformed(window.java:49)
    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.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(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.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(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
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Editing a JTextArea (WindowBuilder Eclipse Indigo) via another class

    Take a look at line 54 of the code you posted, there is something not correct there.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    HaveANiceDay is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: Editing a JTextArea (WindowBuilder Eclipse Indigo) via another class

    Quote Originally Posted by gimbal2 View Post
    Take a look at line 54 of the code you posted, there is something not correct there.
    I don't think this line is the problem. It was automatically created by windowbuilder and we didn't change it.
    Could you explain why you think there is a mistake there?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Editing a JTextArea (WindowBuilder Eclipse Indigo) via another class

    Ever heard of variable hiding? A local variable hides an instance field when both have the same name.

    db
    gimbal2 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Editing a JTextArea (WindowBuilder Eclipse Indigo) via another class

    Yet another thread that proves that code generating tools are only meant to be used by professionals, not novices.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    HaveANiceDay is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: Editing a JTextArea (WindowBuilder Eclipse Indigo) via another class

    Quote Originally Posted by DarrylBurke View Post
    Ever heard of variable hiding? A local variable hides an instance field when both have the same name.

    db
    Yeah, we already thought this isn't a really good idea to write it like that. We had a problem getting access to the text area of the frame.
    Luckily we found a solution for the problem now. We deleted the attribute text area and changed the function write() to the following:

    Java Code:
    	public void write(String text){
    		JTextArea temp = (JTextArea) contentPane.getComponent(1);
    		temp.setText(text);
    Thanks for your help anyway. :)

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: Editing a JTextArea (WindowBuilder Eclipse Indigo) via another class

    I would've changed line #54 to just this:

    Java Code:
    textArea = new JTextArea();
    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Maven 3.2 with eclipse indigo
    By shgvs in forum Apache Maven
    Replies: 1
    Last Post: 06-03-2013, 07:00 AM
  2. Eclipse Indigo compiling ear
    By BenitoJuarez in forum Eclipse
    Replies: 0
    Last Post: 04-16-2012, 11:17 AM
  3. problem compiling jpcap in Eclipse Indigo
    By misteryuku in forum Eclipse
    Replies: 1
    Last Post: 04-16-2012, 05:46 AM
  4. problem in loading eclipse indigo
    By Balavenkatraman in forum Eclipse
    Replies: 2
    Last Post: 04-09-2012, 09:03 PM
  5. how to use databse in eclipse indigo ?
    By naved in forum Eclipse
    Replies: 1
    Last Post: 08-10-2011, 10:44 AM

Tags for this Thread

Posting Permissions

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