0

I have read several articles on how pass-by-value works, but I cannot call my serialized methods from my case statement, because I am not correctly passing my "club" variable. I understand that it outside of the scope of my "MainMenu" class, but I don't know the syntax to pass it to my "MainMenu" class.

I thought about passing it to my "MainMenu" constructor, which I have tried, but I then need to cast the type from object to String, and I am struggling with this.

How do I stop "club" from displaying "cannot find symbol" ? This happens in my Serialize case statements.

Here is my stack trace, I have also included the code snippets which are most pertinent.

MainMenu.java:19: error: cannot find symbol                                                                                                                          
    MainMenu team = new MainMenu(club);                                                                                                                              
                                 ^                                                                                                                                   
  symbol:   variable club                                                                                                                                            
  location: class MainMenu  
MainMenu.java:49: error: cannot find symbol                                                                                                                          
    saveclub1.Serialize(club , club_one);                                                                                                                            
                        ^                                                                                                                                            
  symbol:   variable club                                                                                                                                            
  location: class MainMenu                                                                                                                                           
MainMenu.java:49: error: cannot find symbol                                                                                                                          
    saveclub1.Serialize(club , club_one);                                                                                                                            
                               ^                                                                                                                                     
  symbol:   variable club_one                                                                                                                                        
  location: class MainMenu                                                                                                                                           
MainMenu.java:72: error: cannot find symbol                                                                                                                          
    saveclub2.Serialize(club , club_one);                                                                                                                            
                        ^                                                                                                                                            
  symbol:   variable club                                                                                                                                            
  location: class MainMenu                                                                                                                                           
MainMenu.java:72: error: cannot find symbol                                                                                                                          
    saveclub2.Serialize(club , club_one);  
symbol:   variable club_one                                                                                                                                        
  location: class MainMenu                                                                                                                                           
5 errors           

MainMenu.java snippet

String[] clubName = {"Arsenal","Aston Villa", "Bournemouth", "Chelsea", "Crystal Palace", "Everton", "Leicester City", "Liverpool", "Manchester United", "Manchester City", "Newcastle United", "Norwich City", "Southampton", "Stoke City", "Sunderland", "Swansea City", "Tottenham Hotspur", "Watford", "West Brom", "West Ham United"};

    String[] Menu = {"Choose Team", "Create Profile","Load Game", "Credits" ,    "Quit" , "Save Game" , "League Table"};

    public MainMenu(ClubInfo club){

    // clubName = club; // cast object club, to make it a String e.g clubName?
        //constructor
        }

    public static void main(String args[]){

        MainMenu team = new MainMenu(club);

    }

More from MainMenu.java

case 2: 
    Serialize saveclub1 = new Serialize();
    saveclub1.Serialize(club , club_one);
    exit = true;    
    break;

case 6:
    Serialize saveclub2 = new Serialize();
    saveclub2.Serialize(club , club_one);
    exit = true;
    break; 

Serialize.java

import java.util.InputMismatchException;
import java.util.Scanner;
import java.io.*;
import java.util.Arrays;

public class Serialize
{

    public Serialize(){
    //constructor
    }
   public void Serialize(ClubInfo club , Club1 club_one) // receive return type from printGreeting();
   {

      String fileName = club.teamName + ".ser";


      try {
         FileOutputStream fileOut = new FileOutputStream("/home/cg/root/" + fileName);
         ObjectOutputStream out = new ObjectOutputStream(fileOut);
         out.writeObject(club);
         out.close();
         fileOut.close();

      System.out.printf("Serialized data is saved in C:/tmp/club.ser");
      }catch(IOException i)
      {
          i.printStackTrace();
      }

   }
}

import java.util.InputMismatchException;
import java.util.Scanner;
import java.io.*;
import java.util.Arrays;
/**
 *
 * @author Darren Estcourt
 */

   public class ClubInfo implements java.io.Serializable{

    public ClubInfo(){
    //constructor
    }

   public String teamName;
   public String stadium;
   public String division;
   public transient int SSN;
   public int stadiumCapacity;

} // end ClubInfo class

-------------------------------------------------------------------------------

public class Club1{

    public Club1(){
    //constructor
    }


    public void club_one(ClubInfo club){

    club.teamName = "Arsenal";
      club.stadium = "Emirates";
      club.division = "Premier League";
      club.SSN = 11122333;
      club.stadiumCapacity = 60000;

      }

}

In short, how can I ensure "club" and "club_one" are recognised by my "MainMenu" class. Previously I have been relying on inheritence to pass values, but I have read that constructors and objects are what I might be looking for.

I am very much a newb, any help is greatly appreciated :)

nem035
  • 34,790
  • 6
  • 87
  • 99
  • "Cannot find symbol" on the stack trace, refers to "club" and "club_one". – Darren Estcourt Oct 26 '15 at 16:31
  • 1
    Did you create the clue and club_one's instance? – Kun Oct 26 '15 at 16:43
  • Yes - I have two of them, both like this. public class Club1{ public Club1(){ //constructor } public void club_one(ClubInfo club_one){ club_one.teamName = "Aston Villa"; club_one.stadium = "Villa Park"; club_one.division = "Premier League"; club_one.SSN = 11122334; club_one.stadiumCapacity = 40000; } } – Darren Estcourt Oct 26 '15 at 16:50
  • You have a method named `Serialize()` in a class named `Serialize` that isn't the constructor?? – dcsohl Oct 26 '15 at 16:51
  • I have just declared no argument constructors in all of my classes. Serialize, declares a class, a no argument constructor, and finally the method. Using the same name over and over is probably a bad idea, but I am a newb, I'll think of more meaningful names. – Darren Estcourt Oct 26 '15 at 16:53
  • @DarrenEstcourt I mean did you use syntax like this: Club1 club_1 = new Club1(), you should create this first, then put the reference in your method. I think – Kun Oct 26 '15 at 17:04
  • I have already done that, I didn't include that in the snippets. I just don't know how to pass my two values, I think it's via my MainMenu constructor... – Darren Estcourt Oct 26 '15 at 17:10

1 Answers1

0

The errors are in MainMenu, not Serialize. More specifically, these two lines:

saveclub1.Serialize(club , club_one);
saveclub2.Serialize(club , club_one);

You never declared any variables with the names club or club_one, but you tried to pass them as arguments to a function.


Your code has a few other problems that I think need to be addressed before you try to fix the one that is breaking your code.

Problem 1

"Serialize" is a verb, which is a sign that it should be a method, not a class. You could make it a method of ClubInfo. The syntax for saving your club then becomes:

ClubInfo myClub = new ClubInfo(arg1, arg2, arg3);
myClub.serialize();

Problem 2

You code confuses classes and instances. You made Club1 a class, and you made club_one a method. Club one should really be an instance of ClubInfo.

ClubInfo club1 = new ClubInfo();
club1.teamName = "Arsenal";
club1.stadium = "Emirates";

Problem 3

You rely on the caller to initialize variables. Initialization is the job of the constructor.

public ClubInfo (String teamName, String stadium) {
    this.teamName = teamName;
    this.stadium = stadium;
}

// Example call to the constructor
Club club1 = new Club("Arsenal", "Emirates");

Problem 4

You have a method called Serialize() in a class called Serialize. If you must have a Serialize class (see Problem 1 for why I think you shouldn't), rename it to Serializer.

Problem 5

If an exception occurs halfway through your try block, you will fail to close your streams. The correct way to close your streams is to initialize the streams before the try block, and close them in a finally block:

    FileOutputStream fileOut = null;
    try {
        fileOut = new FileOutputStream("");
        // Do something with the stream
    } catch(IOException i) {
        // Handle error
    } finally {
        try {
            if (fileOut != null) {
                fileOut.close();
            }
        } catch (IOException ex) {
            // Ignore exception
        }
    }

The new way is much shorter. This is called "try-with-resource"

    try (FileOutputStream fileOut = new FileOutputStream("")) {
        // Do something with the stream
    } catch(IOException i) {
        // Handle error
    }

You can extend this to multiple resources. There are examples of that here).

Community
  • 1
  • 1
Rainbolt
  • 3,542
  • 1
  • 20
  • 44