Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Baffling Android Error on Interactive Story App

This is the screen I am getting as an error. Though I've tried as well as possible to follow the video very exactly.

!https://gardenofiden.com/Screenshot%20from%20Android%20Studio.png "'.class' expected"

the code is

    private void loadPage(int pageNumber) {
        Page page = story.getPage(int pageNumber);

        Drawable image = ContextCompat.getDrawable(this, page.getImageId());
        storyImageView.setImageDrawable(image);

        String pageText = getString(page.getTextID());
        pageText = String.format(pageText, name);
        storyTextView.setText(pageText);
        choice1Button.setText(page.getChoice1().getTextId());
        choice2Button.setText(page.getChoice1().getTextId());
    }

the highlighted expression is pageNumber and the error is '.class' expected followed by another error reading ';' expected there is no mising ';' for sure. Is there a problem how I'm calling story.getPage ?

If there is more of the code you need me to add here please lmk.

2 Answers

remove 'int' inside the parenthesis.

Pages pages = story.getPage(pageNumber);

this will do it.

I took out the offending int and now my error has changed to:

Here is Screen shot: https://gardenofiden.com/Screenshot2020-09-16.png

It appears to be telling me I am giving arguments to new Choice where no arguments are required. Very puzzling. I'm definitely copying the video closely but getting weird errors.

the code for Choice.java is:

package com.idenrosenthal.interactivestory.model;

public class Choice {
    private int textId;
    private int nextPage;

    public int getTextId() {
        return textId;
    }

    public void setTextId(int textId) {
        this.textId = textId;
    }

    public int getNextPage() {
        return nextPage;
    }

    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }
}

and the code for Story.java where the Choice.java is called is:

package com.idenrosenthal.interactivestory.ui;

import com.idenrosenthal.interactivestory.R;
import com.idenrosenthal.interactivestory.model.Choice;
import com.idenrosenthal.interactivestory.model.Page;

public class Story {
    private Page []pages;

    public Story() {
        pages = new Page[7];

        pages[0] = new Page(R.drawable.page0 ,
                R.string.page0 ,
// 1st error here:
                new Choice(R.string.page0_choice1 , 1),
                new Choice(R.string.page0_choice2 , 2));

        pages[1] = new Page(R.drawable.page1,
                R.string.page1,
                new Choice(R.string.page1_choice1, 3),
                new Choice(R.string.page1_choice2, 4));

        pages[2] = new Page(R.drawable.page2,
                R.string.page2,
                new Choice(R.string.page2_choice1, 4),
                new Choice(R.string.page2_choice2, 6));

        pages[3] = new Page(R.drawable.page3,
                R.string.page3,
                new Choice(R.string.page3_choice1, 4),
                new Choice(R.string.page3_choice2, 5));

        pages[4] = new Page(R.drawable.page4,
                R.string.page4,
                new Choice(R.string.page4_choice1, 5),
                new Choice(R.string.page4_choice2, 6));

        pages[5] = new Page(R.drawable.page5, R.string.page5);

        pages[6] = new Page(R.drawable.page6, R.string.page6);
    }



    public Page getPage(int pageNumber) {
        if (pageNumber>=pages.length){
            pageNumber=0;
        }
        return pages[pageNumber];
    }
}