[Solved] null pointer exception in java servlet [closed]


I got a “null pointer exception” fault in java servlet. Could someone tell me what happens? And how to avoid that?

That happens when you’re trying to access/invoke some reference which is actually null.

SomeObject someObject = null;
someObject.doSomething(); // Throws NullPointerException.

You need to make sure that you only access/invoke it when it is not null.

SomeObject someObject = null;
if (someObject != null) {
    someObject.doSomething(); // Won't throw NullPointerException.
}

It’s just matter of logical thinking and understanding basic Java.


Another thing, I know java servlet can be used as part of a hybrid solution, could support many different programming languages. But how would the different programs execute/call each other and exchange information?(could someone tell me some words but codes).

Pass them as method parameter(s).


In addition, values extracted from a session object have to be converted(cast) to a specific type, but why? For example, how can I store a variable of type “int” in a session object. Could someone help me to figure it out?

Use Integer instead and/or profit Java 1.5’s autoboxing capabilities.

solved null pointer exception in java servlet [closed]