Null Pointer Exception in Hadoop Reduce method? - java

I'm building an inverted index and currently getting a null pointer exception in reduce when using context.write. Can anyone spot why? I presume something to do with serialising as I've never done that before? The error also happens when I print out h.

Two things that I can spot directly regarding the serialization without a stacktrace:
HMapValue needs a default constructor, it can't be created by Hadoop without one
In the default constructor you need to initialize the ArrayListWritable correctly (not null and it needs the setClass method to deserialize correctly.

Turns out it was because I had iterated over the data set twice (the line int df = Iterables.size(values); tricked me). The iterator hadn't reset hence the main block of reduce didnt run and finally I hit a null pointer because I tried accessing my data that hadn't even initialised.

Related

Issue with Objects.nonNull() behavior

Using Java 11. Noticing a strange behavior with very simple functionality. In the following code, if the expiration date is not null, only then it should try to extract the sql.Timestamp from the given Instant field.
preparedStatement.setTimestamp(expirationDateParameterIndex,
Objects.nonNull(memberReward.getExpirationDate())
? Timestamp.from(memberReward.getExpirationDate())
: null);
Problem is that even though the expiration date is set to null, Timestamp.from(..) is called and throwing NullPointerException.
java.lang.NullPointerException: null
at java.sql/java.sql.Timestamp.from(Timestamp.java:545)
The issue is not reproducible outside the project.
Here are the debugging screenshots:
Well, your debug screenshot is clear: null was passed to Timestamp.from(), which means memberReward.getExpirationDate() is null.
That's weird, because right before that, you check for that condition! Thus, we get to these explanations, all quite exotic:
getExpirationDate() is non-stable: It returns different values every time. Imagine it was implemented as follows: return random.coinFlip() == HEADS ? null : someDateObj; - then this can happen. One way to fix this is to call it once, save to a local variable, and carry on with that.
expirationDate is not immutable, and some other thread is setting the value in between your check and your read. This is unlikely, but technically possible, and suggests you need a serious rewrite of it all, having such casual shared mutable state between threads means this is just one of a ton of race conditions.
The code you're seeing is not the code that is running.
You're getting to Timestamp.from from different code than what you pasted.
That is not the Objects of java.util, and the nonNull method on it, is broken.
These all sound exotic, but it must be one of these things.

What is the purpose of Objects#requireNonNull

After checking the JavaDocs for a method I was thinking of using, requiredNonNull, I stumbled across the first one with the single parameter (T obj).
However what is the actual purpose of this particular method with this signature? All it simply does is throw and NPE which I'm somewhat positive (as a I may be missing something obvious here) would be thrown anyway.
Throws:
NullPointerException - if obj is null
The latter actually makes sense in terms of debugging certain code, as the doc also states, it's primarily designed for parameter validation
public static <T> T requireNonNull(T obj,String message)
Checks that the specified object reference is not null and throws a customized NullPointerException if it is.
Therefore I can print specific information along with the NPE to make debugging a hell of a lot easier.
With this in mind I highly doubt I would come across a situation where I'd rather just use the former instead. Please do enlighten me.
tl;dr - Why would you ever use the overload which doesn't take a message.
A good principle when writing software is to catch errors as early as possible. The quicker you notice, for example, a bad value such as null being passed to a method, the easier it is to find out the cause and fix the problem.
If you pass null to a method that is not supposed to receive null, a NullPointerException will probably happen somewhere, as you already noticed. However, the exception might not happen until a few methods further down, and when it happens somewhere deep down, it will be more difficult to find the exact source of the error.
So, it's better when methods check their arguments up front and throw an exception as soon as they find an invalid value such as null.
edit - About the one-parameter version: even though you won't provide an error message, checking arguments and throwing an exception early will be more useful than letting the null pass down until an exception happens somewhere deeper down. The stack trace will point to the line where you used Objects.requireNonNull(...) and it should be obvious to you as a developer that that means you're not supposed to pass null. When you let a NullPointerException happen implicitly you don't know if the original programmer had the intent that the variable should not be null.
It is a utility method. Just a shortcut! (shortcut designers have their ways of doing their shortcut style).
Why throwing in the first place?
Security and Debugging.
Security: to not allow any illegal value in a sensitive place. (makes inner algorithm more sure about what are they doing and what are they having).
Debugging: for the program to die fast when something unexpected happens.

very specific java constructors and Object class problem

I have an array at actionTable.get(state).
When I go to add an onject to the array, namely the Reduce, the properties of the reduce don't seem to go with it.
The array is of type Action[] where Action is the superclass of Reduce, could this be the reason?
Adding the reduce to the array:
actionTable.get(state)[t] = new Reduce(st.items.get(item).prod);
Checking to see if the field head is defined before adding it:
System.out.println(Prod.prods.get(st.items.get(item).prod).head);
Checking to see if the newly added reduce has the correct head field:
System.out.println(actionTable.get(state)[t].prod.head);
A NullPointerException occurs on the last print statement. The .prod part is defined but the .prod.head is null, even though the original prod object had a defined head.
This is the constructor for Reduce:
Reduce(int pr) {
p = pr;
length = Prod.prods.get(pr).length;
prod = Prod.prods.get(pr);
}
All of the RHS of the assignments in the constructor are defined. So, I don't understand why the head field, within the prod object that the new Reduce has access to is not defined when you access it through the actionTable.
Trust inheritance and all. Most likely with arrays is, that different array instances are involved (if you enlarge/copy array references). Some more System.out.println's will help there.
The first thing you always should do is this: got to your break points view in your IDE, check "stop on Exception thrown" and perhaps give the name NullPointerException.
Then run your code in the debugger and it will stop exactly at the point where the NullPointerException is thrown.

Java, Digester: stuck on this java.lang.NullPointerException

I'm parsing a XML file with Commons Digester and I don't understand what's wrong in my code: I'm stuck with this java.lang.NullPointerException.
THis is the code: http://pastie.org/1708374
and this is the exception: http://pastie.org/1708371
I guess it is a stupid error
thanks
Well, this is the problem:
if (centroids.length == 0)
You're never assigning a value to centroids as far as I can see, so it will always be null. Then when you try to dereference it in the line above, it will throw NullPointerException.
The first that the next line of code tried to use centroids[0] suggests that you don't really understand Java arrays. Perhaps you really wanted a List of some description?
I would also strongly suggest that instead of a Map<String, String> which always has the same five keys, you create a type (e.g. Centroid) which has the properties title, description, tags, time, and event. Then you can just make centroids a List<Centroid>:
List<Centroid> centroids = new ArrayList<Centroid>();
then when you get some data...
Centroid centroid = new Centroid(...);
centroids.add(centroid);
Oh, and you're also currently using == to compare strings... don't do that: use equals, as otherwise you'll be comparing string references.
As a general note on how to read a NPE stacktrace:
When you get an exception stack trace, look at the Caused by line and the first line after it
Caused by: java.lang.NullPointerException
at CentroidGenerator.nextItem2(CentroidGenerator.java:31)
A null pointer exception most often occurs when you try to invoke a method on an object and that object is null.
The error message above tells you that the error occurs on line 31 of CentroidGenerator.java:
if (centroids.length == 0) {
Method invokation is of the format object.method, so you know that in this instance the object that is null is centroids.
A quick visual way to determine what's null is to just look at what's on the left of dots on the line where the exception occurs. In lines where you have multiple method calls, you don't immediately know what object is null and you may need some more exploration, but not in this instance.
To fix the problem, refer to Jon's answer.

NullPointerException error

I am developing in Eclipse a new Java project which uses an existing application (I have added its jar in my project build path). When I create an object of a class (say Model) from this existing application and use any of its methods,
Model model = new Model();
model.start();
I get the following error:
Exception in thread "main" java.lang.NullPointerException
at main.gui.mainwindow.MainWindow.run(MainWindow.java:56)
at main.gui.ApplicationController.main(ApplicationController.java:21)
When I create only the Model object (without calling its method), no error is given and my application works perfectly, does anyone know where the problme may lie? Thanks in advance!
It sounds like there is an uninitialized value inside of your Model class and when you call start(), the class is trying to use the uninitialized value.
You might want to provide a concrete example rather than being Vague. You could be trying to use a class that has a dependency that you haven't set before trying to use some specific part of the class.
If Model (or whatever the real class may be) is something developed by somebody there, I would suggest asking them what could be going wrong or what you might be missing.
The oracle documentations says:
Thrown when an application attempts to
use null in a case where an object is
required. These include:
* Calling the instance method of a null object.
* Accessing or modifying the field of a null object.
* Taking the length of null as if it were an array.
* Accessing or modifying the slots of null as if it were an array.
* Throwing null as if it were a Throwable value.
Applications should throw instances of
this class to indicate other illegal
uses of the null object.
Check if anything in here matches your case. It is impossible to say more without actually seeing the code.

Categories

Resources