1

プログラムの太字部分でヌル ポインター例外が発生していますが、何が間違っていますか? どんな洞察もいいでしょう。ありがとうございました。私は生化学者で、プログラミングは初めてです。どこかにnullが作成されている場合、どうすればそれを処理し、プログラムで終了してランタイムエラーを作成できないようにすることができますか. 私は何を間違っていますか?

    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見ると、文字列が最初は null であることがわかります。

private static String DNASequence;

DNASequenceそのため、メソッドを呼び出す前に、null 以外の文字列に設定する必要があります。

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

private static String DNASequence = null;

Javaでも同じです

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

ここで DNASequence は null を表しているため、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ため、Null Pointer Exception が発生しました。

これを行うより良い方法は次のとおりです。

private static String DNASequence = "";

Null Pointer Exceptionを恐れずに使用できる空白の文字列を取得します。これにより、問題が解決する可能性があります。それが役立つことを願っています!

于 2012-11-29T06:23:54.980 に答える