Results 1 to 16 of 16
- 05-07-2012, 02:53 AM #1
Member
- Join Date
- May 2012
- Posts
- 12
- Rep Power
- 0
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); } } }Thanks in advance! :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; } }
- 05-07-2012, 02:56 AM #2
Member
- Join Date
- May 2012
- Posts
- 12
- Rep Power
- 0
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.
- 05-07-2012, 03:00 AM #3
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Returning A String Using A Method
Can you print the stack trace when you get the error?
- 05-07-2012, 03:03 AM #4
Member
- Join Date
- May 2012
- Posts
- 12
- Rep Power
- 0
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.
- 05-07-2012, 03:28 AM #5
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
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?
- 05-07-2012, 03:37 AM #6
Member
- Join Date
- May 2012
- Posts
- 12
- Rep Power
- 0
- 05-07-2012, 03:50 AM #7
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
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"; }
- 05-07-2012, 03:55 AM #8
Member
- Join Date
- May 2012
- Posts
- 12
- Rep Power
- 0
Re: Returning A String Using A Method
Yup same error...
Maybe it has something to do with how its called...
- 05-07-2012, 04:01 AM #9
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
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"); } }
- 05-07-2012, 04:04 AM #10
Member
- Join Date
- May 2012
- Posts
- 12
- Rep Power
- 0
Re: Returning A String Using A Method
Worked like a charm.
- 05-07-2012, 04:10 AM #11
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
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.
- 05-07-2012, 04:18 AM #12
Member
- Join Date
- May 2012
- Posts
- 12
- Rep Power
- 0
Re: Returning A String Using A Method
Last edited by Extraho Vinco; 05-07-2012 at 04:25 AM.
- 05-07-2012, 04:32 AM #13
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
- 05-07-2012, 04:44 AM #14
Member
- Join Date
- May 2012
- Posts
- 12
- Rep Power
- 0
Re: Returning A String Using A Method
Last edited by Extraho Vinco; 05-07-2012 at 04:48 AM.
- 05-07-2012, 04:46 AM #15
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Returning A String Using A Method
You are most welcome. I find that a different set of eyes can help in these situations.
- 05-07-2012, 10:23 AM #16
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
Re: Returning A String Using A Method
Well..... while i kind of read the explanation given...
... are you supposed to declare Listener 2 times?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); }
(lines 4 and 12)
Similar Threads
-
Returning get() method in put() method in a Hash Map
By Wpbops in forum New To JavaReplies: 1Last Post: 12-15-2011, 04:02 AM -
help with returning the string in reverse
By thorobred in forum New To JavaReplies: 10Last Post: 04-24-2011, 12:21 AM -
My method keeps returning 0
By ToolJob in forum New To JavaReplies: 11Last Post: 03-27-2011, 05:22 PM -
Intaking String and splitting into chars, returning individual chars as string array
By Gokul138 in forum New To JavaReplies: 1Last Post: 02-07-2011, 08:22 PM -
Returning Value from a method
By Mirix in forum New To JavaReplies: 12Last Post: 06-01-2010, 09:48 PM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks