Reputation: 5
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 !
Upvotes: 0
Views: 124
Reputation: 54
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[]
.
Upvotes: 0
Reputation: 425
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;
}
}
Upvotes: 0
Reputation: 47
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;
}
}
Upvotes: 1
Reputation: 2510
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()];
//...
Upvotes: 0
Reputation: 1439
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.
Upvotes: 2