2013-03-16 43 views
0

我得到一个错误有变量被零错误,当它不应该

Exception in thread "main" java.lang.NullPointerException 
at Proj5.main(Proj5.java:61) 

当运行线61程序(char z = placeHolder.charAt(counter);)我无法弄清楚什么是错的,并导致null

String answers = new String("112211324135412") ; 
    int k = 0 ;          
    int[] correct = new int[15] ;     
    String placeHolder = ("112211324135412") ; 
    for (int i = 1 ; i < tokens.length ; i+=2)  
    { 
     int counter = 0 ;       


     int amountCorrect = 0 ;      
     placeHolder = tokens[i] ; 

    while (counter < 15)        

    { 



     char z = placeHolder.charAt(counter) ; 
     char c = answers.charAt(counter) ; 
     if(z == c) 
     { 
      amountCorrect++ ; 
     } // end if 
     counter++ ;         

    } // end while  

    correct[k] = amountCorrect ;      
    k++ ; 


    } 

这是完整的代码。

/** * * * * * /

import java.util.* ; 
import java.io.* ; 

public class Proj5 
{ 

    public static void main(String[] args) throws IOException 
    { 

    Scanner s = new Scanner(System.in) ; 


/* This piece asks for the name of the file with the ids and answers and if it is not 
entered correctly it says invalid and makes the user try again. */ 

    String fileCheck ; 
    File file = null ; 
    do{ 
    System.out.print("Enter the name of the File: ") ; 
    fileCheck = s.nextLine() ; 
    file = new File(fileCheck) ; 
    if (!file.exists()) 
     System.out.println("ERROR, INVALID FILE") ; 
     }while (!file.exists()) ; 


/* This piece opens connections with the file, splits up each line, and then puts the 
pieces into an array (tokens). */ 

    String[] tokens = new String[100] ; 
    tokens= information(fileCheck) ; 

/* This piece contains the answers string and compares each character of answers to the 
odd numbered elements(answers from file) from the tokens array and keeps track of the 
number correct which is then placed into an array. */ 

    String answers = new String("112211324135412") ; 
    int k = 0 ;          // used to help store into correct array. 
    int[] correct = new int[15] ;     // stores amount correct can be used to compare to WID 
    String placeHolder = ("112211324135412") ; 
    for (int i = 1 ; i < tokens.length ; i+=2)  // adds two so it skips WID 
    { 
     int counter = 0 ;       // used to pull characters 
                //in the strings 

     int amountCorrect = 0 ;      // keeps track of amount correct 
     placeHolder = tokens[i] ; 

    while (counter < 15)       // goes through all characters in 
                //the string of answers 
    { 

        //puts answers into a string so 
                //individual answers can be compared 
     char z = placeHolder.charAt(counter) ; 
     char c = answers.charAt(counter) ; 
     if(z == c) 
     { 
      amountCorrect++ ; 
     } // end if 
     counter++ ;         // moves to next char 

    } // end while  

    correct[k] = amountCorrect ;     // stores amount correct in array for later use. 
    k++ ; 


    } // end for 



/* This piece takes "correct" array and loops it through to determine what percentage and 
what letter grade each elements would get and plugs each of those into their own array 
(percentage and letterGrade). */  


    int halfToken = (tokens.length/2) ;     // only need half the amount in tokens array since we dont need to include WID. 
    double[] percentage = new double[halfToken] ; 
    int a = 0 ;          //used to step through the 3 arrays 
    char[] letterGrade = new char[halfToken] ; 

    while(a < halfToken) 
    { 
    if(correct[a] == 15) 
    { 
     percentage[a] = 100.0 ; 
     letterGrade[a] = 'A' ; 
     a++ ; 
    } 
    else if(correct[a] == 14) 
    { 
     percentage[a] = 93.3 ; 
     letterGrade[a] = 'A' ; 
     a++ ; 
    } 
    else if(correct[a] == 13) 
    { 
     percentage[a] = 86.7 ; 
     letterGrade[a] = 'B' ; 
     a++ ; 
    } 
    else if(correct[a] == 12) 
    { 
     percentage[a] = 80.0 ; 
     letterGrade[a] = 'B' ; 
     a++ ; 
    } 
    else if(correct[a] == 11) 
    { 
     percentage[a] = 73.3 ; 
     letterGrade[a] = 'C' ; 
     a++ ; 
    } 
    else if(correct[a] == 10) 
    { 
     percentage[a] = 66.7 ; 
     letterGrade[a] = 'D' ; 
     a++ ; 
    } 
    else if(correct[a] == 9) 
    { 
     percentage[a] = 60.0 ; 
     letterGrade[a] = 'D' ; 
     a++ ; 
    } 
    else if(correct[a] == 8) 
    { 
     percentage[a] = 53.3 ; 
     letterGrade[a] = 'F' ; 
     a++ ; 
    } 
    else if(correct[a] == 7) 
    { 
     percentage[a] = 46.7 ; 
     letterGrade[a] = 'F' ; 
     a++ ; 
    } 
    else if(correct[a] == 6) 
    { 
     percentage[a] = 40.0 ; 
     letterGrade[a] = 'F' ; 
     a++ ; 
    } 
    else if(correct[a] == 5) 
    { 
     percentage[a] = 33.3 ; 
     letterGrade[a] = 'F' ; 
     a++ ; 
    } 
    else if(correct[a] == 4) 
    { 
     percentage[a] = 26.7 ; 
     letterGrade[a] = 'F' ; 
     a++ ; 
    } 
    else if(correct[a] == 3) 
    { 
     percentage[a] = 20.0 ; 
     letterGrade[a] = 'F' ; 
     a++ ; 
    } 
    else if(correct[a] == 2) 
    { 
     percentage[a] = 13.3 ; 
     letterGrade[a] = 'F' ; 
     a++ ; 
    } 
    else if(correct[a] == 1) 
    { 
     percentage[a] = 6.7 ; 
     letterGrade[a] = 'F' ; 
     a++ ; 
    } 
    else if(correct[a] == 0) 
    { 
     percentage[a] = 0.0 ; 
     letterGrade[a] = 'F' ; 
     a++ ; 
    } 

    } // end while 

/* This piece prints the header for id/#correct/%correct/grade and then cycles in a loop 
prints out each students info. */ 


    int f = 0 ; 
    int g = 0 ; 

    System.out.println("Student ID \t" + "# Correct \t" + "% Correct \t" + "Grade") ; 
    while(f < tokens.length) 
    { 
     System.out.println(tokens[f] + "\t" + correct[g] + "\t" + percentage[g] + "\t" + letterGrade[g]) ; 
     f+=2 ; 
     g++ ; 
    } // end while 

/* This piece finds adds up the percentage array and then divides by its length which is 
the class avg grade. It then uses this percentage to determine the letter grade. It 
lastly prints out the total average and letter grade. */  

    double totalAVG = 0.0 ; 
    char totalGrade = 'A' ; 

    while(f<percentage.length) // calculates average percent 
    { 
     totalAVG = (totalAVG+percentage[f]) ; 
    } // end while 

     totalAVG = totalAVG/percentage.length ; 


    if(totalAVG >= 90.0) 
    { 
     totalGrade = 'A' ; 
    } 
    else if (totalAVG >= 80.0 && totalAVG <=89.9) 
    { 
     totalGrade = 'B' ; 
    } 
    else if (totalAVG >= 70.0 && totalAVG <=79.9) 
    { 
     totalGrade = 'C' ; 
    } 
    else if (totalAVG >= 60.0 && totalAVG <=69.9) 
    { 
     totalGrade = 'D' ; 
     }  
     else 
      totalGrade = 'F' ; 

     System.out.println() ; 
     System.out.println("Average: " + totalAVG + "% (" + totalGrade + ")"); 

    /* This next piece cycles through the correct array to find the max and then takes that 
    times 2 to get high score, then it prints it out. */  


     int highScore = 0 ; 

     for(int h = 0; h<correct.length ; h++) 
     { 
      if(correct[h] >= highScore) 
      { 
       highScore = correct[h] ; 
      } 


     } // end for 

     System.out.println("High Score: " + highScore*2) ; 

    /* This piece cycles through the correct array to find the min and then takes that times 
    2 to get the low Score, then it prints it out. */  

     int lowScore = 0 ; 

     for(int h = 0; h<correct.length ; h++) 
     { 
      if(correct[h] <= lowScore) 
      { 
       lowScore = correct[h] ; 
      }  
     } // end for 

     System.out.println("Low Score: " + lowScore*2) ; 




    PrintWriter pw = new PrintWriter(new FileWriter("Result.txt")) ; 

     for (int t=0 ; t< tokens.length ; t+=2) 
     { 
      g = 0 ; 
      pw.println(tokens[t] + "," + correct[g] + percentage[g] + letterGrade[g]) ; 
      g++ ; 
     } 
      pw.println("Average: " + totalAVG + "% (" + totalGrade + ")") ; 
      pw.println("High Score: " + highScore*2) ; 
      pw.println("Low Score: " + lowScore*2) ; 

     pw.close() ;  


     } // end main 

    /* 
    * 
    * Opens connection to file with ids and answers and returns them split up. 
    * @param (String a) pulls in the filename for use in method 
    * @return Returns an array containing the split-up file. */ 




     public static String[] information(String a) throws IOException 
     { 
      Scanner inFile = new Scanner (new File(a)) ; // opens connection with file 
      String[] quarters = new String[100] ; 
      int index = 0 ; 
      while (inFile.hasNext())      // loops while more lines in file 
      { 

       String line = inFile.nextLine() ;   // brings in next line to be broken up 
       String[] array = line.split(",") ; 
       quarters[index] = array[0] ;   //stores lines into array tokens 
       index++ ; 
       quarters[index] = array[1] ; 
       index++ ; 
      }  
       inFile.close() ;       // close connection to file 
       return quarters ; 
     } // end information 



    } // end class 
+0

请使用IDE和调试器。 – Jayan 2013-03-16 01:53:18

+0

如果需要,我可以发布最新项目的完整问题我真的想弄明白并学习它,所以如果你找到解决方案的解释将是惊人的。我觉得我真的很接近完成它。再次感谢。 – 2013-03-16 01:54:02

+0

哪一行是61? – 2013-03-16 02:02:30

回答

1

我以前的答案是不正确的。我误解了你的代码。

问题是你正在从tokens阵列中得到null值,我只是没有正确地隔离原因。

你的代码开始时是这样的:

String[] tokens = new String[100] ; 
    tokens= information(fileCheck) ; 

第一行分配一个100元字符串数组...,第二行扔掉你刚分配的阵列(啊?),并用它替换由information方法创建新的一个。

information方法中,您将分配另一个100个元素的字符串数组,并使用从文件中读取的东西填充它。这是问题开始的地方。你看,如果你只读了30个标记,其余的70个元素将会是null

然后在main方法你这样做:

for (int i = 1 ; i < tokens.length ; i+=2)  // adds two so it skips WID 
{ 
    ... 
    placeHolder = tokens[i]; 
    ... 
    placeHolder.charAt(...) 

所以问题是,你是假设所有令牌阵列中不为空。但事实上,填充令牌的方法并不能保证填满所有插槽。

我会给你找出解决方案。 (这是你的功课!),但这里有几个想法:

  • 使用列表而不是数组的
  • 图了(在某种程度上),其中在阵列停止令牌。
+0

感谢您的帮助。有什么办法可以解决这个问题,所以它会采用方法之后的值而不是默认值(null)。 – 2013-03-16 02:35:54

+0

你给它分配了一些东西。我没有提出任何建议的原因是我无法弄清楚代码是什么*应该做的* – 2013-03-16 02:43:24

+0

代码需要一个文件,用逗号分隔的id(4563123,112211324135412)右边是回答测试。它读入一个最多50个ID的文件,我必须对它们进行评分并做一些计算。这有帮助吗? – 2013-03-16 02:51:14

相关问题