简体   繁体   中英

How I do fix with IlegalStateException?

When I run my app and run my option 10 (NewBoatListActivity), the app crashes. According to the log, it is caused by java.lang.IllegalStateException. I have a feeling that it is caused by somewhere in the if else statement. If anyone know how to fix this, please look through my code below:

public void createList(View view)
{
    //Declare references
    BoatList boat_list;
    Boat newBoat;
    Boat newBoat2;
    Boat newBoat3;
    TextView tv;
    EditText et;
    EditText et2;
    String infilename;
    String urlfile;
    boolean file_problem = false;
    Scanner fsc = null;

    //Set references
    et = (EditText) findViewById(R.id.edit_infile);

    et2 = (EditText) findViewById(R.id.edit_urlfile);

    tv = (TextView) findViewById(R.id.text_main);

    //Get user input. Allows user to choose which way to get the file
    infilename = et.getText().toString();
    urlfile = et2.getText().toString();

    //Create the new boat object
    newBoat = new Boat();
    newBoat2 = new Boat();
    newBoat3 = new Boat();

    //Try to access and open the file in the app's assets directory
    //AssetManager assetManager = getAssets();
    try
    {
        AssetManager assetManager = getAssets();
        fsc = new Scanner(assetManager.open(infilename));

        //Show the file was successfully opened and read
        tv.setText("Infile was opened and read successfully!");

        //Close the file
        fsc.close();
    }
    catch(IOException e)
    {
        tv.setText("Error: File does not exist!");
        file_problem = true;
    }

    //If the file does exist in the assets directory, then read the data and 
    //input the data values to the Boat object
    if(!file_problem)
    {
        String make = fsc.nextLine();
        newBoat.setMake(make);

        String regist = fsc.nextLine();
        newBoat.setRegi(regist);

        int length = fsc.nextInt();
        fsc.nextLine();
        newBoat.setLength(length);

        int beam = fsc.nextInt();
        fsc.nextLine();
        newBoat.setBeam(beam);

        String fuel = fsc.nextLine();
        newBoat.setFuel(fuel);

        double price = fsc.nextDouble();
        fsc.nextLine();
        newBoat.setPrice(price);

        String pic_url = fsc.nextLine();
        newBoat.setPicURL(pic_url);

        String make2 = fsc.nextLine();
        newBoat2.setMake(make2);

        String regist2 = fsc.nextLine();
        newBoat2.setRegi(regist2);

        int length2 = fsc.nextInt();
        fsc.nextLine();
        newBoat2.setLength(length2);

        int beam2 = fsc.nextInt();
        fsc.nextLine();
        newBoat2.setBeam(beam2);

        String fuel2 = fsc.nextLine();
        newBoat2.setFuel(fuel2);

        double price2 = fsc.nextDouble();
        fsc.nextLine();
        newBoat2.setPrice(price2);

        String pic_url2 = fsc.nextLine();
        newBoat2.setPicURL(pic_url2);

        String make3 = fsc.nextLine();
        newBoat3.setMake(make3);

        String regist3 = fsc.nextLine();
        newBoat3.setRegi(regist3);

        int length3 = fsc.nextInt();
        fsc.nextLine();
        newBoat3.setLength(length3);

        int beam3 = fsc.nextInt();
        fsc.nextLine();
        newBoat3.setBeam(beam3);

        String fuel3 = fsc.nextLine();
        newBoat3.setFuel(fuel3);

        double price3 = fsc.nextDouble();
        fsc.nextLine();
        newBoat3.setPrice(price3);

        String pic_url3 = fsc.nextLine();
        newBoat3.setPicURL(pic_url3);

        //Close the file
        fsc.close();
    }

    //Access the list
    boat_list = BoatList.getInstance();

    //Erase all existing items in the BoatList and add new ones
    boat_list.clear();

    if(boat_list.isEmpty())
    {
        boat_list.add(boat_list.size(), newBoat);
        boat_list.add(boat_list.size(), newBoat2);
        boat_list.add(boat_list.size(), newBoat3);
    }

} //end of createList

You're reading from a file you've already closed. Don't close the file until you're done with it.

Also, in the future post the full stack trace. It gives a lot of information that makes debugging easier- line numbers and messages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM