简体   繁体   English

字符串作为Java中的参数

[英]String as parameter in java

first of all I'm a beginner in java language , i wanted to test myself in a problem which is to encrypt a message given by the user , the class was as shown , unfortunately when i tried to use the class in Main class it gave me an Exception "in thread "main" java.lang.NullPointerException", 首先,我是Java语言的初学者,我想测试一下自己的问题,该问题是对用户给出的消息进行加密,该类如图所示,但是不幸的是,当我尝试使用它给出的Main class中的类时我一个异常“在线程“主” java.lang.NullPointerException”,

public class engToEnc {


    public String Message;
    public char []c = new char [Message.length()];



    public String readMessage(String pMessage)            
    {


        this.Message = pMessage;
        for(int i = 0 ; i < Message.length() ; i++)
        {
            c[i] = Message.charAt(i);
            c[i] += (char)27;
        }

        Message = String.copyValueOf(c);

        return Message;
    }

}

i tried to simplify the function to see the reason for the exception like that 我试图简化功能以查看类似异常的原因

public String readMessage(String pMessage)            
    {   
        this.Message = pMessage;

        return Message;
    }

but it also gave me the same exception so i did know that i have an issue with passing the string parameter, please help ! 但这也给了我同样的异常,所以我确实知道我在传递字符串参数时遇到问题,请帮忙!

The problem is with your initiation of char array. 问题在于您启动了char数组。 When you create a new engToEnc object, Java creates variable Message for that particular object. 创建新的engToEnc对象时,Java会为该特定对象创建变量Message。 And according to your code that is null(It's not initiated yet). 并且根据您的代码为null(尚未启动)。 Then Java tries to create the char array and create a new char arry of size of "Message". 然后,Java尝试创建char数组并创建一个大小为“ Message”的新char arry。 But the Message variable is null and it doesn't have property named length(). 但是Message变量为null,并且没有名为length()的属性。 So it gives you NullPointer Exception. 因此,它为您提供了NullPointer异常。 Try the code below. 试试下面的代码。 In there I initialize char array in the readMessage method after initiating Message variable. 在其中初始化Message变量后,在readMessage方法中初始化char数组。

    public class engToEnc {

    public String Message;
    public char[] c;

    public String readMessage(String pMessage) {

        this.Message = pMessage;
        c = new char[Message.length()];

        for (int i = 0; i < Message.length(); i++) {
            c[i] = Message.charAt(i);
            c[i] += (char) 27;
        }

        Message = String.copyValueOf(c);

        return Message;
    }

}

And please follow the naming conventions. 并且请遵循命名约定。 Your code is pretty confusing to look at. 您的代码看起来很混乱。

public class engToEnc { 
   public String message; // message not declared => messsage == null.
    //your are calling length() on message which are null. 
   public char []c = new char [message.length()]; 
    //instead do this:
   public String message;
   public char[] c;

   public String readMessage(String pMessage)            
   {
      //this.Message = pMessage;
      this.message = pMessage; // now message is declared, and we can call length().
      this.c = new char [message.length()];

      for(int i = 0 ; i < message.length() ; i++)
      {
         c[i] = message.charAt(i);
         c[i] += (char)27;
      }

      message = String.copyValueOf(c);

      return message;
   }
}

Here is the problem: 这是问题所在:

public String Message; // Message == null
public char []c = new char [Message.length()]; // NullPointerException

try this: 尝试这个:

public String Message;
public char []c;

then: 然后:

public String readMessage(String pMessage) {
    this.Message = pMessage;
    c = new char [Message.length()];
    //...

You have not allocated memory for Message data member 您尚未为Message数据成员分配内存

Do something like :- 做类似的事情:-

Message = new String();

in your constructor and then inside readMessage allocate memory for c[] . 在构造函数中,然后在readMessage内部为c[]分配内存。

Try the below code: 试试下面的代码:

public class engToEnc {


public String Message;
public char []c ;



public String readMessage(String pMessage)            
{


    this.Message = pMessage;
    this.c = new char [Message.length()]
    for(int i = 0 ; i < Message.length() ; i++)
    {
        c[i] = Message.charAt(i);
        c[i] += (char)27;
    }

    Message = String.copyValueOf(c);

    return Message;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM