Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 局部变量是从内部类访问的,需要声明为final_Java_Parsing_Intellij Idea_Compilation - Fatal编程技术网

Java 局部变量是从内部类访问的,需要声明为final

Java 局部变量是从内部类访问的,需要声明为final,java,parsing,intellij-idea,compilation,Java,Parsing,Intellij Idea,Compilation,我试图从源代码编译。然而,我遇到了一些错误 当我试图编译时,它给了我这些错误- Information:Using javac 1.8.0_101 to compile java sources Information:java: Errors occurred while compiling module 'BumpBot2' Information:4/10/2016 10:27 PM - Compilation completed with 18 errors and 0 warnings

我试图从源代码编译。然而,我遇到了一些错误

当我试图编译时,它给了我这些错误-

Information:Using javac 1.8.0_101 to compile java sources
Information:java: Errors occurred while compiling module 'BumpBot2'
Information:4/10/2016 10:27 PM - Compilation completed with 18 errors and 0 warnings in 1s 297ms
C:\Users\gabri\Desktop\BumpBot2\src\com\achow101\bumpbot\BumpBot.java
Error:(138, 30) java: local variable urlTextField is accessed from within inner class; needs to be declared final
Error:(139, 35) java: local variable bumpTextTextField is accessed from within inner class; needs to be declared final
Error:(144, 25) java: local variable errorText is accessed from within inner class; needs to be declared final
Error:(145, 25) java: local variable errorText is accessed from within inner class; needs to be declared final
Error:(149, 21) java: local variable errorText is accessed from within inner class; needs to be declared final
Error:(150, 21) java: local variable errorText is accessed from within inner class; needs to be declared final
Error:(172, 21) java: local variable errorText is accessed from within inner class; needs to be declared final
Error:(173, 21) java: local variable errorText is accessed from within inner class; needs to be declared final
Error:(253, 39) java: local variable grid is accessed from within inner class; needs to be declared final
Error:(256, 17) java: local variable urlTextField is accessed from within inner class; needs to be declared final
Error:(257, 17) java: local variable bumpTextTextField is accessed from within inner class; needs to be declared final
Error:(291, 64) java: local variable entry is accessed from within inner class; needs to be declared final
Error:(303, 17) java: local variable grid is accessed from within inner class; needs to be declared final
Error:(303, 43) java: local variable entryUrlLbl is accessed from within inner class; needs to be declared final
Error:(304, 17) java: local variable grid is accessed from within inner class; needs to be declared final
Error:(304, 43) java: local variable entryBumpTextLbl is accessed from within inner class; needs to be declared final
Error:(305, 17) java: local variable grid is accessed from within inner class; needs to be declared final
Error:(305, 43) java: local variable delBtn is accessed from within inner class; needs to be declared final
它们都与同一个文件BumpBot.java相关。 开发人员还提供了一个可工作的.jar文件,我已经使用过,并且100%可以工作。当我反编译它时,BumpBot.java输出是完全相同的

那么,我看不出有什么不对。我已经尝试在代码中将所有变量定义为
final
,它修复了我的大部分错误,但是它留下了关于
条目
网格
的变量的四个解析错误

我的Java安装是否已损坏

[人力资源]

编辑: 这就是问题中的代码。太长了,请耐心听我说

public class BumpBot extends Application {

    private int gridHeight = 0;
    private static DoBumps doBumps = new DoBumps();
    private static Thread t = new Thread(doBumps);

    public static void main(String[] args)
    {
        // Start do bumps thread
        t.start();
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception
    {

        // Set GUI Title
        primaryStage.setTitle("Bitcointalk Thread Bump Bot");

        // Create grid for entries
        GridPane grid = new GridPane();
        grid.setAlignment(Pos.TOP_CENTER);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(25, 25, 25, 25));

        // URL Heading lable
        Label urlLbl = new Label("Bitcointalk URL");
        grid.add(urlLbl, 0, 0);

        // New entry url textfield
        TextField urlTextField = new TextField();
        urlTextField.setPrefWidth(300);
        grid.add(urlTextField, 0, 1);
        gridHeight++;

        // Bump Text heading label
        Label bumpTextLbl = new Label("Bump Text");
        grid.add(bumpTextLbl, 1, 0);

        // New entry bump text textfield
        TextField bumpTextTextField = new TextField();
        bumpTextTextField.setPrefWidth(300);
        grid.add(bumpTextTextField, 1, 1);

        // Button to add entry
        Button addBtn = new Button("Add Bump Entry");
        grid.add(addBtn, 2, 1);

        // Error text
        Text errorText = new Text();
        grid.add(errorText, 1, 2);
        gridHeight += 2;

        // Populate the grid with pre-existing entries
        // Open a database connection
        // (create a new database if it doesn't exist yet):
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("bumps.odb");
        EntityManager em = emf.createEntityManager();

        // Get the next thread to bump
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<BumpEntry> qNextBump = cb.createQuery(BumpEntry.class);
        Root<BumpEntry> bump = qNextBump.from(BumpEntry.class);
        qNextBump.select(bump);
        TypedQuery<BumpEntry> query = em.createQuery(qNextBump);
        List<BumpEntry> bumpList = query.getResultList();
        for (BumpEntry entry : bumpList)
        {
            addEntryToGrid(entry, grid);
        }

        // Close the database connection:
        em.close();
        emf.close();

        // Add the entry when clicked
        addBtn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent e) {
                // Get the data from the form
                String url = urlTextField.getText();
                String bumpText = bumpTextTextField.getText();

                // Check the URL
                try {
                    if (!url.substring(0, 40).equals("https://bitcointalk.org/index.php?topic=")) {
                        errorText.setFill(Color.RED);
                        errorText.setText("Incorrect URL");
                        return;
                    }
                } catch (StringIndexOutOfBoundsException e1) {
                    errorText.setFill(Color.RED);
                    errorText.setText("Incorrect URL");
                    return;
                }

                // Trim the URL to just the topic
                String trimUrl = url.substring(0, 40);
                int dotIndex = url.indexOf(".", 40);
                trimUrl += url.substring(40, dotIndex);
                trimUrl += ".0";

                // Escape the bump text
                //escapeInput(bumpText);

                // Open a database connection
                // (create a new database if it doesn't exist yet):
                EntityManagerFactory emf = Persistence.createEntityManagerFactory("bumps.odb");
                EntityManager em = emf.createEntityManager();

                // Check that the thread is not already being bumped
                BumpEntry bEntry = em.find(BumpEntry.class, trimUrl);
                if (bEntry != null)
                {
                    errorText.setFill(Color.RED);
                    errorText.setText("Thread already being bumped");
                    return;
                }

                // Create the entry
                BumpEntry entry = new BumpEntry(trimUrl, bumpText, 0);

                // Check the date of the last post
                try {
                    // Get the first page of the thread
                    Document threadFirstPage = Jsoup.connect(url).get();

                    // Get the navpages elements
                    Element headerNavBar = threadFirstPage.select("div[id=bodyarea] > table[width=100%][cellspacing=0][cellpadding=0][border=0]").first();
                    Elements headerNavPages = headerNavBar.select("tr > td.middletext > a.navPages");

                    // Get the page if there is only one page of posts
                    if (headerNavPages.size() == 0) {
                        headerNavPages = headerNavBar.select("tr > td.middletext > b");
                    }

                    // Get the last page and calculate url number
                    Element lastPage = headerNavPages.last();
                    int pages = Integer.parseInt(lastPage.text());
                    int urlNum = (pages - 1) * 20;

                    // Create the URL for the last page
                    String lastPageUrl = url.substring(0, url.length() - 1);
                    lastPageUrl += urlNum;

                    // Get the last page of the thread
                    Document threadLastPage = Jsoup.connect(lastPageUrl).get();

                    // Get the last post in thread
                    Element postTable = threadLastPage.select("table[cellpadding=0][cellspacing=0][border=0][width=100%].bordercolor > tbody").first();
                    Element firstPost = postTable.select("tr").first();
                    String postClass = firstPost.className();
                    Elements posts = postTable.select("tr." + postClass);
                    Element lastPost = posts.last();

                    // Get the date of last post
                    Element headerAndPost = lastPost.select("td.td_headerandpost").first();
                    Element dateAndSubj = headerAndPost.select("table > tbody > tr > td[valign=middle]").get(1);
                    Element dateElem = dateAndSubj.select("div.smalltext").first();
                    String dateStr = dateElem.text();

                    // Parse date string and get unix timestamp
                    SimpleDateFormat fmt = new SimpleDateFormat("MMMM dd, yyyy, hh:mm:ss a");
                    fmt.setTimeZone(TimeZone.getTimeZone("UTC"));
                    Date date;
                    if (dateStr.contains("Today")) {
                        date = new Date();
                        String currentDateStr = fmt.format(date);
                        dateStr = dateStr.replace("Today at", currentDateStr.substring(0, currentDateStr.lastIndexOf(",") + 1));
                    }
                    date = fmt.parse(dateStr);
                    long unixtime = date.getTime() / 1000;
                    entry.setTime(unixtime);


                } catch (Exception e2) {
                    e2.printStackTrace();
                }

                // add entry to db
                em.getTransaction().begin();
                em.persist(entry);
                em.getTransaction().commit();

                // Notify the bumping thread
                synchronized (doBumps)
                {
                    doBumps.notify();
                }

                // Close the database connection:
                em.close();
                emf.close();

                // Add the entry to display
                addEntryToGrid(entry, grid);

                // Clear textfields
                urlTextField.clear();
                bumpTextTextField.clear();
            }
        });

        Scene scene = new Scene(grid, 1000, 800);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void addEntryToGrid(BumpEntry entry, GridPane grid)
    {
        // Thread URL
        TextField entryUrlLbl = new TextField();
        entryUrlLbl.setText(entry.getUrl());
        entryUrlLbl.setEditable(false);

        // Bump Text
        TextField entryBumpTextLbl = new TextField();
        entryBumpTextLbl.setText(entry.getBumpText());
        entryBumpTextLbl.setEditable(false);

        // Delete button
        Button delBtn = new Button("Remove");
        final int row = gridHeight;
        delBtn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                // Open a database connection
                // (create a new database if it doesn't exist yet):
                EntityManagerFactory emf = Persistence.createEntityManagerFactory("bumps.odb");
                EntityManager em = emf.createEntityManager();

                // Get the entry to be deleted
                BumpEntry bumpEntry = em.find(BumpEntry.class, entry.getUrl());

                // Remove entry from db
                em.getTransaction().begin();
                em.remove(bumpEntry);
                em.getTransaction().commit();

                // Close the database connection:
                em.close();
                emf.close();

                // Clear this row's grid
                grid.getChildren().remove(entryUrlLbl);
                grid.getChildren().remove(entryBumpTextLbl);
                grid.getChildren().remove(delBtn);

                // Notify the bumping thread
                synchronized (doBumps)
                {
                    doBumps.notify();
                }
            }
        });

        // Add to display
        grid.add(entryUrlLbl, 0, gridHeight);
        grid.add(entryBumpTextLbl, 1, gridHeight);
        grid.add(delBtn, 2, gridHeight);
        gridHeight++;
    }

    @Override
    public void stop()
    {
        System.exit(0);
    }

    public String escapeInput(String input) {
        String[] characters = {"\"", "\\", "{", "}"};
        StringBuilder sb = new StringBuilder();
        sb.append("\"");
        String line = input;
        for (String test : characters) {
            line = line.replace(test, "\\" + test);
        }
        sb.append("\"");
        return sb.toString();
    }
}
公共类BumpBot扩展应用程序{
私有int gridHeight=0;
私有静态DoBumps DoBumps=新的DoBumps();
私有静态线程t=新线程(doBumps);
公共静态void main(字符串[]args)
{
//开始做颠簸线程
t、 start();
发射(args);
}
@凌驾
public void start(Stage primaryStage)引发异常
{
//设置GUI标题
setTitle(“Bitcointalk线程Bump Bot”);
//为条目创建网格
GridPane grid=新建GridPane();
网格设置对齐(位置顶部\中心);
网格。setHgap(10);
网格设置间隙(10);
网格设置填充(新插图(25,25,25,25));
//URL标题标签
标签urlLbl=新标签(“Bitcointalk URL”);
add(urlLbl,0,0);
//新条目url文本字段
TextField urlTextField=新建TextField();
urlTextField.setPrefWidth(300);
add(urlTextField,0,1);
gridHeight++;
//凹凸文本标题标签
标签凹凸文本LBL=新标签(“凹凸文本”);
grid.add(bumpTextLbl,1,0);
//新条目凹凸文本文本字段
TextField bumptextfield=新建TextField();
bumpTextField.setPrefWidth(300);
添加(bumptextfield,1,1);
//按钮添加条目
按钮addBtn=新按钮(“添加凹凸条目”);
添加(addBtn,2,1);
//错误文本
Text errorText=新文本();
添加(errorText,1,2);
网格高度+=2;
//使用预先存在的条目填充网格
//打开数据库连接
//(如果数据库尚不存在,请创建新数据库):
EntityManagerFactory emf=Persistence.createEntityManagerFactory(“bumps.odb”);
EntityManager em=emf.createEntityManager();
//让下一个线程碰撞
CriteriaBuilder cb=em.getCriteriaBuilder();
CriteriaQuery qNextBump=cb.createQuery(BumpEntry.class);
Root bump=qNextBump.from(BumpEntry.class);
qNextBump.选择(通气);
TypedQuery query=em.createQuery(qNextBump);
List bumpList=query.getResultList();
用于(颠簸条目:颠簸列表)
{
addEntryToGrid(条目,网格);
}
//关闭数据库连接:
em.close();
emf.close();
//单击时添加条目
addBtn.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent e){
//从表单中获取数据
字符串url=urlTextField.getText();
字符串bumpText=bumpTextField.getText();
//检查URL
试一试{
如果(!url.substring(0,40).equals(“https://bitcointalk.org/index.php?topic=")) {
errorText.setFill(颜色为红色);
errorText.setText(“不正确的URL”);
回来
}
}捕获(StringIndexOutOfBoundsException e1){
errorText.setFill(颜色为红色);
errorText.setText(“不正确的URL”);
回来
}
//修剪URL,使其仅适合主题
String trimUrl=url.substring(0,40);
int dotIndex=url.indexOf(“.”,40);
trimUrl+=url.substring(40,点索引);
trimUrl+=“.0”;
//转义凹凸文本
//转义输入(文本);
//打开数据库连接
//(如果数据库尚不存在,请创建新数据库):
EntityManagerFactory emf=Persistence.createEntityManagerFactory(“bumps.odb”);
EntityManager em=emf.createEntityManager();
//检查螺纹是否已被碰撞
BumpEntry bEntry=em.find(BumpEntry.class,trimUrl);
if(bEntry!=null)
{
errorText.setFill(颜色为红色);
errorText.setText(“线程已被碰撞”);
回来
}
//创建条目
BumpEntry=新的BumpEntry(trimUrl,bumpText,0);
//检查最后一次发布的日期
试一试{
//获取线程的第一页
Document threadFirstPage=Jsoup.connect(url.get();
//获取navpages元素
元素headerNavBar=threadFirstPage。选择(“div[id=bodyarea]>table[width=100%][cellspacting=0][cellpadding=0][border=0]”。first();
Elements headerNavPages=headerNavBar.select(“tr>td.middletext>a.navPages”);
//如果只有一页文章,则获取该页
if(headerNavPages.size()==0){
headerNavPages=headerNavBar.选择(“tr>td.middletext>b”);
}
//获取最后一页并计算url编号
元素lastPage=headerNavPages.last