静态变量在main方法之外显示为null

时间:2022-11-16 08:23:25

Im building a lexical/syntex analyzer for class. The problem I am having is when I try to access my static variable "lexems" or "tokens" from a method besides main they are NULL. When I use them in main (such as the lex.printList method) they are fine and filled with data.

我正在为课堂建立一个词汇/ syntex分析器。我遇到的问题是当我尝试从除了main之外的方法访问我的静态变量“lexems”或“tokens”时它们是NULL。当我在main中使用它们(例如lex.printList方法)时,它们很好并且填充了数据。

Whats going on???

这是怎么回事???

import java.io.IOException;
import java.util.ArrayList;

public class SyntaxAnalyzer {
    public static int pos = 0;
    public static ArrayList<String> lexems = new ArrayList<String>();
    public static ArrayList<String> tokens = new ArrayList<String>();
    public static String nextToken;

    public static void main(String[] args) throws IOException {
        LexicalAnalysis lex = new LexicalAnalysis();
        lex.getFile();
        lex.parseText();
        ArrayList<String> lexems = lex.getLexems();
        lex.printList(lexems);
        ArrayList<String> tokens = lex.getTokens();
        lex.printList(tokens);
        //expr();
        lex();

    }


    static void lex(){
        //String lexem = lexems.get(pos);
        //System.out.println(lexem);
        nextToken = tokens.get(pos);
        pos++;
    }
}

4 个解决方案

#1


8  

You are overriding the lexems object with the local one so it is not static variable you are modifying inside main function.

你用本地的方法覆盖了lexems对象,所以它不是你在main函数中修改的静态变量。

To operate on the static one you should do

要对静态操作进行操作

    /*NOTHING HERE!!*/ lexems = lex.getLexems();
    lex.printList(lexems);
    ...

The same issue with tokens occurs

令牌出现同样的问题

    /*NOTHING HERE!!*/ tokens = lex.getTokens();
    lex.printList(tokens);
    ...

#2


1  

The problems are here:

问题在这里:

ArrayList<String> lexems = lex.getLexems();
lex.printList(lexems);
ArrayList<String> tokens = lex.getTokens();

In you main function you do not modify the static variables but local ones (local in the main function). Just change it to that:

在main函数中,不要修改静态变量,而应修改本地变量(主函数中的本地变量)。只需将其更改为:

lexems = lex.getLexems();
tokens = lex.getTokens();

#3


0  

You are creating another pair of variables in your main method, which happen to have same names as your static variables, and "overshadow" them within the scope of main method.

您正在main方法中创建另一对变量,这些变量恰好与静态变量具有相同的名称,并在main方法的范围内“掩盖”它们。

To fix it, you should not declare new variables, but initialise the existing ones:

要修复它,您不应声明新变量,而应初始化现有变量:

  public static void main(String[] args) throws IOException {
    LexicalAnalysis lex = new LexicalAnalysis();
    lex.getFile();
    lex.parseText();
    lexems = lex.getLexems();
    lex.printList(lexems);
    tokens = lex.getTokens();
    lex.printList(tokens);
    //expr();
    lex();
  }

#4


0  

This should help make the difference between the scopes used in your code :

这应该有助于区分代码中使用的范围:

public class MyClass{
    private static int myInt;
    public static void main(String[] args){
         int myInt = 6;
         printMyInt();
    }
    static void printMyInt(){ System.out.println(myInt); } // Prints 0 because uses the class field
}

#1


8  

You are overriding the lexems object with the local one so it is not static variable you are modifying inside main function.

你用本地的方法覆盖了lexems对象,所以它不是你在main函数中修改的静态变量。

To operate on the static one you should do

要对静态操作进行操作

    /*NOTHING HERE!!*/ lexems = lex.getLexems();
    lex.printList(lexems);
    ...

The same issue with tokens occurs

令牌出现同样的问题

    /*NOTHING HERE!!*/ tokens = lex.getTokens();
    lex.printList(tokens);
    ...

#2


1  

The problems are here:

问题在这里:

ArrayList<String> lexems = lex.getLexems();
lex.printList(lexems);
ArrayList<String> tokens = lex.getTokens();

In you main function you do not modify the static variables but local ones (local in the main function). Just change it to that:

在main函数中,不要修改静态变量,而应修改本地变量(主函数中的本地变量)。只需将其更改为:

lexems = lex.getLexems();
tokens = lex.getTokens();

#3


0  

You are creating another pair of variables in your main method, which happen to have same names as your static variables, and "overshadow" them within the scope of main method.

您正在main方法中创建另一对变量,这些变量恰好与静态变量具有相同的名称,并在main方法的范围内“掩盖”它们。

To fix it, you should not declare new variables, but initialise the existing ones:

要修复它,您不应声明新变量,而应初始化现有变量:

  public static void main(String[] args) throws IOException {
    LexicalAnalysis lex = new LexicalAnalysis();
    lex.getFile();
    lex.parseText();
    lexems = lex.getLexems();
    lex.printList(lexems);
    tokens = lex.getTokens();
    lex.printList(tokens);
    //expr();
    lex();
  }

#4


0  

This should help make the difference between the scopes used in your code :

这应该有助于区分代码中使用的范围:

public class MyClass{
    private static int myInt;
    public static void main(String[] args){
         int myInt = 6;
         printMyInt();
    }
    static void printMyInt(){ System.out.println(myInt); } // Prints 0 because uses the class field
}