Getting a null pointer exception.
Okay, I'm still learning programming, and I have this assignment where I have to create a class Message, which 4 instance variables: sender, receiver, subject and body. I have to implement a constructor that takes four parameters and initializes the attributes accordingly.
I have to implement a method isValid() that returns true only if: the Message object on which the method is invoked has a non-empty sender and receiver, and at least one of the body or subject must be a non-empty string.
Along with a toString() method. I understand all this, and here is my code so far. I have no idea what I am doing wrong.
Code Java:
public class Message
{
String sender;
String receiver;
String subject;
String body;
public Message(String sender, String receiver, String subject, String body)
{
this.sender = sender;
this.receiver = receiver;
this.subject = subject;
this.body = body;
}
public Message()
{
this.sender = null;
this.receiver = null;
this.subject = null;
this.body = null;
}
public static void main (String[] args)
{
Message trying = new Message();
trying.sender = "sendername";
trying.receiver = null;
trying.subject = "Hi";
trying.body = null;
System.out.println(trying);
System.out.println(trying.isValid());
}
public boolean isValid ()
{
boolean flag = false;
if (sender.length() > 0 && receiver.length() > 0)
flag = true;
if(subject.length() >0 || body.length()>0)
flag = true;
else
flag = false;
return flag;
}
public String toString()
{
return ("From: " + sender + "\n" + "To: " + receiver + "\n" + "Subject: " + subject + "\n" + "Body: " + body);
}
}
When I run this, I get a runtime exception (it compiles, but when I run) I get this:
Exception in thread "main" java.lang.NullPointerException
at Message.isValid(Message.java:40)
at Message.main(Message.java:33)
Re: Getting a null pointer exception.
Something is null on that line listed in the Exception stack trace...step through the code and think about what is set to null (null is just that - you cannot call methods on a null reference)
Re: Getting a null pointer exception.
Yeah, I understand that a null pointer exception is that when I try to invoke a method on a non-existing object, but I created my object, and when I output it, it works ...
Nevermind, I changed that isValid() method to the following and it works:
Code Java:
public boolean isValid ()
{
if (this.sender.length() > 0 && this.receiver.length() >0)
flag = true;
if (this.subject.length() < 0 || this.body.length() <0 )
flag = false;
return flag;
}
of course I added,
Code Java:
static boolean flag = false;
to my instance variables.
Re: Getting a null pointer exception.
To further elaborate on copeg's post, the .length() method throws a Null Pointer when you invoke it on a null. I'm not entirely sure what line 40 is, but this is an indication that either sender and/or receiver OR subject and/or body is null.
Hopefully that will help you narrow down your search.
Re: Getting a null pointer exception.
Hello Ouzi!
When you get an NPE you should print the variables in the line that the exception tells you to find which of them is null, and then you can find out why it is null.
Here the problem is that you are trying to get receiver.length(), but receiver is null (i.e. you are trying to take an attribute of something that doesn't exist). Therefore a NPE is thrown.
In your case, you can test if the receiver (or sender) is null because -I repeat in your case- a 0-length reciever is the same as no receiver (null).
Hope it helps.
Re: Getting a null pointer exception.
Quote:
Originally Posted by
andreas90
Hello Ouzi!
When you get an NPE you should print the variables in the line that the exception tells you to find which of them is null, and then you can find out why it is null.
Thank you so much for this, I will take that into consideration if I ever face a NPE. However when I updated the isValid() method, the exception was gone, which is weird. I used the same technique more or less. Comparing sender.length() while being empty and it ran and gave a correct output.
Re: Getting a null pointer exception.
Quote:
Nevermind, I changed that isValid() method to the following and it works:
Based upon the initial code you posted that should still throw the exception as you set receiver to null, then call length() on that reference, so I suspect that is not all that changed
Re: Getting a null pointer exception.
That's the only thing I have changed, the code isn't working 100% as intended, still trying to figure out why ... Thing is, the method isValid() should check if the variable is empty or not, if its empty it should return false, and if its not empty it should return true.
Re: Getting a null pointer exception.
Quote:
Originally Posted by
Ouzi
Thank you so much for this, I will take that into consideration if I ever face a NPE. However when I updated the isValid() method, the exception was gone, which is weird. I used the same technique more or less. Comparing sender.length() while being empty and it ran and gave a correct output.
If the updated isValid() method is the one from post#3, I think it will still throw a NPE, unless you gave the reciever a non null value. I told you that in your case a null sender is the same with a 0-length one, because there can't be a number with no digits. But generally a 0-length String is different than a null String. Try the following;
Code java:
public class Test {
public static void main(String[] args) {
String s = "";
String st = null;
System.out.println(s.equals(st));
}
}
Re: Getting a null pointer exception.
Code Java:
public class Message
{
String sender;
String receiver;
String subject;
String body;
static boolean flag = false;
public Message(String sender, String receiver, String subject, String body)
{
this.sender = sender;
this.receiver = receiver;
this.subject = subject;
this.body = body;
}
public static void main (String[] args)
{
Message one = new Message("Slim","Georg","","");
System.out.println(one);
System.out.println("\n");
System.out.println(one.isValid());
}
public boolean isValid ()
{
if (this.sender.length() > 0 & this.receiver.length() >0)
{
if (this.subject.length() > 0 || this.body.length() > 0 )
flag = true;
}
else
flag = false;
return flag;
}
public String toString()
{
return ("From: " + sender + "\n" + "To: " + receiver + "\n" + "Subject: " + subject + "\n" + "Body: " + body);
}
}
The output is as follows:
--------------------Configuration: <Default>--------------------
From: Slim
To: Georg
Subject:
Body:
false
Process completed.
Also: System.out.print(one.sender.length()); Outputs: 4.
Re: Getting a null pointer exception.
If I were you, I would first check for null and then length() > 0, to avoid a NPE.
And also I would use the logical && instead of the bitwise & operator. They seem to work the same but they are quite different.
Re: Getting a null pointer exception.
Quote:
Originally Posted by
andreas90
If I were you, I would first check for
null and then
length() > 0, to avoid a NPE.
And also I would use the logical
&& instead of the bitwise
& operator. They seem to work the same but they are
quite different.
First, thank you for your continuous support :) And also, I need to check both conditions that's why i was using the & operator.
Re: Getting a null pointer exception.
Quote:
Originally Posted by
Ouzi
And also, I need to check both conditions that's why i was using the & operator.
Why you need to check both conditions?
Code java:
if (this.sender.length() > 0 & this.receiver.length() >0)
If the first is false then the whole if will be false, thus there is no need to check the second one. This is what && does. It evaluates the left side of the operation, if it's true, it continues and evaluates the right side.
And evaluations cost ;)
Re: Getting a null pointer exception.
Ah, my understanding was that the && operator checks if the first condition is true, then it doesn't evaluate the other. But you and a friend of mine at university today, made it clear that this wasn't the case :)
I need to check both conditions cause this is the assignment I have, I have to check that two fields aren't empty.
Re: Getting a null pointer exception.
The || operator will check only the first condition if it is true. The && operator will check only the first condition if it is false.