1

I'm having trouble with some java code. The program consists of about 7 files, but I will try to keep it short.

I'm trying to load an ArrayList from a file into a variable, with ObjectStream. It gave me a warning, because all the compiler could see, was that I said an Object should be casted to ArrayList. of course the compiler won't know what kind of object there is in the file. As the coder I know that the file can only consist of one ArrayList and nothing else. So I searched the web, and found out to supress the warning, nut now it give me the error:

Schedule.java:34: error: <identifier> expected

To give you a picture of what's happening, here is the code the error happens in. This error shouldn't be affected by any of the other classes

import java.util.*;
import java.io.*;


public class Schedule
{
    private static ArrayList<Appointment> schedule; 
    private static File file;

    private static ObjectInputStream objIn;
    private static boolean exit;
    private static Scanner in = new Scanner(System.in);

    public static void main(String[] args)
    {
        initializeSchedule();
        System.out.println("Welcome!");
        while(!exit){
            System.out.print("write command: ");
            Menu.next(in.next());
        }
    }

    public static void initializeSchedule()
    {
        try{
            file = new File("Schedule.ca");
            if(!file.exists()){
                schedule = new ArrayList<Appointment>();
            }
            else{
                objIn = new ObjectInputStream(new FileInputStream("Schedule.ca"));
                @SuppressWarnings("unchecked")
                schedule = (ArrayList<Appointment>)objIn.readObject();  
                objIn.close();
            } 
        } catch (IOException e){
            System.out.println("Exception thrown  :" + e);
        } catch (ClassNotFoundException e){
                System.out.println("Exception thrown  :" + e);
        }   
    }

    public static void exit()
    {
        exit = true;
    }

    public static ArrayList<Appointment> getSchedule()
    {
        return schedule;
    }

}

the error is in initializeSchedule, right under the supression, where schedule is set to the ObjectStream input.

1
  • It will work if you annotate the whole method. Commented Dec 8, 2013 at 10:44

2 Answers 2

2

The correct locations for @SuppressWarnings("unchecked") are

TYPE, FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE

So the compiler cannot parse @SuppressWarnings at this point, but considers it a statement. If you move it above the method declaration or above the declaration of schedule, it should be fine.

A better way to fix that is to actually correct the issue that the compiler is complaining about like this:

final Object input = objIn.readObject();
if (input instanceof ArrayList) {
  schedule = (ArrayList<Appointment>) input;
} else {
  throw new IllegalStateException(); // or whatever suits you
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can't annotate an assignment. Move the

@SuppressWarnings("unchecked")

to the line before the method starts.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.