1

我的程序在粗体部分出现空指针异常,我做错了什么?任何见解都会很好。谢谢你。关于我的一些背景,我是一名生物化学家,对编程非常陌生。如果在某处创建了一个空值,我该如何处理它并且不允许它终止并在程序上创建运行时错误。我究竟做错了什么?

    public class DNASequence {

//create a private static variable that can be accessed
private static String DNASequence;

public static void main(String[] args){

    DNASequence DNAStrandInput;
    DNAStrandInput = new DNASequence(DNASequence);

    System.out.println(DNAStrandInput);

     // Invoke the countLetters method to count each letter
    **int[] countsofDNA = countsLetters(DNASequence.toUpperCase());**

    // Display results
    for (int i = 0; i < countsofDNA.length; i++) {
      if (countsofDNA[i] != 0)
        System.out.println((char)('a' + i) + " appears  " +
          countsofDNA[i] + ((countsofDNA[i] == 1) ? " time" : " times"));
    }   

}

//Constructor Method that takes parameter a string and checks to see if its only A, T, C, G.
public DNASequence(String DNAStrand){

    DNASequence = DNAStrand;

    Scanner input = new Scanner(System.in);
    System.out.println("Please enter a sequence of DNA: ");
    String UserInputDNA = input.nextLine();

    boolean checkStrand = true;

    if (UserInputDNA.matches(".*A.*") && UserInputDNA.matches(".*C.*") && UserInputDNA.matches(".*T.*") && UserInputDNA.matches(".*G.*")){
        checkStrand = true;
    }

    else{
        checkStrand = false;
        System.err.println("You did not enter a valid sequence.");
    }




    //      // Invoke the countLetters method to count each letter
    //      int[] counts = countLetters(DNAStrand.toUpperCase());
    //
    //      // Display results
    //      for (int i = 0; i < counts.length; i++) {
    //        if (counts[i] != 0)
    //          System.out.println((char)('a' + i) + " appears  " +
    //            counts[i] + ((counts[i] == 1) ? " time" : " times"));
    //      }
    //    }
    //
    //    /** Count each letter in the string */
    //    public static int[] countLetters(String s) {
    //      int[] counts = new int[26];
    //
    //      for (int i = 0; i < s.length(); i++) {
    //        if (Character.isLetter(s.charAt(i)))
    //          counts[s.charAt(i) - 'a']++;
    //      }
    //
    //      return counts;
          }


// toString Method that just returns the stored sequence
public String toString(){
    return DNASequence;     
}

//Counts method that keeps track of how many of each appear in the DNA Strand
private static int[] countsLetters(String string){

          // Count each letter in the string    
        // I thought this was maybe easier but idk if it actually works..
            **int countA = DNASequence.indexOf('A');**
            int countC = DNASequence.indexOf('C');
            int countT = DNASequence.indexOf('T');
            int countG = DNASequence.indexOf('G');

            int []counts = new int [4];
                counts [0] = 'A' + countA;
                counts [1] = 'C'+ countC;
                counts [2] = 'T'+ countT;
                counts [3] = 'G'+ countG;           

            return counts;
}



private static boolean isSubsequenceOf(String DNAStrand){

    //if the second strand is smaller than the one that is supposed to be a     subsequence of, it cannot be true.
    if (DNAStrand.length() < DNASequence.length()){
        return false;
    }

    //I know how to do the substring but i dont know how to get it to be true or false. it won't let me do an if statement
    DNAStrand.indexOf(DNASequence);
    String subString = DNAStrand.substring(0, DNASequence.length());
    return true;
}


private static String[] dissolve(String letter){

    String [] dissolved;
    //if statement says that if the letter is either "a" "T" "C" or "G", to delete that character and print out the new string.
     if (letter == 'A' || 'T' || 'C'|| 'G'){
         DNASequence.split(letter);
     }



    return dissolved;





    }
    }

给我带来麻烦的代码行是:

int[] countsofDNA = countsLetters(DNASequence.toUpperCase());

int countA = DNASequence.indexOf('A');

编辑:

在构造函数中使用此代码使其工作。

    public DNASequence(String DNAStrand){

    DNASequence = DNAStrand;


    Scanner input = new Scanner(System.in);
    System.out.println("Please enter a sequence of DNA: ");
    DNASequence = input.nextLine();


    boolean checkStrand = true;

    if (DNASequence.matches(".*A.*") && DNASequence.matches(".*C.*") && DNASequence.matches(".*T.*") && DNASequence.matches(".*G.*")){
        checkStrand = true;
    }

    else{
        checkStrand = false;
        System.err.println("You did not enter a valid sequence.");
    }
4

4 回答 4

2

如果您查看声明,DNASequence您可以看到该字符串最初为空:

private static String DNASequence;

因此,在尝试对其调用任何方法之前,您需要确保设置DNASequence为非空字符串。

于 2012-11-29T03:54:14.707 回答
1
private static String DNASequence;

private static String DNASequence = null;

对于java来说是一样的

int[] countsofDNA = countsLetters(DNASequence.toUpperCase());

在这里你的 DNASequence 代表 null 因此你得到空指针异常。

因此你像这样调用构造函数DNASequence(null)和内部构造函数

null 再次分配给 DNASequence 变量。

于 2012-11-29T03:59:45.743 回答
0

您应该新建 DNASequence 的每个单元格(使用 for 循环)。有

DNAStrandInput = new DNASequence(DNASequence);

是不足够的。

于 2012-11-29T03:52:29.827 回答
0
private static String DNASequence;

你还没有初始化 DNASequence String 变量——你只是声明了它,所以它在你的程序中算作 null。您尝试使用 DNASequence,但它被设置为null,这就是您得到空指针异常的原因。

一个更好的方法是:

private static String DNASequence = "";

你得到一个空白字符串,你可以使用它而不用担心空指针异常,我认为这可以解决你的问题。希望有帮助!

于 2012-11-29T06:23:54.980 回答