Results 1 to 16 of 16
  1. #1
    Extraho Vinco is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default Returning A String Using A Method

    Hi, I am working on a Java Lab for school and trying to have it done before Monday, but I have hit a small problem.

    Basically I am trying to return a string after an action event using a method called from a separate class, but I am running into a runtime error no matter how I return it. Its driving me up a wall and I have been trying different things for an hour now.

    Here are my classes:

    Java Code:
    import javax.swing.JFrame;
    
    public class Driver08 {
    	
    	public static void main (String[] args) throws Exception
    	{
            JFrame frame = new JFrame("Unit4, Lab08: Dictionary");
            frame.setSize(300, 110);
            frame.setLocation(150, 100);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setContentPane(new Panel08());
            frame.setVisible(true);
    	}
    
    }
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    @SuppressWarnings("serial") // Suppression for an extension warning
    public class Panel08 extends JPanel {
    	
    	JLabel output;
    	Listener08 Listener;
    	public Panel08() throws Exception
    	{
    		setLayout(new BorderLayout());
    		add(new JLabel(" "), BorderLayout.NORTH);
    		output = new JLabel("-----", SwingConstants.CENTER);
    		add(output, BorderLayout.NORTH);
    
    		Listener08 Listener = new Listener08();
    		add(Listener, BorderLayout.CENTER);
    		
    		JPanel panel = new JPanel();
            panel.setLayout(new FlowLayout());
            addButton(panel, "Ok", new Listener1());
            addButton(panel, "Cancel", new Listener2());
            add(panel, BorderLayout.SOUTH);
    	}
    	
        private void addButton(JPanel p, String s, ActionListener a)
        {
           JButton b = new JButton(s);
           b.addActionListener(a);
           p.add(b);
        }
        
        private class Listener1 implements ActionListener
        {
           public void actionPerformed(ActionEvent e)
           {
        	   output.setText(Listener.checkWord());
           }
        }
        private class Listener2 implements ActionListener
        {
           public void actionPerformed(ActionEvent e)
           {
        	   System.out.println("Good-Bye.");
        	   System.exit(0);
           }
        }   
    }
    Java Code:
    import java.awt.GridLayout;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;
    
    @SuppressWarnings("serial") //Same as above
    public class Listener08 extends JPanel {
    
    	private JTextField input;
    	
    	public Listener08() throws Exception
    	{
    		setLayout(new GridLayout(1,1));
    		input = new JTextField();
    		add(new JLabel("Enter Word: ", SwingConstants.CENTER));
    		add(input);
    	}
    
    	public String checkWord() { //This Is supposed to check the string against an array, but the runtime error is preventing a return.
    		
    		String my_string = input.getText(); 
    		return my_string;
    		
    	}
    }
    Thanks in advance! :

  2. #2
    Extraho Vinco is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default Re: Returning A String Using A Method

    Actually, I think the error falls within the calling of checkWord(), I tried to print a line within that method to the console and nothing appeared.

  3. #3
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Returning A String Using A Method

    Can you print the stack trace when you get the error?

  4. #4
    Extraho Vinco is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default Re: Returning A String Using A Method

    Here you go!
    Defiantly broke something...

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Panel08$Listener1.actionPerformed(Panel08.java:39)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    	at java.awt.Component.processMouseEvent(Component.java:6375)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    	at java.awt.Component.processEvent(Component.java:6140)
    	at java.awt.Container.processEvent(Container.java:2083)
    	at java.awt.Component.dispatchEventImpl(Component.java:4737)
    	at java.awt.Container.dispatchEventImpl(Container.java:2141)
    	at java.awt.Component.dispatchEvent(Component.java:4565)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4619)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4280)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4210)
    	at java.awt.Container.dispatchEventImpl(Container.java:2127)
    	at java.awt.Window.dispatchEventImpl(Window.java:2482)
    	at java.awt.Component.dispatchEvent(Component.java:4565)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:684)
    	at java.awt.EventQueue.access$000(EventQueue.java:85)
    	at java.awt.EventQueue$1.run(EventQueue.java:643)
    	at java.awt.EventQueue$1.run(EventQueue.java:641)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    	at java.awt.EventQueue$2.run(EventQueue.java:657)
    	at java.awt.EventQueue$2.run(EventQueue.java:655)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:654)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Last edited by Extraho Vinco; 05-07-2012 at 03:09 AM.

  5. #5
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Returning A String Using A Method

    Yeah, it's throwing the exception at that line. What is the value of the JTextField input when you put it through the program?

  6. #6
    Extraho Vinco is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default Re: Returning A String Using A Method

    Quote Originally Posted by Wnt2bsleepin View Post
    Yeah, it's throwing the exception at that line. What is the value of the JTextField input when you put it through the program?
    "Test", I Have even tried instantiating a string within checkWord and returning that, but I'm still getting an exception thrown.

  7. #7
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Returning A String Using A Method

    Try this:

    Java Code:
      public String checkWord() { //This Is supposed to check the string against an array, but the runtime error is preventing a return.
             
            return "Hello World"; 
             
        }

  8. #8
    Extraho Vinco is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default Re: Returning A String Using A Method

    Yup same error...
    Maybe it has something to do with how its called...

  9. #9
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Returning A String Using A Method

    Ok, we know the error is probably not with the checkWord method. It could be what you said, but in case it's not, let's try this:

    Java Code:
     private class Listener1 implements ActionListener
        {
           public void actionPerformed(ActionEvent e)
           {
               output.setText("Hello Again Glorious World");
           }
        }

  10. #10
    Extraho Vinco is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default Re: Returning A String Using A Method

    Worked like a charm.

  11. #11
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Returning A String Using A Method

    Ignore what I said before about how the problem is not in checkWord. We were able to pass a String to the Label, but unable to get the String from the method even if we directly returned it. If I had to guess, it would be that because Listener1 is a class, Listener may have not been initialized to anything and is pointing to null.

  12. #12
    Extraho Vinco is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default Re: Returning A String Using A Method

    Quote Originally Posted by Wnt2bsleepin View Post
    Ignore what I said before about how the problem is not in checkWord. We were able to pass a String to the Label, but unable to get the String from the method even if we directly returned it. If I had to guess, it would be that because Listener1 is a class, Listener may have not been initialized to anything and is pointing to null.
    This is where my knowledge of java breaks down as this is becoming very abstract... ^_^
    But basically I gather that I did it wrong xD

    So, how can I "fix" it?

    EDIT: But wait isn't The Listener initialized by -
    Java Code:
    Listener08 Listener = new Listener08();
    Last edited by Extraho Vinco; 05-07-2012 at 04:25 AM.

  13. #13
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Returning A String Using A Method

    That's in a different class, so I don't think it passes through. I got it to work.

    Returning A String Using A Method-hellos.jpg

    If you can't access the variable Listener, how would you make a call to this method?

  14. #14
    Extraho Vinco is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default Re: Returning A String Using A Method

    Quote Originally Posted by Wnt2bsleepin View Post
    That's in a different class, so I don't think it passes through. I got it to work.

    Click image for larger version. 

Name:	HelloS.jpg 
Views:	4 
Size:	15.0 KB 
ID:	3772

    If you can't access the variable Listener, how would you make a call to this method?
    Excuse me while I go bash my head on my desk a couple times

    Java Code:
    output.setText(Listener08.checkWord());
    and
    public static String checkWord(){
    return "hello world";
    }
    Works now! Thanks for the help, it was greatly appreciated!
    Last edited by Extraho Vinco; 05-07-2012 at 04:48 AM.

  15. #15
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Returning A String Using A Method

    You are most welcome. I find that a different set of eyes can help in these situations.

  16. #16
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: Returning A String Using A Method

    Well..... while i kind of read the explanation given...
    Java Code:
    public class Panel08 extends JPanel {
         
        JLabel output;
        Listener08 Listener;
        public Panel08() throws Exception
        {
            setLayout(new BorderLayout());
            add(new JLabel(" "), BorderLayout.NORTH);
            output = new JLabel("-----", SwingConstants.CENTER);
            add(output, BorderLayout.NORTH);
     
            Listener08 Listener = new Listener08();
            add(Listener, BorderLayout.CENTER);
             
            JPanel panel = new JPanel();
            panel.setLayout(new FlowLayout());
            addButton(panel, "Ok", new Listener1());
            addButton(panel, "Cancel", new Listener2());
            add(panel, BorderLayout.SOUTH);
        }
    ... are you supposed to declare Listener 2 times?
    (lines 4 and 12)

Similar Threads

  1. Replies: 1
    Last Post: 12-15-2011, 04:02 AM
  2. help with returning the string in reverse
    By thorobred in forum New To Java
    Replies: 10
    Last Post: 04-24-2011, 12:21 AM
  3. My method keeps returning 0
    By ToolJob in forum New To Java
    Replies: 11
    Last Post: 03-27-2011, 05:22 PM
  4. Replies: 1
    Last Post: 02-07-2011, 08:22 PM
  5. Returning Value from a method
    By Mirix in forum New To Java
    Replies: 12
    Last Post: 06-01-2010, 09:48 PM

Posting Permissions

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