Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8899001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:45:02+00:00 2026-06-15T00:45:02+00:00

Having null pointer exception in my program at the bolded part, what am I

  • 0

Having null pointer exception in my program at the bolded part, what am I doing wrong? Any insight would be nice. Thank you. Some background on me, I am a biochemist, very new to programming. If there is a null created somewhere, how can I take care of it and not allow it to terminate and create a runtime error on the program. What am I doing wrong?

    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;





    }
    }

the lines of code causing me trouble are:

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

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

EDIT:

Got it to work using this code in the constructor.

    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.");
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:45:04+00:00Added an answer on June 15, 2026 at 12:45 am

    If you look at the declaration of DNASequence you can see that the string is initially null:

    private static String DNASequence;
    

    So you need to make sure to set DNASequence to a non-null string before you try to call any methods on it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

having problem with null pointer exception and i read few article bout that error
I am having trouble with java.util.LinkedList. I am getting a null pointer exception upon
I'm having trouble reproducing a bug where I get a null pointer exception when
I am having some trouble with the HashMaps in my program. I have several
I'm getting a null pointer exception when I do this: private String[] foo; private
Alright so I'm having a problem with some code that is generating a null
I'm having a problem where I receive this error: Exception in thread main java.lang.NullPointerException
I'm having a document.body is null error in my javascript because I use: $(window).width()
I'm having problems updating records to contain NULL values - in particular, a field
I am having an issue with NSDictionary returning null for an NSString even though

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.