answersLogoWhite

0

Java null pointer exceptions

Updated: 8/11/2023
User Avatar

Wiki User

11y ago

Best Answer

A null pointer exception in java comes when you are trying to perform any action on an object that isnt initialized/has a value i.e., is a NULL Value

Ex:

private String s; //declare a string

if(s.equals("test")){

//do something..

}

You will get a null pointer in the if condition because you are checking a value that is null which is not allowed..

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Java null pointer exceptions
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can NullPointerException be used with ArithmaticException using throws keyword in java?

No. You cannot throw or catch Null pointer exceptions


Java does not support pointers then what is null pointer exception?

A null pointer exception is thrown when you are trying to manipulate an object that is null. It is just the name and does not have any relevance to the pointers as in C Example: ArrayList lst = null; Object obj = lst.get(0); In the first line we have declared an array list. Without initializing it we have tried to access the element in the 0th position. This would cause a null pointer exception.


Describe the JAVA throwable class hierarchy and types of exceptions?

In Java there are two main types of Exceptions. * Checked Exceptions - The ones that can be checked & handled in our code. Ex: I/O Exception, SQL Exception etc. In most cases, the compiler itself forces us to catch & handle these exceptions * Un-checked Exceptions - The ones that we cannot & should not handle in our code. Ex. Null Pointer Exception The java.lang.Throwable is the super class of all errors and exceptions in Java. Only objects of this class can be thrown & caught and handled by try-catch blocks. Ex: try { ..... ..... } catch (Exception e){ ... } finally { ... }


How can you fix uncaught exception java lang Null Pointer Exception on blackberry 8300?

The best way to fix uncaught exception java lang Null Pointer Exception on a Blackberry 8300 is by doing a restart. Shut the phone off and remove the battery for a minute. Replace the battery and turn the phone back on.


When you let your pointer linger over an object the pointer is?

The pointer is non-NULL.

Related questions

Can NullPointerException be used with ArithmaticException using throws keyword in java?

No. You cannot throw or catch Null pointer exceptions


Java does not support pointers then what is null pointer exception?

A null pointer exception is thrown when you are trying to manipulate an object that is null. It is just the name and does not have any relevance to the pointers as in C Example: ArrayList lst = null; Object obj = lst.get(0); In the first line we have declared an array list. Without initializing it we have tried to access the element in the 0th position. This would cause a null pointer exception.


Describe the JAVA throwable class hierarchy and types of exceptions?

In Java there are two main types of Exceptions. * Checked Exceptions - The ones that can be checked & handled in our code. Ex: I/O Exception, SQL Exception etc. In most cases, the compiler itself forces us to catch & handle these exceptions * Un-checked Exceptions - The ones that we cannot & should not handle in our code. Ex. Null Pointer Exception The java.lang.Throwable is the super class of all errors and exceptions in Java. Only objects of this class can be thrown & caught and handled by try-catch blocks. Ex: try { ..... ..... } catch (Exception e){ ... } finally { ... }


How can you fix uncaught exception java lang Null Pointer Exception on blackberry 8300?

The best way to fix uncaught exception java lang Null Pointer Exception on a Blackberry 8300 is by doing a restart. Shut the phone off and remove the battery for a minute. Replace the battery and turn the phone back on.


What is a null macro what is the difference between a null pointer and a null macro?

NULL Macro is simply what is defined as 0 in a macro provided by the libraryNull pointer is a pointer which has 0 or NULL value stored and points to nowhwere still it points to 0x00 i.e. the first memory location of the OSNull pointer != Uninitialized pointer because an uninitialised pointer can point anywhere in the memory location ...but a NULL pointer surely points to no where(but still behind the scene we can say that it only points to 0x00). Never we can retrive a Null pointer location using th"&" operator..neither will malloc/calloc return NULL IF THERE IS SPACE IN THE MEMORY. NULL pointer is unique !!nishantnitb@aol.com


What is the use of null pointer?

Pointer is a variable that stores address of a variable . A NULL Pointer a pointer that doesn't point to anything, it is a literal zero .Some people ,notably C++ programmers, prefer to use 0 rather than NULL.


When you let your pointer linger over an object the pointer is?

The pointer is non-NULL.


Why you get null pointer assignment error occurs in c?

You haven't assigned the pointer yet, so it's initialized as NULL, or you're trying to assign NULL to the value of the pointer. You have to check if the value is NULL before you use it, or you'll end up with errors just like this.


What is the size of null pointer?

A NULL pointer has the same size as a non NULL pointer. NULL means that the pointer has been set to the NULL value that is usually zero (0) but the NULL value is at the digression of the compiler manufacture (and may have a value other than zero) so a pointer should always be set to the NULL value and not zero. Current compilers (32 and 64 bit, Intel chip) have a pointer size of 4 (8 bit) bytes. It should be noted that the number of bits in any data type is at the compiler manufactures digression but is heavily influenced by the computer hardware. void *p= NULL; printf ("%d\n", sizeof (p)); or printf ("%d\n", sizeof (void *));


How will you declare null pointer in C?

#define NULL ((void *)0) /* defined in <stddef.h> */ const char *mynullvar = NULL;


Difference between void pointer and null pointer?

A Null pointer has the value 0. void pointer is a generic pointer introduced by ANSI. Before ANSI, char pointers are used as generic pointer. Generic pointer can hold the address of any data type. Pointers point to a memory address, and data can be stored at that address.


What is the difference between a null pointer and a null macro?

Using a NULL macro to make C portableI'll assume that you're asking your question for C type language programming. A NULL pointer is a pointer that's guarnteed to point to nothing. This may be 0 in a UNIX/Linux system or some other address in another system. Using the NULL macro to set/initialize your pointers will make your programs more portable among systems than using something like the 0.#include char *c = 0; // initialize to NULL--not portablechar *p = NULL; // initialize to NULL as defined in stdio is portableAddendumThe code:char *c = 0;actually is portable because the compiler converts 0's used in a pointer context (cast to a pointer) to the machine's representation of a NULL pointer, which may or may not be all 0 bits. The NULL macro itself might be defined as something like 0 or (void *)0, and both definitions are portable. As a corollary, the following code is also portable:if (!c) {// do something}because it is equivalent to:if (c != 0) {// do something}and the 0 above is converted to a NULL pointer because it is being compared with a pointer.