变量在构造函数中定义,但抛出NullPoInterException

发布于 2025-01-29 08:16:29 字数 2588 浏览 1 评论 0原文

我是Java的新手,我正在练习 strings ,但是当我运行它时,它会引发nullpointeexception,

我定义了一个构造函数,该构造函数具有字符串S1,stopcodon,startcodon的值。

它的价值为S1,但不为Stopcodon,StartCodon
当我放置值时,它可以正常工作 请解释一下,以帮助我...

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{   
        String s1;
        String startCodon;
        String stopCodon;
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        //String s1;
        //String startCodon;
        //String stopCodon;
        Codechef test1 = new Codechef();
        test1.testSimpleGene();
    }
     public void testSimpleGene(){
           System.out.println("Gene Strand is = " + s1); 
           System.out.println("Gene1 is = " + findSimpleGene(s1));
           System.out.println("Gene2 is = " + findSimpleGene(s1,startCodon,stopCodon));
     }
     private String findSimpleGene(String dna,String x,String y){
           String dnaResult  = "";
           int startIndex = dna.toUpperCase().indexOf(x);
           if (startIndex == -1){
               return "";            
            }
           int stopIndex = dna.toUpperCase().indexOf(y,startIndex+3);
           if (stopIndex == -1){
               return "";            
            } 
           //System.out.println(startIndex +" Part2 "+ (stopIndex));   
            if((stopIndex - startIndex)%3 == 0){
            dnaResult = dna.substring(startIndex,stopIndex+3);
           }
           return dnaResult;
        }
        public String findSimpleGene(String dna){
           String dnaResult  = "";
           int startIndex = dna.indexOf(startCodon);
           if (startIndex == -1){
               return "";            
            }
            int stopIndex = dna.indexOf(stopCodon,startIndex+3);
           if (stopIndex == -1){
               return "";            
            } 
          //System.out.println(startIndex +" "+ (stopIndex));   
            if((stopIndex - startIndex)%3 == 0){
            dnaResult = dna.substring(startIndex,stopIndex+3);
        }
           return dnaResult;
        }
        Codechef(){
        String s1 = "taaatg";
        String startCodon = "TAA";
        String stopCodon  = "ATG";
        }
    }

错误

    at Codechef.findSimpleGene(Main.java:45)
    at Codechef.testSimpleGene(Main.java:24)
    at Codechef.main(Main.java:20)```

I am new in Java, I am practicing Strings but when i run this it throws NullPointeException

I have defined a constructor, which has value of String s1,stopCodon,startCodon.
it takes value of s1 but not stopCodon,startCodon
when i put value in instance variable it works fine
Please explain little bit so that it help me...

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{   
        String s1;
        String startCodon;
        String stopCodon;
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        //String s1;
        //String startCodon;
        //String stopCodon;
        Codechef test1 = new Codechef();
        test1.testSimpleGene();
    }
     public void testSimpleGene(){
           System.out.println("Gene Strand is = " + s1); 
           System.out.println("Gene1 is = " + findSimpleGene(s1));
           System.out.println("Gene2 is = " + findSimpleGene(s1,startCodon,stopCodon));
     }
     private String findSimpleGene(String dna,String x,String y){
           String dnaResult  = "";
           int startIndex = dna.toUpperCase().indexOf(x);
           if (startIndex == -1){
               return "";            
            }
           int stopIndex = dna.toUpperCase().indexOf(y,startIndex+3);
           if (stopIndex == -1){
               return "";            
            } 
           //System.out.println(startIndex +" Part2 "+ (stopIndex));   
            if((stopIndex - startIndex)%3 == 0){
            dnaResult = dna.substring(startIndex,stopIndex+3);
           }
           return dnaResult;
        }
        public String findSimpleGene(String dna){
           String dnaResult  = "";
           int startIndex = dna.indexOf(startCodon);
           if (startIndex == -1){
               return "";            
            }
            int stopIndex = dna.indexOf(stopCodon,startIndex+3);
           if (stopIndex == -1){
               return "";            
            } 
          //System.out.println(startIndex +" "+ (stopIndex));   
            if((stopIndex - startIndex)%3 == 0){
            dnaResult = dna.substring(startIndex,stopIndex+3);
        }
           return dnaResult;
        }
        Codechef(){
        String s1 = "taaatg";
        String startCodon = "TAA";
        String stopCodon  = "ATG";
        }
    }

Error

    at Codechef.findSimpleGene(Main.java:45)
    at Codechef.testSimpleGene(Main.java:24)
    at Codechef.main(Main.java:20)```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

摇划花蜜的午后 2025-02-05 08:16:29

更改构造函数,看起来像这样。

public Codechef(){
    s1 = "taaatg";
    startCodon = "TAA";
    stopCodon  = "ATG";
}

通过在每行的开头中拥有String的单词,您实际上是在构造函数中声明局部变量。您不想要那个,因为您想与对象中的字段一起工作,而不是与其他变量一起工作。

Change the constructor to look like this.

public Codechef(){
    s1 = "taaatg";
    startCodon = "TAA";
    stopCodon  = "ATG";
}

By having the word String at the beginning of each line, you're actually declaring local variables in your constructor. You don't want that, because you want to work with the fields in your object, not with additional variables.

怪我太投入 2025-02-05 08:16:29

如评论中所述,您正在定义构造函数中的新局部变量,这些变量恰好具有与类字段变量相同的名称。

在您的构造函数中,您应该分配类字段的值(s1startCodonstopcodon):

Codechef(){
     s1 = "taaatg";
     startCodon = "TAA";
     stopCodon  = "ATG";
}

但是,如果有一个本地变量与a field变量(对象的属性)相同的名称,您可以使用关键字 this

Codechef(){
     String s1 = "local variable";

     this.s1 = "taaatg";
     startCodon = "TAA";
     stopCodon  = "ATG";
}

As mentioned in the comments, you are defining new local variables in your constructor, that happen to have the same name as your class field variables.

In your constructor you should assign the values of your class fields (s1, startCodon, stopCodon):

Codechef(){
     s1 = "taaatg";
     startCodon = "TAA";
     stopCodon  = "ATG";
}

However, if there is a local variable having the same name as a field variable (the property of the object), you can reference the field variable with the keyword this:

Codechef(){
     String s1 = "local variable";

     this.s1 = "taaatg";
     startCodon = "TAA";
     stopCodon  = "ATG";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文