Hey guys! I am kind of new to Java and am having issues understanding why this code will not run. Any advise would be greatly appreciated! Thanks in advance!

// Phone.java
 // Program creates a GUI that resembles a phone with functionality.
 import java.awt.*;
 import java.awt.GridLayout;
 import java.awt.event.*;
 import javax.swing.*;

 public class Phone extends JFrame
 {
 private JButton keyJButton[];
 private JPanel keyJPanel;
 private JPanel lcdJPanel;
 private JTextArea lcdJTextArea;
 private String lcdOutput = "";
 private int count;


 // constructor sets up GUI
 public Phone()
 {
 super( "Phone" );



 lcdJTextArea = new JTextArea( 4, 15 );
 lcdJTextArea.setEditable( false );
 lcdJPanel.add( lcdJTextArea );

 keyJButton = new JButton[ 15 ];

 // initialize all digit key Buttons
 for ( int i = 3; i <= 11; i++ )
 keyJButton[ i ] = new JButton( String.valueOf( i - 2 ) );

 // initialize all non-digit key Buttons
 keyJButton[ 0 ] = new JButton( "Send" );
 keyJButton[ 1 ] = new JButton( "clr" );
 keyJButton[ 2 ] = new JButton( "End" );
 keyJButton[ 12 ] = new JButton( "*" );
 keyJButton[ 13 ] = new JButton( "0" );
 keyJButton[ 14 ] = new JButton( "#" );

 keyJButton[ 0 ].addActionListener(

 new ActionListener()
         {
 public void actionPerformed( ActionEvent e )
 {

 lcdOutput = "Calling...\n\n" + lcdOutput;
 lcdJTextArea.setText( lcdOutput );
 } // end method actionPerformed
 } // end new ActionListener
 ); // end addActionListener call



 keyJButton[ 1 ].addActionListener(

 new ActionListener()
 {
 public void actionPerformed( ActionEvent e )
 {
 if ( lcdOutput.length() == 0 ||
 lcdOutput.substring( 0, 1 ).equals( "C" ) )
 return;
 else
 {
 lcdOutput = lcdOutput.substring( 0, ( lcdOutput.length() - 1 ) );
 lcdJTextArea.setText( lcdOutput );
 } // end else
 } // end method actionPerformed
 } // end object ActionLstener
 ); // end addActionListener call

 keyJButton[ 2 ].addActionListener(

 new ActionListener()
 {
 public void actionPerformed( ActionEvent e )
 {
 lcdJTextArea.setText( " " );
 lcdOutput = "";
 } // end method actionPerformed
 } // end new ActionListener
 ); // end ActionListener call

 for ( int i = 3; i <= 14; i++ )
 {
 keyJButton[ i ].addActionListener(

 new ActionListener()
 {
 public void actionPerformed( ActionEvent e )
 {
 lcdOutput += e.getActionCommand();

 if ( lcdOutput.substring( 0, 1 ).equals( "C" ) )
 return;

 lcdJTextArea.append( e.getActionCommand() );
 } // end method actionPerformed
 } // end new ActionListener
 ); // end addActionListener call
 } // end for loop

 // set keyJPanel layout to grid layout
 keyJPanel = new JPanel();
 keyJPanel.setLayout( new GridLayout( 5, 3 ) );

 // add buttons to keyJPanel
 for ( int i = 0; i <= 14; i++ )
 keyJPanel.add( keyJButton[ i ] );

 // add components to container
 add( lcdOutput, BorderLayout.NORTH );
 } // end Phone constructor

    private void add(String lcdOutput, String NORTH) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
 } // end class Phone

 // PhoneTest.java
 // Program creates a GUI that resembles a phone with functionality.
import javax.swing.JFrame;

public class PhoneTest
 {
 // execute application
 public static void main( String args[] )
 {
 Phone application = new Phone();
 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
 application.setSize( 200, 300 );
 application.setVisible( true );
} // end main
 } // end class PhoneTest

I get this compilation error when trying to complie.

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
at Phone.<init>(Phone.java:29) * actually line 27 in this post
at PhoneTest.main(PhoneTest.java:8) * actually line 9 in this post

I'm sure this is a newbie error. Thanks all!

Edited 5 Years Ago by jtodd: n/a

That's a run time message generated when you try to run code that previously failed in compilation. What is the exact message that you get when you first compile it?

I don't know why you get that error. Are you using an IDE?

You should be getting a NPE because a variable at that line does not have a value.

I just saw the updated line number. Agree Norm - uninitialised variable -> NPE, but that's not the message you posted - puzzling!

Well... you are trying to run (line 27)
lcdJPanel.add( lcdJTextArea );
when you haven't created the lpcJPanel yet.
Defining it (line 12) doesn't create it.
lcdJPanel = new JPanel ();

The previous post was right about the first error. For the second one try this code

Phone application = new Phone();
application.runPhone();

then add a method in your phone like so

public void runPhone (){
Phone p = new Phone ();
}

That may fix the problem.

Thanks guys! I did not create a lcdJPanel as HFX642 suggested. Using lcdJPanel = new JPanel (); to create the lcdJPanel fixed the problem. Thanks all!

I got back around to working on this code again and now I am having issues at line 123 of Phone.java. I suspect I may have a syntax error somewhere but I am really not sure. Any direction in this issue would be greatly appreciated!

// Phone.java
 // Program creates a GUI that resembles a phone with functionality.
 import java.awt.*;
 import java.awt.GridLayout;
 import java.awt.event.*;
 import javax.swing.*;

 public class Phone extends JFrame
 {
 private JButton keyJButton[];
 private JPanel keyJPanel;
 private JPanel lcdJPanel;
 private JTextArea lcdJTextArea;
 private String lcdOutput = "";
 private int count;
 private BorderLayout layout;

 // constructor sets up GUI
 public Phone()
 {
 super( "Phone" );


 lcdJPanel = new JPanel ();
 lcdJTextArea = new JTextArea( 4, 15 );
 lcdJTextArea.setEditable( false );
 lcdJPanel.add( lcdJTextArea );

 keyJButton = new JButton[ 15 ];

 // initialize all digit key Buttons
 for ( int i = 3; i <= 11; i++ )
 keyJButton[ i ] = new JButton( String.valueOf( i - 2 ) );

 // initialize all non-digit key Buttons
 keyJButton[ 0 ] = new JButton( "Send" );
 keyJButton[ 1 ] = new JButton( "clr" );
 keyJButton[ 2 ] = new JButton( "End" );
 keyJButton[ 12 ] = new JButton( "*" );
 keyJButton[ 13 ] = new JButton( "0" );
 keyJButton[ 14 ] = new JButton( "#" );

 keyJButton[ 0 ].addActionListener(

 new ActionListener()
         {
 public void actionPerformed( ActionEvent e )
 {

 lcdOutput = "Calling...\n\n" + lcdOutput;
 lcdJTextArea.setText( lcdOutput );
 } // end method actionPerformed
 } // end new ActionListener
 ); // end addActionListener call



 keyJButton[ 1 ].addActionListener(

 new ActionListener()
 {
 public void actionPerformed( ActionEvent e )
 {
 if ( lcdOutput.length() == 0 ||
 lcdOutput.substring( 0, 1 ).equals( "C" ) )
 return;
 else
 {
 lcdOutput = lcdOutput.substring( 0, ( lcdOutput.length() - 1 ) );
 lcdJTextArea.setText( lcdOutput );
 } // end else
 } // end method actionPerformed
 } // end object ActionLstener
 ); // end addActionListener call

 keyJButton[ 2 ].addActionListener(

 new ActionListener()
 {
 public void actionPerformed( ActionEvent e )
 {
 lcdJTextArea.setText( " " );
 lcdOutput = "";
 } // end method actionPerformed
 } // end new ActionListener
 ); // end ActionListener call

 for ( int i = 3; i <= 14; i++ )
 {
 keyJButton[ i ].addActionListener(

 new ActionListener()
 {
 public void actionPerformed( ActionEvent e )
 {
 lcdOutput += e.getActionCommand();

 if ( lcdOutput.substring( 0, 1 ).equals( "C" ) )
 return;

 lcdJTextArea.append( e.getActionCommand() );
 } // end method actionPerformed
 } // end new ActionListener
 ); // end addActionListener call
 } // end for loop

 // set keyJPanel layout to grid layout
 keyJPanel = new JPanel();
 keyJPanel.setLayout( new GridLayout( 5, 3 ) );

 // add buttons to keyJPanel
 for ( int i = 0; i <= 14; i++ )
 keyJPanel.add( keyJButton[ i ] );
 
 
 Container container = getContentPane();
 layout = new BorderLayout();
 container.setLayout(layout);
 container.add( lcdOutput, BorderLayout.NORTH );
     // add components to container
import javax.swing.JFrame;

public class PhoneTest
 {
 // execute application
 public static void main( String args[] )
 {
 Phone application = new Phone();
 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
 application.setSize( 200, 300 );
 application.setVisible( true );
} // end main
 } // end class PhoneTest

I suspect I may have a syntax error somewhere

Does that mean we should wait until you finish editing the code and compile it?

If you are getting errors, copy and paste the full text of the error message here.

The code will not compile. Here is the error message:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
at Phone.<init>(Phone.java:123)
at PhoneTest.main(PhoneTest.java:8)
Java Result: 1

Any thoughts? Thanks in advance for your help!

lcdOutput is a String.
container.add(... can only add GUI components eg JLabels
So yes, line 123 is invalid.
You're doing something wrong here. The compiler will have given you an explicit error message about not being able to find a method add(String.. etc.
You must look for such messages and read them. There's no point trying to execute your program when there's a compile error - by that time the proper error message has gone and all you get is a generic "uncompilable code".

JamesCherrill you are right. Line 123 " container.add( lcdOutput, BorderLayout.NORTH ); " is giving me error message:

Cannot find symbol
Symbol: Method Add( Java.Lang.String.Java.lang.String)
Location: Class Java.awt.Container
Reached end of file while parsing

But I am not sure how to fix this. lcdOutput is a string. If I remove the string I still get the same error message. Am I missing something?

Read the API doc for the add() method you are using. The error message gives you the class.
It will tell you what are valid arguments for the add method.

Cannot find ... Method Add( Java.Lang.String.Java.lang.String)

Its looking for a method Add with two String parameters and it cannot find one. Look at the API doc for your container object and see what methods there are, and what parameters they need.

What are you trying to add to container? If you are trying to display some text you need to put that into a JLabel or somesuch.

I am trying to add components to a container. I'm not sure I am going about it the right way though. I looked up the API for container as NormR1 suggested and this is what I found.

add(String name, Component comp)
Adds the specified component to this container.

This is what I have isn't it? Thanks guys for helping out a newbie!

Read the next sentence of the API doc. It says:
This method is obsolete as of 1.1. Please use the method add(Component, Object) instead.

In your code is add's second argument a Component or a String?

container.add( lcdOutput, BorderLayout.NORTH );

Edited 5 Years Ago by NormR1: n/a

Very good point NormR1. BorderLayout.NORTH is a String. Needs to be an Object.... right?

What are you trying to do with the Container's add() method?

Needs to be an Object.... right?

What part of the API doc are you reading? What does it say?

I am trying, not very successfully to, set keyJPanel to grid layout, add buttons to keyJPanel and add the components to the container. Am I going about this wrong? Is there a better way to do this? I have posted the code for all of this below.

// set keyJPanel layout to grid layout
 keyJPanel = new JPanel();
 keyJPanel.setLayout( new GridLayout( 5, 3 ) );

 // add buttons to keyJPanel
 for ( int i = 0; i <= 14; i++ )
 keyJPanel.add( keyJButton[ i ] );


 Container container = getContentPane();
 layout = new BorderLayout();
 container.setLayout(layout);
 container.add( lcdOutput, BorderLayout.NORTH);
     // add components to container

This is what I read that made me think I needed an object. I am sooooo confused. I can't seem to make since on this.
"This method is obsolete as of 1.1. Please use the method add(Component, Object) instead."
http://download.oracle.com/javase/1,5.0/docs/api/

Please explain why you have the line with the error in your code.
What is that line supposed to do?

container.add( lcdOutput, BorderLayout.NORTH);

Why did you code it the way you did?
After reading the API doc, do you see what is required?

Do you know what a Component is? add(Component, Object)
Is lcdOutput a Component?

Edited 5 Years Ago by NormR1: n/a

This line is there to add the components to the container. A component is a button, label or a text box. lcdOutput is a String. Is there a better way to add components to the container I am missing?

NormR1 Thank you very much for you patience and help and pointing me in the right direction! I just figured out what you where trying to tell me. Here is the solution I came up with.

// set keyJPanel layout to grid layout
 keyJPanel = new JPanel();
 keyJPanel.setLayout( new GridLayout( 5, 3 ) );

 // add buttons to keyJPanel
 for ( int i = 0; i <= 14; i++ )
 keyJPanel.add( keyJButton[ i ] );


 Container container = getContentPane();
 layout = new BorderLayout();
 container.setLayout(layout);
 container.add( keyJPanel ,  BorderLayout.SOUTH    );
 container.add( lcdJPanel ,  BorderLayout.NORTH    );
     // add components to container

lcdOutput is a String.
You can't add a string to a Container or JPanel.
Make a JLabel out of the string.
Then add the JLabel to the Container.

He did not know why he had added that line because he never did. His assignment was to find that error and correct it.

This question has already been answered. Start a new discussion instead.