2013-03-16 18 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