Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

Help!  IncompatibleClassChangeError

Thread view: 
Luch - 22 Oct 2005 19:34 GMT
I'm doing a class assignment comprised of three modules: Airport.java,
AirportClient.java, and Pause.java (the code for all three is included
below.)  We were provided with the AirportClient and Pause compiled
modules (both *.java and *.class) and a partially completed Airport
module, and had to flesh out the Airport one (the required additions
are marked in the Airport code with '*****', five asterisks).  My
Airport code compiles fine, but when I run the AirportClient.java,
which calls the Airport and Pause modules, I get a series of errors
like this:

Exception in thread "AWT-EventQueue-0"
java.lang.IncompatibleClassChangeError

relating to the AirportClient.paint(AirportClient.java:80) method/line.
There are multiple error blocks, all the same, repeating this
information.

There is an additional error, same as above, but relating to the
AirportClient.workWithAirports method, line 42.  It is not liking the
reference to getCountAirports, which is an addition we had to make in
Airport.java.

If I try to recompile AirportClient, I get the error:

AirportClient.java:43: non-static method getCountAirports() cannot be
referenced from a static context
                    Airport.getCountAirports( ) );

my reference in Airport.java is:
   private static int countAirports = 0;

I also tried
   public static int countAirports = 0;

and
   static int countAirports = 0;

I know that to examine closely, the code below will have to copied in
and compiled and run, but if anyone could take the time to tell me
what's wrong I would appreciate it.

thanks,
Shaun

******************     Airport.java   *********************
public class Airport
{

 // instance variables
 private String airportCode;
 private int gates;

 // 1. ***** Add a static class variable *****
 //  countAirports is an int
 //  assign an initial value of 0
 public static int countAirports = 0;  // this line is my addition

 // 2. ***** Modify this method *****
 // Default constructor:
 // method name: Airport
 // return value:  none
 // parameters: none
 // function: sets the airportCode to an empty String
 //    ***** add 1 to countAirports class variable
 public Airport( )
 {
    airportCode = "";
    countAirports = countAirports + 1;  // this line is my addition

 }

 // 3. ***** Modify this method *****
 // Overloaded constructor:
 // method name: Airport
 // return value: none
 // parameters:  a String airport code and an int startGates
 // function: assigns airportCode the value of the
 //      startAirportCode parameter;
 //      calls the setGates method,
 //      passing the startGates parameter
 //    ***** add 1 to countAirports class variable
 public Airport( String startAirportCode, int startGates )
 {
    airportCode = startAirportCode;
    setGates( startGates );
    countAirports = countAirports + 1;  // this line is my addition

 }

 // Accessor method for the airportCode instance variable
 // method name: getAirportCode
 // return value: String
 // parameters: none
 // function: returns airportCode
 public String getAirportCode( )
 {
    return airportCode;
 }

 // Accessor method for the gates instance variable
 // method name: getGates
 // return value: int
 // parameters: none
 // function: returns gates
 public int getGates( )
 {
    return gates;
 }

 // 4. ***** Write this method *****
 // Accessor method for the countAirports class variable
 // method name: getCountAirports
 // return value: int
 // parameters: none
 // function: returns countAirports
    public int getCountAirports()  // this line is my addition
    {
        return countAirports;    // this line is my addition
    }

 // Mutator method for the airportCode instance variable
 // method name: setAirportCode
 // return value: void
 // parameters: String newAirportCode
 // function: assigns airportCode the value of the
 //                    newAirportCode parameter
 public void setAirportCode( String newAirportCode )
 {
    airportCode = newAirportCode;
 }

 // Mutator method for the gates instance variable
 // method name: setGates
 // return value:  void
 // parameters: int newGates
 // function: validates the newGates parameter.
 //   if newGates is greater than 0, sets gates to newGates;
 //   otherwise, prints an error message to System.err
 //   and does not change value of gates
 public void setGates( int newGates )
 {
    if ( newGates  >=  0 )
      gates = newGates;
    else
    {
      System.err.println( "Gates must be at least 0" );
      System.err.println( "Value of gates unchanged." );
    }
 }

 // 5. ***** Write this method *****
 // method name:  toString
 // return value: String
 // parameters: none
 // function:  returns a String that contains the airportCode
 //    and gates

    public String toString()  // this method is my addition
    {
        return "Airport Code: " + airportCode + "\n"
            + "Gate: " + gates;
    }

 // 6. ***** Write this method *****
 // method name: equals
 // return value: boolean
 // parameter:  Airport object
 // function:  returns true if airportCode
 //     and gates in this object
 //    are equal to those in the parameter object;
 //    returns false otherwise

    public boolean equals(Airport airportA)  // this method is my addition
    {
        if (airportCode.equals(airportA.airportCode)
            && gates==airportA.gates)
            return true;
        else
            return false;
    }

  }  // end of Airport class definition
***************** end of Airport.java *****************************

*****************  AirportClient.java  ****************************
// A client program to display Airport object values

import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JOptionPane;
import javax.swing.JFrame;

public class AirportClient extends JFrame
{
 String action1, action2;
 boolean firstTime = true;

 int animationPause = 6;     // 6 seconds between animations
 Airport airport1, airport2;  // declare Airport object references

 public void workWithAirports( )
 {
   animate( "Two airport object references declared:",
                  "Airport airport1, airport2;" );

   /* Instantiate airport1 using the overloaded constructor  */
   airport1 = new Airport( "IAD", 30 );
   animate( "Instantiated airport1 using overloaded constructor:",
                  "airport1 = new Airport( \"IAD\", 30 );" );

   /* Call toString() */
   animate( "Calling toString:",
                   "JOptionPane.showMessageDialog( null,
airport1.toString( ) );" );
   JOptionPane.showMessageDialog( null, airport1.toString( ) );

   /*  Instantiate a second airport object using overloaded
constructor*/
   airport2 = new Airport( "IAD", 30 );
   animate( "Instantiated airport2 using overloaded constructor:",
                  "airport2 = new Airport( \"IAD\", 30 );" );

   /* Get the value of countAirports */
   animate( "Getting the value of countAirports:",
                   "JOptionPane.showMessageDialog( null,
\"countAirports is \" +"
                           + "  Airport.getCountAirports( ) );" );
   JOptionPane.showMessageDialog( null, "countAirports is " +
                    Airport.getCountAirports( ) );

    /* Compare the two airport objects */
    animate( "Comparing airport1 and airport2 using the equality
operator ",
                   " if ( airport1 ==  airport2 )..." );
    if ( airport1 ==  airport2 )
       JOptionPane.showMessageDialog( null, "airport1 and airport2 are
equal" );
    else
       JOptionPane.showMessageDialog( null, "airport1 and airport2 are
not equal" );

    /* Compare the two Airport objects */
    animate( "Comparing airport1 and airport2 using equals:",
                      " if ( airport1.equals( airport2 ) )..." );
    if ( airport1.equals( airport2 ) )
       JOptionPane.showMessageDialog( null, "airport1 and airport2 are
equal" );
    else
       JOptionPane.showMessageDialog( null, "airport1 and airport2 are
not equal" );

    /* Finished */
    animate( "Actions are complete, exiting", "" );
    System.exit( 1 );
  }

 public AirportClient( )
 {
   super( "Using the Airport Class" );
   setSize( 520, 400 );
   setVisible( true );
 }

 public void paint( Graphics g )
 {
   super.paint( g );
   if ( firstTime )
     firstTime = false;
   else
   {
     int boxL = 75, boxH = 20;
     int sX = 50, sY = 50;

     // countAirports
     g.setColor( Color.BLACK );
     g.drawRect( sX, sY, boxL, boxH );
     g.drawString( "countAirports", sX, sY - 10 );
     g.setColor( Color.BLUE );
     g.drawString( Integer.toString( Airport.getCountAirports( ) ),
                           sX + 15, sY + 15 );

     // airport1
     sY = 125;
     if ( airport1 != null )
     {
         // object reference box
         g.setColor( Color.BLACK );
         g.drawRect( sX, sY, boxL, boxH );
         g.drawString( "airport1", sX, sY - 10 );
         draw( g, sX, sY, airport1 ); // draw airport object
     }
     else
     {
       // indicate null reference
       g.setColor( Color.BLACK );
       g.drawRect( sX, sY, boxL, boxH );
       g.drawString( "airport1", sX, sY - 10 );
       g.setColor( Color.BLUE );
       g.drawString( "null", sX + 15, sY + 15 );
     }

     sY = 250;
     if ( airport2 != null )
     {
         // object reference box
         g.setColor( Color.BLACK );
         g.drawRect( sX, sY, boxL, boxH );
         g.drawString( "airport2", sX, sY - 10 );
         draw( g, sX, sY, airport2 ); // draw airport object
      }
      else
      {
         // indicate null reference
           g.setColor( Color.BLACK );
           g.drawRect( sX, sY, boxL, boxH );
           g.drawString( "airport2", sX, sY - 10 );
           g.setColor( Color.BLUE );
         g.drawString( "null", sX + 15, sY + 15 );
      }

      // display action at bottom of screen
      g.setColor( Color.BLUE );
      g.drawString( action1, 15, 370 );
      g.drawString( action2, 20, 385 );
   }
 }

 private void draw( Graphics g, int sX, int sY, Airport a )
 {
   int boxL = 75, boxH = 20;

   // arrow
   g.setColor( Color.BLACK );
   g.drawLine( sX + boxL, sY + boxH / 2,
                sX + boxL + 25, sY + boxH / 2 );
   g.drawLine( sX + boxL + 25, sY + boxH / 2,
                sX + boxL + 25, sY + boxH * 2 );
   g.drawLine( sX + boxL + 25 - 5, sY + boxH * 2 - 5,
                sX + boxL + 25, sY + boxH * 2 );
   g.drawLine( sX + boxL + 25 + 5, sY + boxH * 2 - 5,
                sX + boxL + 25, sY + boxH * 2 );

   // airportCode
   g.setColor( Color.BLACK );
   g.drawString( "airport code", sX + boxL - 75, sY + 2 * boxH + 15 );
   g.drawRect( sX + boxL, sY + 2 * boxH, boxL, boxH );
   g.setColor( Color.BLUE );
   g.drawString( a.getAirportCode( ),
                        sX + boxL + 5, sY + 2 * boxH + 15);

   // gates
   g.setColor( Color.BLACK );
   g.drawString( "gates", sX + boxL - 75, sY + 3 * boxH + 15 );
   g.drawRect( sX + boxL, sY + 3 * boxH, boxL, boxH );
   g.setColor( Color.BLUE );
   g.drawString( Integer.toString( a.getGates( ) ),
                 sX + boxL + 5, sY + 3 * boxH + 15 );
 }

 private void animate( String a1, String a2 )
 {
   action1 = a1;
   action2 = a2;
   repaint( );
   Pause.wait( animationPause );
 }

 public static void main( String[] args )
 {
   AirportClient app = new AirportClient( );
   app.workWithAirports( );
   app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
 }
}
************  end of AirportClient.java  *****************

************ Pause.java  *********************************
public class Pause
{
 /** wait method
 *  @param seconds  number of seconds to pause
 */
 public static void wait( int seconds )
 {
    try
    {
      Thread.sleep( (int)( seconds * 1000 ) );
    }
    catch ( InterruptedException e )
    {
      e.printStackTrace( );
    }
 }
}
************ end of Pause.java  **********************
Ross Bamford - 22 Oct 2005 20:25 GMT
> I'm doing a class assignment comprised of three modules: Airport.java,
> AirportClient.java, and Pause.java (the code for all three is included
[quoted text clipped - 8 lines]
> Exception in thread "AWT-EventQueue-0"
> java.lang.IncompatibleClassChangeError

This could be related to your classes becoming of out sync. Try deleting  
all .class files, and compile everything again after changing the  
'getCountAirports()' method, making it static, to fix the compilation  
error you mentioned:

  // 4. ***** Write this method *****
  // Accessor method for the countAirports class variable
  // method name: getCountAirports
  // return value: int
  // parameters: none
  // function: returns countAirports
  public static int getCountAirports()
  //     ^^^^^^
  {
    return countAirports;
  }

You should then find it compiles. Now you can start work on the thread  
model :)

Signature

Ross Bamford - rosco@roscopeco.remove.co.uk

Andrew Thompson - 22 Oct 2005 20:35 GMT
> If I try to recompile AirportClient, I get the error:
>
[quoted text clipped - 10 lines]
> and
>     static int countAirports = 0;

That error is not referring to the declaration of the
countAirports attribute, but the getCountAirports( )
*method*.

Of course, any attribute used in a static method
will need to be local, passed in as an argument, or static
(I think that covers it).

HTHT
Luch - 22 Oct 2005 21:51 GMT
Ross, Andrew,

Well, that worked.  I changed the getCountAirports to static, it runs
like it should.  It's always the little details.....

thank you very much.
Shaun


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.