?
Solved

Java.lang.nullpointerexception

Posted on 2006-04-14
15
Medium Priority
?
284 Views
Last Modified: 2011-09-20
What is the fix for this error
0
Comment
Question by:AmericanLand
  • 2
  • 2
  • 2
  • +6
13 Comments
 

Author Comment

by:AmericanLand
ID: 16455774
I need to find a fix for this error
0
 
LVL 1

Expert Comment

by:sriki77
ID: 16455802
please give the stack trace
0
 
LVL 1

Accepted Solution

by:
geekprog earned 340 total points
ID: 16455876
The object ( String object for example  ) you are using in your code has NULL value

for example, consider a HastTable

HashTable ht = new HashTable();
String[]  s = new String();

   for(int i=0;i<=10;i++)
   {
          ht.add( s[i] , "Naveen");
   }


in the above sample code, object s value is NULL and HashTable doesn't accept key values as NULL
and it will throw  Java.lang.nullpointerexception error
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 332 total points
ID: 16455881
You can fix it right away by assigning a value to that uninitialised reference variable that causes it.
And the fatal message lists in what method on what line it happens.

;JOOP!
0
 
LVL 23

Assisted Solution

by:Siva Prasanna Kumar
Siva Prasanna Kumar earned 332 total points
ID: 16455954
It will be easy if you put forward the stack trace as such you just need to check the variable for NULL before trying to print it or trying to insert in the database in a non nullable field.

so just before doing any such thing just check the things in this way.

For example

Shiva shiva=NULL;

System.out.println(shiva.toString());

will surely fail.

so you can just use a try & catch block or do a check & then proceed.

like

if(shiva!=NULL)
{
System.out.println(shiva.toString());

}

0
 
LVL 10

Expert Comment

by:radarsh
ID: 16460138
>> Shiva shiva=NULL;

Should be

Siva shiva = null;

;-)
0
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 16460198
it can also be

RAdarsh radarsh = null;

..
..
..

while(true)
{
if(radarsh!=null)
radarsh=null;
}

Oops that was a Typo in my case.Any way thanks for correcting radarsh But i think only NULL must be null, no need of changing the class name from Shiva to Siva.

;-)

Bye.
0
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 332 total points
ID: 16461107
Hi AmericanLand,

This exception is thrown when you are using null as object.

Examples :
    Object obj;             // <- not allocated (null)
    obj.toString();

    Object obj=hashmap.get("key"); // <- no associated object for "key"
    obj.toString();

    ...

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/NullPointerException.html
http://mindprod.com/jgloss/nullpointerexception.html
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16461123
Other examples with arrays :

   int[] counters;  // == null (not allocated)
   couters[0]
->
    int[] counters = new int[10];


   Object[] objects=new Object[10];  // array allocated, but elements are null
   objects[0].toString();   // objects!=null    objects[0]==null
->
    objects[0] = new Object();

0
 
LVL 10

Expert Comment

by:radarsh
ID: 16461176
Guys, let's wait for the questioner to post some kind of stack trace now.

________
radarsh
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 332 total points
ID: 16464139
0
 
LVL 23

Assisted Solution

by:rama_krishna580
rama_krishna580 earned 332 total points
ID: 16480906
Hi,

Java has a powerful concept for exception and error handling. An exception is an error that occurs at runtime. It is either generated by the Java Virtual Machine (VM) in response to an unexpected condition or it is generated by your code as a result of executing a throw statement.

Understanding Exceptions

Exceptions generated from runtime are called unchecked exceptions, since it is not possible for the compiler to determine that your code will handle the exception. Exception classes that descend from RuntimeException and Error classes are unchecked exceptions. Examples for RuntimeException are illegal cast operation, inappropriate use of a null pointer, referencing an out of bounds array element. Error exception classes signal critical problems that typically cannot be handled by your application. Examples are out of memory error, stack overflow, failure of the Java VM.

Thrown exceptions are referred to as checked exceptions. The compiler will confirm at compile time that the method includes code that might throw an exception. Moreover the compiler requires the code that calls such a method to include this call within a try block, and provide an appropriate catch block to catch the exception.

java.lang.Object
   |
   +--java.lang.Throwable
         |
         +--java.lang.Exception
         |     |
         |     +--java.lang.ClassNotFoundException
         |     |
         |     +--java.io.IOException
         |     |     |
         |     |     +--java.io.FileNotFoundException
         |     |
         |     +--java.lang.RuntimeException
         |           |
         |           +--java.lang.NullPointerException
         |           |
         |           +--java.lang.IndexOutOfBoundsException
         |                 |
         |                 +--java.lang.ArrayIndexOutOfBoundsException
         |
         +--java.lang.Error
               |
               +--java.lang.VirtualMachineError
                     |
                     +--java.lang.OutOfMemoryError

When an (either checked or unchecked) exception is thrown, execution will attempt to immediately branch to the first catch block whose associated exception class matches the class or a superclass of the thrown exception. If the exception does not occur within a try block or the thrown exception is not caught in a matching catch block, execution of the method immediately terminates and control returns to the invoker of the method, where this process is repeated. The result is that the exception chain is escalated until a matching catch block is found. If not, the thread containing the thrown exception is terminated.

First Example

The following Demo1 class demonstrates the behaviour of exceptions and applications.

import java.io.*;

class Demo1 {

  public static FileInputStream f1(String fileName)
    throws FileNotFoundException
  {
    FileInputStream fis = new FileInputStream(fileName);
    System.out.println("f1: File input stream created");
    return fis;
  }

  public static FileInputStream f2(String fileName)
  {
    FileInputStream fis = null;
    try
    {
      fis = new FileInputStream(fileName);
    }
    catch (FileNotFoundException ex)
    {
      System.out.println("f2: Oops, FileNotFoundException caught");
    }
    finally
    {
      System.out.println("f2: finally block");
    }
    System.out.println("f2: Returning from f2");
    return fis;
  }

  public static void main(String args[])
  {
    FileInputStream fis1 = null;
    FileInputStream fis2 = null;
    String fileName = "foo.bar";
    // String fileName = null;

    System.out.println(  "main: Starting " + Demo1.class.getName()
                       + " with file name = " + fileName);

    // get file input stream 1
    try {
      fis1 = f1(fileName);
    }
    catch (FileNotFoundException ex)
    {
      System.out.println("main: Oops, FileNotFoundException caught");
    }
    catch (Exception ex)
    {
      System.out.println("main: Oops, genreal exception caught");
    }

    // get file input stream 2
    fis2 = f2(fileName);

    System.out.println("main: " + Demo1.class.getName() + " ended");
  }
}

Compile and run the Demo1 class will generate output as follows (assuming that the file foo.bar does not exist):

main: Starting Demo1 with file name = foo.bar
main: Oops, FileNotFoundException caught
f2: Oops, FileNotFoundException caught
f2: finally block
f2: Returning from f2
main: Demo1 ended

The example program tries to create two file input streams. Therefore two methods f1 and f2 are implemented. First, the main program calls f1 method. Because the constructor of the FileInputStream throws a FileNotFoundException the method f1 must be defined with the throws FileNotFoundException in the method definition. The thrown exception is not handled in the method but forwarded to the invoker. Note, that the system output before the return statement is never executed.

So the invoker, in our example the main program, must catch this exception. The method f1 is called in a try block. The following catch blocks catch either a FileNotFoundException or a general Exception. In our example, the exception is caught in the first catch block and the system output is generated. Since the exception in f1 is caught and handled, the execution of the program is not terminated.

Second, the example program creates another FileInputStream invoking the f2 method. This method catches the FileNotFoundException, so this exception must not be forwarded to the invoker. The try ... catch statement is followed by a finally block. This block is always executed, regardless whether or not an exception occurs within the try block. This generates the system output in our example. Again, the exception is caught and the program executes, this generates the system output in f2 and main.

This was a straight forward example with caught exceptions. What happens with uncaught exceptions? Change the fileName assignment in the main method: Comment out the first assignment and activate the second String fileName = null; then compile and execute Demo1 again. The generated output is:

main: Starting Demo1 with file name = null
main: Oops, genreal exception caught
f2: finally block
java.lang.NullPointerException
java.io.FileInputStream Demo1.f2(java.lang.String)
void Demo1.main(java.lang.String[])
Exception in thread main

This time, we try to create two FileInputStream objects using null instead of a file name. Invoking f1 will generate a NullPointerException which is caught in the main program. Next f2 is called which could handle only the FileNotFoundException but not a NullPointerException. Nevertheless the finally block is executed and then the control returns to the main program. Because there is no try ... catch statement around the call to f2 and no matching catch block is found, the thread is terminated. Note, that f2 and main can not execute the system output statements any more.

Second Example

The second example will show some special behaviour in catch and finally blocks:

import java.io.*;

class Demo2 {

  public static FileInputStream f1(String fileName)
  {
    FileInputStream fis = null;
    try
    {
      fis = new FileInputStream(fileName);
    }
    catch (FileNotFoundException ex)
    {
      System.out.println("f1: Oops, FileNotFoundException caught");
      throw new Error("f1: File not found");
    }
    System.out.println("f1: File input stream created");
    return fis;
  }

  public static FileInputStream f2(String fileName)
  {
    FileInputStream fis = null;
    try
    {
      fis = new FileInputStream(fileName);
    }
    catch (FileNotFoundException ex)
    {
      System.out.println("f2: Oops, FileNotFoundException caught");
      return fis;
    }
    finally
    {
      System.out.println("f2: finally block");
      return fis;
    }

    // Compiler error: statement not reacheable
    // System.out.println("f2: Returning from f2");
    // return fis;
  }

  public static void main(String args[])
  {
    FileInputStream fis1 = null;
    FileInputStream fis2 = null;
    String fileName = "foo.bar";
    // String fileName = null;

    System.out.println(  "main: Starting " + Demo2.class.getName()
                       + " with file name = " + fileName);

    // get file input stream 1
    try {
      fis1 = f1(fileName);
    }
    catch (Exception ex)
    {
      System.out.println("main: Oops, general exception caught");
    }
    catch (Throwable th)
    {
      System.out.println("main: Oops, throwable object caught");
    }

    // get file input stream 2
    fis2 = f2(fileName);

    System.out.println("main: " + Demo2.class.getName() + " ended");
  }
}
}

Compile and run Demo2 will generate the following output:

main: Starting Demo2 with file name = foo.bar
f1: Oops, FileNotFoundException caught
main: Oops, throwable object caught
f2: Oops, FileNotFoundException caught
f2: finally block
main: Demo2 ended

Again f1 is called to get a new FileInputStream with a given file name. The thrown FileNotFoundException in f1 is caught in the following catch block. But this time an Error is thrown so that the method is terminated immediately. Because Error is not a subclass of Exception the first catch block does not match. But the second catch block matches all throwable objects.

Calling f2 method will generate a FileNotFoundException too. This exception is caught in f2 and the method returns directly from the catch block. Remember, that the finally block is executed regardless whether an exception is caught or not.

In both methods f1 and f2, the FileNotFoundException is caught and handled, so the program can terminate normally. Change again the fileName assignment in the main method and compile and run Demo2 again:

main: Starting Demo2 with file name = null
main: Oops, general exception caught
f2: finally block
main: Demo2 ended

The NullPointerException thrown in f1 is not caught, so the exception is forwarded to the invoker. The main program catches Exception, which is a superclass of the thrown exception.

The call to f2 method seems to work fine even the thrown NullPointerException is not caught directly. Note, that the finally block is executed regardless whether an exception is caught or not. Since the finally block terminates with a return statement, the program executes without failure. So be careful returning within finally blocks, this breaks the exception chaining to the invoker and it simulates error free program execution.

R.K
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16484543
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month16 days, 20 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question