Comp Sci Java NullPointerException: How Can I Fix a Null Pointer Exception in My Code?

  • Thread starter Thread starter magnifik
  • Start date Start date
  • Tags Tags
    Java
AI Thread Summary
A NullPointerException occurs when accessing values in a doubly-linked list containing null elements, particularly when trying to call methods on a null pointer. The issue arises when using cursor._data.equals(o) without checking if cursor._data is null first. It is recommended to implement a series of if-statements to handle null checks before calling methods on potentially null objects. A try-catch block can also be used to catch exceptions, but proactive null checks are more efficient. The suggested code modification successfully resolved the issue by ensuring proper null handling.
magnifik
Messages
350
Reaction score
0
For the following segment of code, I am getting a null pointer exception when trying to access any value in a list (doubly-linked with dummy nodes) AFTER a null value. For example, if I have the following list of integers:

0, 1, 2, 3, null, 4, 5

and I run the function list.contains(0) or list.contains(1)...list.contains(null), the program works as it should. When I try to call list.contains(4) or list.contains(5), I receive a null pointer exception. I know the order in which I use == and .equals() matters, but should I separate it into multiple if statements? I tried to do so, and I was not receiving any different results.

for (Node cursor = _head._next; cursor != _tail; cursor = cursor._next){
if ((o == null & cursor._data == null) || cursor._data.equals(o)) // o is the Object data
return true;
 
Physics news on Phys.org
Hi magnifik! :smile:

You cannot use the function cursor._data.equal() if cursor._data is a null pointer.
Your current code does not prevent this (which manifests when you have o=4).

I recommend turning it into an if-statement where you just check if cursor._data is a null pointer, and then take appropriate action.
 
You can also consider using a try-catch statement similar to this:
Code:
try {
    cursor._data.equals(o)
} 
catch (NullPointerException e) {
    System.out.println("NullPointerException thrown");
}

I'm presuming that "I like Serena" is correct in that the exception is being caused by cursor._data.equal().
 
I like Serena said:
Hi magnifik! :smile:

You cannot use the function cursor._data.equal() if cursor._data is a null pointer.
Your current code does not prevent this (which manifests when you have o=4).

I recommend turning it into an if-statement where you just check if cursor._data is a null pointer, and then take appropriate action.

thank you for the suggestion! my program functioned correctly after using a series of if statements rather than my single one :)
 
magnifik said:
thank you for the suggestion! my program functioned correctly after using a series of if statements rather than my single one :)

Good! :smile:

And here's one for the road:

Code:
bool found = false;
for (Node cursor = _head._next; (cursor != _tail) && !found; cursor = cursor._next)
{
   // o is the Object data
   if (cursor._data == null) 
   {
      found = (o == null);  
   }
   else 
   {
      found = cursor._data.equals(o);
   }
}
return found;
 
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...

Similar threads

Replies
1
Views
2K
Replies
9
Views
4K
Replies
3
Views
2K
Replies
11
Views
12K
Replies
2
Views
6K
Replies
5
Views
3K
Replies
1
Views
1K
Replies
5
Views
3K
Replies
2
Views
4K
Back
Top