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