【问题标题】:Java Initializing Variable ErrorJava 初始化变量错误
【发布时间】:2015-02-21 20:55:35
【问题描述】:

我有一个错误,提示我没有初始化buffer.write(variable); 行中的第一行、第二行和第三行变量,变量为第一行、第二行和第三行。直到我在代码中的 variable == JOptionPane.showInputDialog("Enter name"); 行周围添加 while(number == null || number.equals("")) 后,才出现此错误。有什么方法可以在保留我添加的代码的同时处理此错误?

    import java.awt.Graphics;   
    import javax.swing.JOptionPane;
    import java.io.*;
    import java.io.FileWriter; 

    public class CreateData {

    public static void main(String[] args)
    {

        JOptionPane.showMessageDialog(null, 
          "this program writes payroll data",
          "Welcome", JOptionPane.PLAIN_MESSAGE);

        Write();
    }

    static void Write()
    {
      try {  
        String firstLine, secondLine, thirdLine, number = " ";
        File check = new File("payroll.txt");  
        FileWriter file;
        if(check.exists()) 
          file = new FileWriter("payroll.txt", true);
        else
          file = new FileWriter("payroll.txt"); 

        BufferedWriter buffer = new BufferedWriter(file);
        int size, count = 1;
        while(number == null || number.equals("")) 
        {
        number = JOptionPane.showInputDialog("how many records?");
        }

        size = Integer.parseInt(number);

         do { 
          while(number == null || number.equals("")) 
          {
          firstLine = JOptionPane.showInputDialog("Enter name");// prompts for input and displays "Enter Name"
          }
          while(number == null || number.equals("")) 
          {
          secondLine = JOptionPane.showInputDialog("Enter hours");
          }
          while(number == null || number.equals("")) 
          {     
          thirdLine = JOptionPane.showInputDialog("Enter wage");
          }
          buffer.write(firstLine);//write firstLine variable to payroll.txt file
          buffer.newLine();
          buffer.write(secondLine);
          buffer.newLine();
          buffer.write(thirdLine);
          buffer.newLine();
          count++;

        }while(count <= size);

        buffer.close();//closes the data writing stream to payroll.txt file

        JOptionPane.showMessageDialog(null, "data processed",
        "Result", JOptionPane.PLAIN_MESSAGE );//display message "data processed"

        System.exit(1);
        }

      catch (IOException e) { System.out.println(e); }  


    }
    }

【问题讨论】:

    标签: java variables null initialization


    【解决方案1】:

    这一行

    String firstLine, secondLine, thirdLine, number = " ";
    

    等于

    String firstLine;
    String secondLine;
    String thirdLine;
    String number = " ";
    

    所以你需要初始化你的firstLine、secondLine、thirdLine:

    String firstLine = "";
    String secondLine = "";
    String thirdLine = "";
    String number = " ";
    

    【讨论】:

      【解决方案2】:

      在您设置变量的位置周围添加while 循环意味着如果条件从未满足,则变量将不会收到值。

      但是那些while 循环是错误的。一般来说,while 循环不应该有一个条件来等待它们内部没有改变的东西。由于number 是一个局部变量,因此没有其他线程会改变它,并且它不会在循环本身内改变:

            while(number == null || number.equals("")) 
            {
                firstLine = JOptionPane.showInputDialog("Enter name");// prompts for input and displays "Enter Name"
            }
      

      我很确定你真的想做出这个条件:

            while(firstLine == null || firstLine.equals("")) 
            {
                firstLine = JOptionPane.showInputDialog("Enter name");// prompts for input and displays "Enter Name"
            }
      

      所以你应该纠正它。尽管如此,编译器可能仍然对此不满意,因此您确实应该在声明变量时提供默认值,正如另一个答案告诉您的那样,声明:

      String firstLine, secondLine, thirdLine, number = " ";
      

      是否将所有变量设置为 " " - 只有 number 变量。您需要分别分配给它们中的每一个。

      您设置的值应该" "(一个空格)。因为这不满足任何一个条件(它不是 null 也不是空的)所以它不会进入循环,你会想知道为什么你只是得到空格。您应该将其设置为 null 或空字符串。

      【讨论】:

        【解决方案3】:

        让我用类似的问题回答你的问题:

        int x;
        
        while(false) {
            x = 10;
        }
        
        System.out.println(x);
        

        因为没有保证你进入while循环,所以没有保证x被初始化。

        同样的问题出现在您的代码中。即使你在逻辑上确信你会输入while loop,编译器也需要确定。

        因此,请初始化您的变量。这不会像您认为的那样初始化它们。事实上,它只初始化最后一个变量。

        String firstLine, secondLine, thirdLine, number = " ";
        

        试试这个:

        String firstLine, secondLine, thirdLine, number;
        number = " ";
        firstLine = null;
        secondLine = null;
        thirdLine = null;
        

        您也可以这样做:

        String firstLine, secondLine, thirdLine, number;
        number = " ";
        firstLine = secondLine = thirdLine = null;
        

        请注意,您不必将变量初始化为 null:您也可以将它们初始化为任何其他 String

        【讨论】:

          【解决方案4】:

          只需将三个变量初始化为null.. 字符串 firstLine=null, secondLine=null, thirdLine=null;

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-03-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多