0

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",

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 !

1
  • 1
    Message has no value assigned, it is just a null pointer. Message.length() will cause a NullPointerException. Try Message="some value". BTW, naming convention says to name attributed starting in lower case: this.message instead of this.Message. The latter will confuse everyone reading your code,. Commented Sep 20, 2014 at 13:44

5 Answers 5

2

The problem is with your initiation of char array. When you create a new engToEnc object, Java creates variable Message for that particular object. And according to your code that is null(It's not initiated yet). Then Java tries to create the char array and create a new char arry of size of "Message". But the Message variable is null and it doesn't have property named length(). So it gives you NullPointer Exception. Try the code below. In there I initialize char array in the readMessage method after initiating Message variable.

    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.

Sign up to request clarification or add additional context in comments.

Comments

1
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;
   }
}

Comments

0

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()];
    //...

Comments

0

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;
  }
}

Comments

0

You have not allocated memory for Message data member

Do something like :-

Message = new String();

in your constructor and then inside readMessage allocate memory for c[].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.