Results 1 to 17 of 17
  1. #1
    Mike. is offline Member
    Join Date
    Dec 2011
    Posts
    12
    Rep Power
    0

    Default Calling a JTextField from another class

    I'm having some problems with my code.
    I have 2 classes: Engine.class and itemadd.class.
    My Engine.class handles the GUI and all the variables and ActionListeners etc..
    And the itemadd.class handles the method which I'm using to save data from several JComboBoxes and one JTextField.

    In the Engine.class I have an JButton called "button1" and an actionlistener for it, when the button1 is clicked it will call the method "addfile".

    Currently I'm only trying to get it to write the text from JTextField called "answer4" to a text document.
    The method itself works, I've tried it without calling the answer4 JTextField and everything worked like it should.
    But when I'm trying to call the answer4 JTextField it gives me an Null Pointer Exception.

    Please look at the codes and let me know if you have any ideas to make it work :)

    Engine.class
    Java Code:
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    
    
    import javax.swing.*;
    
    public class Engine extends JPanel {
    
    	
        /**
    	 * 
    	 */
    	private static final long serialVersionUID = -1828230033027140410L;
    
    
    
    
    
    	public Engine() {
            super(new GridLayout(10,3));  //2 rows, 1 column
            JLabel label1, label2, label3, label4, label5, label6, label7, filler1, filler2, filler3, filler4, filler5, filler6, filler7,
            filler8, filler9, filler10, filler11;
            
            final JComboBox answer1;
    		JComboBox answer2, answer3, answer5, answer6;
    		final JComboBox answer7;
            JTextField answer4;
            final JButton edit3;
    		final JButton edit5;
    		final JButton edit6;
    		JButton button1, button2;
            
     
            //Create the labels:
            label1 = new JLabel("Item type:");
            label2 = new JLabel("Item status:");
            label3 = new JLabel("Item name:");
            label4 = new JLabel("Item attributes:");
            label5 = new JLabel("Account name:");
            label6 = new JLabel("Character:");
            label7 = new JLabel("Choose file to open:");
     
            //Create the "answers":
            answer1 = new JComboBox(itemtype);
            answer2 = new JComboBox(itemstatus);
            answer3 = new JComboBox();
            answer4 = new JTextField();
            answer5 = new JComboBox();
            answer6 = new JComboBox();
            answer7 = new JComboBox(choosefile);
            
            //Create the edit buttons:
            edit3 = new JButton("Edit");
            edit5 = new JButton("Edit");
            edit6 = new JButton("Edit");
            
            //Create the fillers:
            filler1 = new JLabel("");
            filler2 = new JLabel("");
            filler3 = new JLabel("");
            filler4 = new JLabel("");
            filler5 = new JLabel("");
            filler6 = new JLabel("");
            filler7 = new JLabel("");
            filler8 = new JLabel("");
            filler9 = new JLabel("");
            filler10 = new JLabel("");
            filler11 = new JLabel("");
            
            //Create the other buttons:
            button1 = new JButton("Add item");
            button2 = new JButton("Open");
     
            //Add the components.
            add(label1);
            add(answer1);
            add(filler10);
            
            add(label2);
            add(answer2); 
            add(filler11);
            
            add(label3);
            add(answer3);
            add(edit3);
            
            add(label4);
            add(answer4);
            add(filler1);
            
            add(label5);
            add(answer5);
            add(edit5);
            
            add(label6);
            add(answer6);
            add(edit6);
            
            add(filler2);
            add(filler3);
            add(filler4);
            
            add(filler5);
            add(button1);
            add(filler6);
            
            add(filler7);
            add(filler8);
            add(filler9);
            
            add(label7);
            add(answer7);
            add(button2);
            
            //Create the listeners
            button1.addActionListener(new java.awt.event.ActionListener() {
            	public void actionPerformed(java.awt.event.ActionEvent evt) {
            	button1ActionPerformed(evt);
            	}
            	});
            
            button2.addActionListener(new java.awt.event.ActionListener() {
            	public void actionPerformed(java.awt.event.ActionEvent evt) {
            	button2ActionPerformed(evt, answer7);
            	}
            	});
            
            edit3.addActionListener(new java.awt.event.ActionListener() {
            	public void actionPerformed(java.awt.event.ActionEvent evt) {
            	edit3ActionPerformed(evt, edit3);
            	}
            	});
    
            edit5.addActionListener(new java.awt.event.ActionListener() {
            	public void actionPerformed(java.awt.event.ActionEvent evt) {
            	edit5ActionPerformed(evt, edit5);
            	}
            	});
            
            edit6.addActionListener(new java.awt.event.ActionListener() {
            	public void actionPerformed(java.awt.event.ActionEvent evt) {
            	edit6ActionPerformed(evt, edit6);
            	}
            	});
            
            answer1.addActionListener(new java.awt.event.ActionListener(){
                public void actionPerformed(ActionEvent e) {
                	answer1ActionPerformed(e);
                }
                });
            
            answer2.addActionListener(new java.awt.event.ActionListener(){
                public void actionPerformed(ActionEvent e) {
                	answer2ActionPerformed(e);
                }
                });
            
            answer3.addActionListener(new java.awt.event.ActionListener(){
                public void actionPerformed(ActionEvent e) {
                	answer3ActionPerformed(e);
                }
                });
            
            answer5.addActionListener(new java.awt.event.ActionListener(){
                public void actionPerformed(ActionEvent e) {
                	answer5ActionPerformed(e);
                }
                });
            
            answer6.addActionListener(new java.awt.event.ActionListener(){
                public void actionPerformed(ActionEvent e) {
                	answer6ActionPerformed(e);
                }
                });
    	}
    
    
    
    	protected void answer6ActionPerformed(ActionEvent e) {
            JComboBox answer6 = (JComboBox)e.getSource();
            chosenfile6 = (String)answer6.getSelectedItem();
    	}
    
    	protected void answer5ActionPerformed(ActionEvent e) {
            JComboBox answer5 = (JComboBox)e.getSource();
            chosenfile5 = (String)answer5.getSelectedItem();
    	}
    
    	protected void answer3ActionPerformed(ActionEvent e) {
            JComboBox answer3 = (JComboBox)e.getSource();
            chosenfile4 = (String)answer3.getSelectedItem();
    	}
    
    	protected void answer2ActionPerformed(ActionEvent e) {
            JComboBox answer2 = (JComboBox)e.getSource();
            chosenfile3 = (String)answer2.getSelectedItem();
    	}
    	
    	protected void answer1ActionPerformed(ActionEvent e) {
            JComboBox answer1 = (JComboBox)e.getSource();
            chosenfile2 = (String)answer1.getSelectedItem();
    	}
    
    	protected void button1ActionPerformed(ActionEvent evt) {
    		itemadd.addfile();
    	}
    
    	protected void button2ActionPerformed(ActionEvent evt, JComboBox answer7) {
    		chosenfile1 = (String) answer7.getSelectedItem();
    		openselectedfile();
    	}
    
    
    
    	private void openselectedfile() {
        	{ try
        	{
        		String program = "C:\\Windows\\System32\\notepad.exe";
        		Runtime.getRuntime().exec(program + " " + chosenfile1); 
        	}
        	catch(Exception exception)
        	{
        	exception.printStackTrace();
        	} }
    	}
    
    
    
    	protected void edit6ActionPerformed(ActionEvent evt, JButton edit6) {
        	{ try
        	{
        		String program = "C:\\Windows\\System32\\notepad.exe";
        		Runtime.getRuntime().exec(program + " " + "Characters.txt"); 
        	}
        	catch(Exception exception)
        	{
        	exception.printStackTrace();
        	} }
    	}
    
    
    
    
    
    	protected void edit5ActionPerformed(ActionEvent evt, JButton edit5) {
        	{ try
        	{
        		String program = "C:\\Windows\\System32\\notepad.exe";
        		Runtime.getRuntime().exec(program + " " + "Accountnames.txt"); 
        	}
        	catch(Exception exception)
        	{
        	exception.printStackTrace();
        	} }
    	}
    
    
    
    
    	protected void edit3ActionPerformed(ActionEvent evt, JButton edit3) {
        	{ try
        	{
        		String program = "C:\\Windows\\System32\\notepad.exe";
        		Runtime.getRuntime().exec(program + " " + "Itemnames.txt"); 
        	}
        	catch(Exception exception)
        	{
        	exception.printStackTrace();
        	} }
    	}
    
    
    
    	/**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
        private static void engineGUI() {
            // Create and set up the window.
            JFrame mainframe = new JFrame("Mule Handler PRO");
            mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainframe.add(new Engine());
            mainframe.pack();
            mainframe.setSize(400,200); // make frame 400x200
            mainframe.setResizable(false);
            mainframe.setVisible(true);
        }
         
        
        
        
        
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    engineGUI();
                }
            });
        }
        
        private String chosenfile1;
        static String chosenfile2;
        static String chosenfile3;
        static String chosenfile4;
        static String chosenfile5;
        static String chosenfile6;
        
        static JTextField answer4;
        
        String[] itemtype = {"Amulet", "Armor", "Belt", "Boots", "Charm", "Gloves", "Helmet", "Jewel", "Ring", "Rune", "Shield" ,"Weapon"};
        String[] itemstatus = {"Non-ethereal", "Ethereal"};
        String[] choosefile = {"Amulets.txt", "Armors.txt", "Belts.txt", "Boots.txt", "Charms.txt", "Gloves.txt", "Helmets.txt", "Jewels.txt", "Rings.txt", "Runes.txt", "Shields.txt" ,"Weapons.txt"};
    
    }







    itemadd.class
    Java Code:
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import javax.swing.JPanel;
    
    
    
    
    public class itemadd  extends JPanel {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    
    	public static void addfile() {
    		
    		// Amulet
    		if (Engine.chosenfile2 == "Amulet") {			
    			
    			// Amulet -> Non-ethereal
    			if (Engine.chosenfile3.equals("Non-ethereal")) { try {
        			out = new BufferedWriter(new FileWriter("Amulets.txt", true));
        			
        			
        			out.write("NON-Ethereal" + " " + "Name of the item" + " " + Engine.answer4.getText());
        			
        			
    	    		out.newLine();
    	    		out.close(); 
    	    		} catch (IOException e) { 
    	    			e.printStackTrace();
    				}} else {
    			
    			// Amulet -> Ethereal
    			try {
        			out = new BufferedWriter(new FileWriter("Amulets.txt", true));
        			out.write("Ethereal" + " " + "Name of the item" + " " + Engine.answer4.getText());
    	    		out.newLine();
    	    		out.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}}}
    		
    		
    		
    		
    		// Armor
    		if (Engine.chosenfile2 == "Armor") {
    			
    			// Armor -> Non-ethereal
    			if (Engine.chosenfile3 == "Non-ethereal") { try {
        			out = new BufferedWriter(new FileWriter("Armors.txt", true));
    				out.write("Non-ethereal Armor");
    	    		out.newLine();
    	    		out.close(); 
    	    		} catch (IOException e) { 
    	    			e.printStackTrace();
    				}} else {
    			
    			// Amulet -> Ethereal
    			try {
        			out = new BufferedWriter(new FileWriter("Armors.txt", true));
    				out.write("Ethereal Armor");
    	    		out.newLine();
    	    		out.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}}}
    		
    	
    
    }
    	
    	private static BufferedWriter out;
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,398
    Rep Power
    25

    Default Re: Calling a JTextField from another class

    gives me an Null Pointer Exception.
    Please post the full text of the error message. It gives the location of the error. Look at the line with the error and find the variable with the null value. Then backtrack in the code to see why that variable does not have a valid non-null value.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Mike. is offline Member
    Join Date
    Dec 2011
    Posts
    12
    Rep Power
    0

    Default Re: Calling a JTextField from another class

    Quote Originally Posted by Norm View Post
    Please post the full text of the error message. It gives the location of the error. Look at the line with the error and find the variable with the null value. Then backtrack in the code to see why that variable does not have a valid non-null value.
    Error message
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at itemadd.addfile(itemadd.java:26)
    	at Engine.button1ActionPerformed(Engine.java:205)
    	at Engine$1.actionPerformed(Engine.java:118)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown 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.mouseReleased(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(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown 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$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(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.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)

    itemadd.java:26
    Java Code:
        			out.write("NON-Ethereal" + " " + "Name of the item" + " " + Engine.answer4.getText());

    Engine.java:205
    Java Code:
    		itemadd.addfile();

    Engine.java:118
    Java Code:
            	button1ActionPerformed(evt);

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,398
    Rep Power
    25

    Default Re: Calling a JTextField from another class

    What variable on line 26 has the null value? If you can't tell, add a println that prints out the values of all the variables used on that line. Put the println after line 25.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Mike. is offline Member
    Join Date
    Dec 2011
    Posts
    12
    Rep Power
    0

    Default Re: Calling a JTextField from another class

    Quote Originally Posted by Norm View Post
    What variable on line 26 has the null value? If you can't tell, add a println that prints out the values of all the variables used on that line. Put the println after line 25.
    Variable "answer4", its the JTextField.
    If I delete the "Engine.answer4.getText()" part it does not give me null pointer exception.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,398
    Rep Power
    25

    Default Re: Calling a JTextField from another class

    Where do you give a value to variable: Engine.answer4?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Mike. is offline Member
    Join Date
    Dec 2011
    Posts
    12
    Rep Power
    0

    Default Re: Calling a JTextField from another class

    Quote Originally Posted by Norm View Post
    Where do you give a value to variable: Engine.answer4?
    By typing something into the JTextField when the application is running I assume.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,398
    Rep Power
    25

    Default Re: Calling a JTextField from another class

    Normally you give a value to a variable using an assignment statement. Where does the code give that variable a value?
    Currently its value is null.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Mike. is offline Member
    Join Date
    Dec 2011
    Posts
    12
    Rep Power
    0

    Default Re: Calling a JTextField from another class

    Theres no statements, other than:

    Java Code:
    answer4 = new JTextField();
    and
    Java Code:
    static JTextField answer4;
    Should there be something that keeps on track when I type something into the JTextField?

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,398
    Rep Power
    25

    Default Re: Calling a JTextField from another class

    Theres no statements, other than:
    Are you sure? Look at the code again. Have you changed it since you posted it here?
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Mike. is offline Member
    Join Date
    Dec 2011
    Posts
    12
    Rep Power
    0

    Default Re: Calling a JTextField from another class

    Quote Originally Posted by Norm View Post
    Are you sure? Look at the code again. Have you changed it since you posted it here?
    Yea I'm sure.
    I don't find any other statements regarding to answer4 than those what I posted already.
    And no, I haven't touched the code after posting it here.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,398
    Rep Power
    25

    Default Re: Calling a JTextField from another class

    Yea I'm sure.
    Look at line 27 in post#1
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Mike. is offline Member
    Join Date
    Dec 2011
    Posts
    12
    Rep Power
    0

    Default Re: Calling a JTextField from another class

    Quote Originally Posted by Norm View Post
    Look at line 27 in post#1
    Oops. I didn't see that one
    Should it say "final" too

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,398
    Rep Power
    25

    Default Re: Calling a JTextField from another class

    Should what say final?
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    Mike. is offline Member
    Join Date
    Dec 2011
    Posts
    12
    Rep Power
    0

    Default Re: Calling a JTextField from another class

    Quote Originally Posted by Norm View Post
    Should what say final?
    edit: aha so it was setting the variable's value to null?
    or at least it started working when I removed that line.

    Thanks a lot man :)
    Last edited by Mike.; 05-15-2012 at 12:06 AM.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,398
    Rep Power
    25

    Default Re: Calling a JTextField from another class

    The local variable in the constructor was shadowing the class variable with the same name. Your IDE should have warned you.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Calling a JTextField from another class

    Quote Originally Posted by Norm View Post
    .... Your IDE should have warned you.
    Sometimes you have to tweak settings in the IDE for this to occur. I know that you do with Eclipse since its default is to *not* warn for variable shadowing.

Similar Threads

  1. Replies: 8
    Last Post: 04-18-2012, 04:58 PM
  2. Replies: 3
    Last Post: 01-26-2012, 11:20 PM
  3. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  4. Replies: 3
    Last Post: 04-20-2009, 11:31 PM
  5. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 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
  •