Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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初始化变量错误_Java_Variables_Null_Initialization - Fatal编程技术网

Java初始化变量错误

Java初始化变量错误,java,variables,null,initialization,Java,Variables,Null,Initialization,我有一个错误,说我没有初始化buffer.write(变量)中的第一行、第二行和第三行变量变量为第一行、第二行和第三行的行。直到我在变量==JOptionPane.showInputDialog(“输入名称”)周围添加了while(number==null | | number.equals(“”),此错误才出现我的代码中的行。在保留添加的代码的同时,是否有任何方法可以处理此错误 import java.awt.Graphics; import javax.swing.JO

我有一个错误,说我没有初始化
buffer.write(变量)中的第一行、第二行和第三行变量变量为第一行、第二行和第三行的行。直到我在
变量==JOptionPane.showInputDialog(“输入名称”)周围添加了
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.awt.Graphics;
导入javax.swing.JOptionPane;
导入java.io.*;
导入java.io.FileWriter;
公共类CreateData{
公共静态void main(字符串[]args)
{
JOptionPane.showMessageDialog(null,
“此程序写入工资单数据”,
“欢迎”,JOptionPane。普通信息);
Write();
}
静态void Write()
{
试试{
字符串第一行,第二行,第三行,数字=”;
文件检查=新文件(“payroll.txt”);
文件编写器文件;
if(检查.exists())
file=newfilewriter(“payroll.txt”,true);
其他的
file=newfilewriter(“payroll.txt”);
BufferedWriter buffer=新的BufferedWriter(文件);
整数大小,计数=1;
while(number==null | | number.equals(“”)
{
number=JOptionPane.showInputDialog(“多少条记录?”);
}
size=Integer.parseInt(数字);
做{
while(number==null | | number.equals(“”)
{
firstLine=JOptionPane.showInputDialog(“输入名称”);//提示输入并显示“输入名称”
}
while(number==null | | number.equals(“”)
{
secondLine=JOptionPane.showInputDialog(“输入小时”);
}
while(number==null | | number.equals(“”)
{     
thirdLine=JOptionPane.showInputDialog(“输入工资”);
}
write(firstLine);//将firstLine变量写入payroll.txt文件
buffer.newLine();
缓冲区写入(第二行);
buffer.newLine();
缓冲区写入(第三行);
buffer.newLine();
计数++;
}而(计数此行

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

String firstLine;
String secondLine;
String thirdLine;
String number = " ";
因此,您需要初始化第一行、第二行、第三行:

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

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

int x;

while(false) {
    x = 10;
}

System.out.println(x);
由于不能保证您进入while循环,因此不能保证
x
已初始化

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

同样地,请初始化你的变量。这并不像你想象的那样初始化它们。事实上,它只初始化最后一个变量

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
:您也可以将它们初始化为任何其他
字符串。

添加
,同时在设置变量的位置循环,这意味着如果从未满足条件,变量将不会收到值

但是那些
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 = " ";
不将所有变量设置为
——仅将
编号设置为
变量。您需要分别为每个变量赋值


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

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