如何访问我的代码中的数据? - 变量可能尚未初始化

时间:2015-05-04 08:01:08

标签: java swing compiler-errors

我在阅读价值ba,fr,lit和mid时遇到了一些问题。我可以知道如何解决它。我试图将它们声明为全局变量,但它无济于事。请帮忙谢谢,下面是代码,错误发生在这一行(fileout.println(m +“,”+ l +“,”+ s +“,”+ u +“,”+ ba +“,”+ “,”+ fr +“,”+ lit +“,”+ mid);)

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        String m = model.getText();
        String l = line.getText();
        String s = shift.getText();
        String u = unitno.getText();
        String ba;
        String fr;
        String lit;
        String mid;
        try{
             Double b = Double.parseDouble(back.getText());
             if(b<350 || b>625){
                  back.setForeground(Color.BLUE);
                  back.setBackground(Color.YELLOW);
                 int dialogButton = JOptionPane.YES_NO_OPTION;
                 int dialogResult = JOptionPane.showConfirmDialog(this, "The value that you have entered is not within the range of 350 to 625. Press yes to save anyway and cancel to edit.", "Error", dialogButton);
                 if(dialogResult ==0){
                      ba = back.getText();
                 }
             }
             else{
                   ba = back.getText();
                  back.setForeground(Color.BLACK);
                  back.setBackground(Color.WHITE);
             }
        }catch(NumberFormatException  nfe){
            JOptionPane.showMessageDialog(null,"Please ensure that it is in integer.");
            back.setForeground(Color.BLUE);
            back.setBackground(Color.YELLOW);
        }
         try{
             Double f = Double.parseDouble(front.getText());
             if(f<350 || f>625){
                  front.setForeground(Color.BLUE);
                  front.setBackground(Color.YELLOW);
                 int dialogButton = JOptionPane.YES_NO_OPTION;
                 int dialogResult = JOptionPane.showConfirmDialog(this, "The value that you have entered is not within the range of 350 to 625. Press yes to save anyway and cancel to edit.", "Error", dialogButton);
                 if(dialogResult ==0){
                      fr = front.getText();
                 }
             }
             else{
                   fr = front.getText();
                  front.setForeground(Color.BLACK);
                  front.setBackground(Color.WHITE);
             }
        }catch(NumberFormatException  nfe){
            JOptionPane.showMessageDialog(null,"Please ensure that it is in integer.");
            front.setForeground(Color.BLUE);
            front.setBackground(Color.YELLOW);
        }
           try{
            Double li = Double.parseDouble(little.getText());
             if(li<0.8 || li>3.4){
                  little.setForeground(Color.BLUE);
                  little.setBackground(Color.YELLOW);
                 int dialogButton = JOptionPane.YES_NO_OPTION;
                 int dialogResult = JOptionPane.showConfirmDialog(this, "The value that you have entered is not within the range of 0.8 to 3.4. Press yes to save anyway and cancel to edit.", "Error", dialogButton);
                 if(dialogResult ==0){
                      lit = little.getText();
                 }
             }
             else{
                   lit = little.getText();
                  little.setForeground(Color.BLACK);
                  little.setBackground(Color.WHITE);
             }
        }catch(NumberFormatException  nfe){
            JOptionPane.showMessageDialog(null,"Please ensure that it is in integer.");
            little.setForeground(Color.BLUE);
            little.setBackground(Color.YELLOW);
        }
           try{
            Double mi = Double.parseDouble(middle.getText());
             if(mi<0.8 || mi>3.4){
                  middle.setForeground(Color.BLUE);
                  middle.setBackground(Color.YELLOW);
                 int dialogButton = JOptionPane.YES_NO_OPTION;
                 int dialogResult = JOptionPane.showConfirmDialog(this, "The value that you have entered is not within the range of 0.8 to 3.4. Press yes to save anyway and cancel to edit.", "Error", dialogButton);
                 if(dialogResult ==0){
                      mid = middle.getText();
                 }
             }
             else{
                   mid = middle.getText();
                  middle.setForeground(Color.BLACK);
                  middle.setBackground(Color.WHITE);
             }
        }catch(NumberFormatException  nfe){
            JOptionPane.showMessageDialog(null,"Please ensure that it is in integer.");
            middle.setForeground(Color.BLUE);
            middle.setBackground(Color.YELLOW);
        }
            BufferedWriter output = null;
         FileInputStream fs = null;
         FileWriter fout = null;
        try {
            // TODO add your handling code here:

            File myFile = new File("C:/Users/kai/Desktop/capforce.txt");
            if(!myFile.exists()) {
                myFile.createNewFile();
            } 
            fs = new FileInputStream("C:/Users/kai/Desktop/capforce.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fs));
            output = new BufferedWriter(new FileWriter(myFile,true));
            PrintWriter fileout = new PrintWriter(output,true);
            if (br.readLine() == null) {
                   fileout.println("Model" + " " + "Date" + " " + " " + "Line" + " " + "Shift" + " " + "Unit Number" + " "+ "Capped Force - Capped Z1 (Back)" + " " + "Capped Force - Capped Z1 (front)" + " " +"Wiper to pen interference Z(Little man)" + " " + "Wiper to pen interference Z(Middle man)" );
             }
            for(int i = 1; i<100; ++i){
                String line = br.readLine();
                if(line==null){
                   fileout.println(m + " , " + l + " , " + s + " , " + u + " , " + ba + " , " + " , " + fr + " , " + lit + " , " + mid );
                   break;
                }
            }        
        } catch (IOException ex) {
            Logger.getLogger(setup.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                output.close();
            } catch (IOException ex) {
                Logger.getLogger(setup.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

       this.dispose();
    }              

1 个答案:

答案 0 :(得分:3)

初始化所有变量:

    String ba = null;
    String fr = null;
    String lit = null;
    String mid = null;